"use client"

import { useRatesStore } from "@/stores/select-rate"

import { RoomContext } from "@/contexts/SelectRate/Room"

import type { RoomProviderProps } from "@/types/providers/select-rate/room"

export default function RoomProvider({
  children,
  idx,
  room,
}: RoomProviderProps) {
  const activeRoom = useRatesStore((state) => state.activeRoom)
  const closeSection = useRatesStore((state) => state.actions.closeSection(idx))
  const modifyRate = useRatesStore((state) => state.actions.modifyRate(idx))
  const roomAvailability = useRatesStore(
    (state) => state.roomsAvailability?.[idx]
  )
  const selectFilter = useRatesStore((state) => state.actions.selectFilter(idx))
  const selectRate = useRatesStore((state) => state.actions.selectRate(idx))
  const selectRateRedemption = useRatesStore((state) =>
    state.actions.selectRateRedemption(idx)
  )
  const selectRateCheque = useRatesStore((state) =>
    state.actions.selectRateCheque(idx)
  )
  const selectRateVoucher = useRatesStore((state) =>
    state.actions.selectRateVoucher(idx)
  )
  const roomNr = idx + 1
  return (
    <RoomContext.Provider
      value={{
        ...room,
        actions: {
          closeSection,
          modifyRate,
          selectFilter,
          selectRate,
          selectRateRedemption,
          selectRateCheque,
          selectRateVoucher,
        },
        isActiveRoom: activeRoom === idx,
        isMainRoom: roomNr === 1,
        roomAvailability,
        roomNr,
        totalRooms: room.rooms.length,
      }}
    >
      {children}
    </RoomContext.Provider>
  )
}
