"use client"

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

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

import { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"
import type { RoomProviderProps } from "@/types/providers/select-rate/room"

export default function RoomProvider({
  children,
  idx,
  room,
}: RoomProviderProps) {
  const { activeRoom, roomAvailability, roomPackages } = useRatesStore(
    (state) => ({
      activeRoom: state.activeRoom,
      roomPackages: state.roomsPackages[idx],
      roomAvailability: state.roomsAvailability?.[idx],
    })
  )
  const roomNr = idx + 1

  const petRoomPackage = roomPackages.find(
    (pkg) => pkg.code === RoomPackageCodeEnum.PET_ROOM
  )

  return (
    <RoomContext.Provider
      value={{
        ...room,
        isActiveRoom: activeRoom === idx,
        isMainRoom: roomNr === 1,
        petRoomPackage,
        roomAvailability,
        roomPackages,
        roomNr,
        totalRooms: room.rooms.length,
      }}
    >
      {children}
    </RoomContext.Provider>
  )
}
