import { BookingStatusEnum, CancellationRuleEnum } from "@/constants/booking" import { dt } from "@/lib/dt" import { formatChildBedPreferences } from "../utils" import { convertToChildType } from "./convertToChildType" 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 breakfastPkg = booking.packages.find( (pkg) => pkg.code === BreakfastPackageEnum.REGULAR_BREAKFAST || pkg.code === BreakfastPackageEnum.FREE_MEMBER_BREAKFAST || pkg.code === BreakfastPackageEnum.SPECIAL_PACKAGE_BREAKFAST ) const featuresPkg = booking.packages.filter( (pkg) => pkg.code === RoomPackageCodeEnum.PET_ROOM || pkg.code === RoomPackageCodeEnum.ALLERGY_ROOM || pkg.code === RoomPackageCodeEnum.ACCESSIBILITY_ROOM ) const breakfast: BreakfastPackage | false = breakfastPkg ? { code: breakfastPkg.code, description: breakfastPkg.description, localPrice: { currency: breakfastPkg.currency, price: breakfastPkg.unitPrice, totalPrice: breakfastPkg.totalPrice, }, requestedPrice: { currency: breakfastPkg.currency, price: breakfastPkg.unitPrice, totalPrice: breakfastPkg.totalPrice, }, packageType: PackageTypeEnum.BreakfastAdult, } : false const isCancelled = booking.reservationStatus === BookingStatusEnum.Cancelled const childrenAsString = formatChildBedPreferences({ childrenAges: booking.childrenAges, childBedPreferences: booking.childBedPreferences, }) const childrenInRoom = convertToChildType( booking.childrenAges, booking.childBedPreferences ) const isPrePaid = !!booking.guaranteeInfo?.paymentMethodDescription || booking.rateDefinition.cancellationRule !== CancellationRuleEnum.CancellableBefore6PM 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, 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: featuresPkg.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 ?? "", }, 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, }, }, breakfast, isPrePaid, } }