"use client"

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

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

import type { RoomProviderProps } from "@/types/providers/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 selectFilter = useRatesStore((state) => state.actions.selectFilter(idx))
  const selectRate = useRatesStore((state) => state.actions.selectRate(idx))
  const totalRooms = useRatesStore((state) => state.allRooms.length)
  const roomNr = idx + 1
  return (
    <RoomContext.Provider
      value={{
        ...room,
        actions: {
          closeSection,
          modifyRate,
          selectFilter,
          selectRate,
        },
        isActiveRoom: activeRoom === idx,
        isMainRoom: roomNr === 1,
        roomNr,
        totalRooms,
      }}
    >
      {children}
    </RoomContext.Provider>
  )
}
