import { BookingStatusEnum } from "@/constants/booking" import { dt } from "@/lib/dt" import { formatChildBedPreferences } from "../utils" import { convertToChildType } from "./convertToChildType" import { getPriceType } from "./getPriceType" import { getBreakfastPackagesFromBookingFlow } from "./hasBreakfastPackage" import type { BreakfastPackage } from "@/types/components/hotelReservation/breakfast" import { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter" import { BreakfastPackageEnum } from "@/types/enums/breakfast" import { PackageTypeEnum } from "@/types/enums/packages" import type { Room } from "@/types/hotel" import type { BookingConfirmation } from "@/types/trpc/routers/booking/confirmation" import type { Room as MyStayRoom } from "@/stores/my-stay/myStayRoomDetailsStore" interface MapRoomDetailsParams { booking: BookingConfirmation["booking"] room: (Room & { bedType: Room["roomTypes"][number] }) | null roomNumber: number } export function mapRoomDetails({ booking, room, roomNumber, }: MapRoomDetailsParams): MyStayRoom { const nights = dt(booking.checkOutDate) .startOf("day") .diff(dt(booking.checkInDate).startOf("day"), "days") const breakfastPackages = getBreakfastPackagesFromBookingFlow( booking.packages ) const featuresPackages = booking.packages.filter( (pkg) => pkg.code === RoomPackageCodeEnum.PET_ROOM || pkg.code === RoomPackageCodeEnum.ALLERGY_ROOM || pkg.code === RoomPackageCodeEnum.ACCESSIBILITY_ROOM ) const breakfast: BreakfastPackage | null = breakfastPackages?.length ? { code: BreakfastPackageEnum.REGULAR_BREAKFAST, description: breakfastPackages[0].description, localPrice: { currency: breakfastPackages[0].currency, price: breakfastPackages.reduce( (acc, curr) => acc + curr.unitPrice, 0 ), totalPrice: breakfastPackages.reduce( (acc, curr) => acc + curr.totalPrice, 0 ), }, requestedPrice: { currency: breakfastPackages[0].currency, price: breakfastPackages.reduce( (acc, curr) => acc + curr.unitPrice, 0 ), totalPrice: breakfastPackages.reduce( (acc, curr) => acc + curr.totalPrice, 0 ), }, packageType: PackageTypeEnum.BreakfastAdult, } : null const isCancelled = booking.reservationStatus === BookingStatusEnum.Cancelled const childrenAsString = formatChildBedPreferences({ childrenAges: booking.childrenAges, childBedPreferences: booking.childBedPreferences, }) const childrenInRoom = convertToChildType( booking.childrenAges, booking.childBedPreferences ) const priceType = getPriceType( booking.cheques, booking.roomPoints, booking.vouchers ) return { hotelId: booking.hotelId, roomTypeCode: booking.roomTypeCode, adults: booking.adults, childrenAges: booking.childrenAges, checkInDate: booking.checkInDate, checkOutDate: booking.checkOutDate, confirmationNumber: booking.confirmationNumber, cancellationNumber: booking.cancellationNumber, createDateTime: booking.createDateTime, rateDefinition: booking.rateDefinition, guaranteeInfo: booking.guaranteeInfo, linkedReservations: booking.linkedReservations, bookingCode: booking.bookingCode, cheques: booking.cheques, vouchers: booking.vouchers, isCancelable: booking.isCancelable, multiRoom: booking.multiRoom, canChangeDate: booking.canChangeDate, guest: booking.guest, currencyCode: booking.currencyCode, vatPercentage: booking.vatPercentage, mainRoom: booking.mainRoom, roomName: room?.name ?? "", roomNumber, isCancelled, childrenInRoom, childrenAsString, terms: booking.rateDefinition.cancellationText, packages: featuresPackages.map((pkg) => ({ code: pkg.code as RoomPackageCodeEnum, description: pkg.description, inventories: [], itemCode: "", localPrice: { currency: pkg.currency, price: pkg.unitPrice, totalPrice: pkg.totalPrice, }, requestedPrice: { currency: pkg.currency, price: pkg.unitPrice, totalPrice: pkg.totalPrice, }, })), bedType: { description: room?.bedType.mainBed.description ?? "", roomTypeCode: room?.bedType.code ?? "", }, roomPoints: booking.roomPoints, roomPrice: { perNight: { local: { currency: booking.currencyCode, price: isCancelled ? 0 : booking.roomPrice / nights, }, requested: undefined, }, perStay: { local: { currency: booking.currencyCode, price: isCancelled ? 0 : booking.roomPrice, }, requested: undefined, }, }, totalPriceExVat: booking.totalPriceExVat, totalPrice: booking.totalPrice, vatAmount: booking.vatAmount, breakfast, priceType, } }