"use client"
import { PropsWithChildren, useRef } from "react"

import {
  GuestsRoomsContext,
  type GuestsRoomsStore,
  initGuestsRoomsState,
} from "@/stores/guests-rooms"

import { GuestsRoom } from "@/types/components/bookingWidget/guestsRoomsPicker"

export default function GuestsRoomsProvider({
  selectedGuests,
  children,
}: PropsWithChildren<{ selectedGuests?: GuestsRoom[] }>) {
  const initialStore = useRef<GuestsRoomsStore>()
  if (!initialStore.current) {
    initialStore.current = initGuestsRoomsState(selectedGuests)
  }

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