"use client"

import { useRef } from "react"

import { createBookingConfirmationStore } from "@/stores/booking-confirmation"

import { BookingConfirmationContext } from "@/contexts/BookingConfirmation"

import type { BookingConfirmationStore } from "@/types/contexts/booking-confirmation"
import type { BookingConfirmationProviderProps } from "@/types/providers/booking-confirmation"

export default function BookingConfirmationProvider({
  bookingCode,
  children,
  currencyCode,
  fromDate,
  toDate,
  rooms,
  vat,
}: BookingConfirmationProviderProps) {
  const storeRef = useRef<BookingConfirmationStore>()

  if (!storeRef.current) {
    const initialData = {
      bookingCode,
      currencyCode,
      fromDate,
      toDate,
      rooms,
      vat,
    }

    storeRef.current = createBookingConfirmationStore(initialData)
  }

  return (
    <BookingConfirmationContext.Provider value={storeRef.current}>
      {children}
    </BookingConfirmationContext.Provider>
  )
}
