import type {
  Rate,
  Room as SelectRateRoom,
} from "@/types/components/hotelReservation/selectRate/selectRate"
import type { Room } from "@/components/HotelReservation/PriceDetailsModal/PriceDetailsTable"

export function mapToPrice(
  rooms: (Rate | null)[],
  bookingRooms: SelectRateRoom[],
  isUserLoggedIn: boolean
) {
  return rooms
    .map((room, idx) => {
      if (!room) {
        return null
      }

      let price = null
      if ("corporateCheque" in room.product) {
        price = {
          corporateCheque: room.product.corporateCheque.localPrice,
        }
      } else if ("redemption" in room.product) {
        price = {
          redemption: room.product.redemption.localPrice,
        }
      } else if ("voucher" in room.product) {
        price = {
          voucher: room.product.voucher,
        }
      } else {
        const isMainRoom = idx === 0
        const memberRate = room.product.member
        const onlyMemberRate = !room.product.public && memberRate
        if ((isUserLoggedIn && isMainRoom && memberRate) || onlyMemberRate) {
          price = {
            regular: memberRate.localPrice,
          }
        } else if (room.product.public) {
          price = {
            regular: room.product.public.localPrice,
          }
        }
      }

      const bookingRoom = bookingRooms[idx]
      return {
        adults: bookingRoom.adults,
        bedType: undefined,
        breakfast: undefined,
        breakfastIncluded: room.product.rateDefinition.breakfastIncluded,
        childrenInRoom: bookingRoom.childrenInRoom,
        packages: room.packages,
        price,
        roomType: room.roomType,
      }
    })
    .filter((r) => !!(r && r.price)) as Room[]
}
