!> @file schemes_2d.f90
!> @brief Bind a 2D solver state's reconstruction pointer + stencil geometry by
!!        name, reusing the dimension-agnostic 1D reconstructor routines directly.
!!
!! Every bound reconstructor operates on assumed-shape arrays, and any internal
!! work arrays they declare are sized from `size(f_hat)` (not a fixed neq), so
!! the same 1D kernels reconstruct the 2D Euler system's four components
!! correctly — no neq-specific 2D wrappers are needed. (MUSCL/upwind have no
!! such work arrays and were already neq-agnostic.)
!! (Previously weno5/weno7/mp5/teno5 had thin neq2d-sized copies here; the 1D
!! kernels were made `size(f_hat)`-driven, matching weno9, so those copies were
!! removed.)
module schemes_2d
  use solver_state_2d, only: solver_state_2d_t
  use logger, only: log_warn
  use muscl, only: muscl_minmod_reconstruct
  use upwind1, only: upwind1_reconstruct
  use upwind2, only: upwind2_reconstruct
  use weno5_js, only: weno5_js_reconstruct
  use weno7_js, only: weno7_js_reconstruct
  use weno9_js, only: weno9_js_reconstruct
  use mp5, only: mp5_reconstruct
  use teno5, only: teno5_reconstruct
  use option_registry, only: recon_weno5, recon_weno7, recon_weno9, recon_mp5, &
                             recon_teno5, recon_muscl, recon_upwind1, recon_upwind2
  implicit none
  private
  public :: init_recon_scheme_2d

contains

  ! ---------------------------------------------------------------------------
  !> Bind the reconstruction procedure pointer and stencil metadata in @p state
  !! to the requested scheme (taken from state%cfg%recon_scheme).
  ! ---------------------------------------------------------------------------
  subroutine init_recon_scheme_2d(state)
    type(solver_state_2d_t), intent(inout) :: state

    select case (trim(state % cfg % recon_scheme))
    case (recon_weno5)
      state % reconstruct => weno5_js_reconstruct
      state % stencil_width = 5; state % stencil_start_offset = -3; state % halo_width = 3
    case (recon_weno7)
      state % reconstruct => weno7_js_reconstruct
      state % stencil_width = 7; state % stencil_start_offset = -4; state % halo_width = 4
    case (recon_weno9)
      state % reconstruct => weno9_js_reconstruct
      state % stencil_width = 9; state % stencil_start_offset = -5; state % halo_width = 5
    case (recon_mp5)
      state % reconstruct => mp5_reconstruct
      state % stencil_width = 5; state % stencil_start_offset = -3; state % halo_width = 3
    case (recon_teno5)
      state % reconstruct => teno5_reconstruct
      state % stencil_width = 5; state % stencil_start_offset = -3; state % halo_width = 3
    case (recon_muscl)
      state % reconstruct => muscl_minmod_reconstruct
      state % stencil_width = 3; state % stencil_start_offset = -2; state % halo_width = 2
    case (recon_upwind1)
      state % reconstruct => upwind1_reconstruct
      ! upwind1 reads only 1-2 cells near the face; stencil metadata is set
      ! to WENO5 defaults so ghost-cell allocation is consistently sized,
      ! matching the 1D reconstruction.f90 convention.
      state % stencil_width = 5; state % stencil_start_offset = -3; state % halo_width = 3
    case (recon_upwind2)
      state % reconstruct => upwind2_reconstruct
      state % stencil_width = 3; state % stencil_start_offset = -2; state % halo_width = 2
    case default
      call log_warn('schemes_2d: unknown recon_scheme "'//trim(state % cfg % recon_scheme)// &
                    '"; falling back to weno5')
      state % reconstruct => weno5_js_reconstruct
      state % stencil_width = 5; state % stencil_start_offset = -3; state % halo_width = 3
    end select
  end subroutine init_recon_scheme_2d

end module schemes_2d
