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

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

import { EnterDetailsProviderProps } from "@/types/components/hotelReservation/enterDetails/store"

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

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