"use client"
import { useSearchParams } from "next/navigation"
import { PropsWithChildren, useRef } from "react"

import {
  EnterDetailsContext,
  type EnterDetailsStore,
  initEditDetailsState,
} from "@/stores/enter-details"

import { StepEnum } from "@/types/components/hotelReservation/enterDetails/step"

export default function EnterDetailsProvider({
  step,
  children,
}: PropsWithChildren<{ step: StepEnum }>) {
  const searchParams = useSearchParams()
  const initialStore = useRef<EnterDetailsStore>()
  if (!initialStore.current) {
    initialStore.current = initEditDetailsState(step, searchParams)
  }

  return (
    <EnterDetailsContext.Provider value={initialStore.current}>
      {children}
    </EnterDetailsContext.Provider>
  )
}
