import type { Price } from "@/types/components/hotelReservation/price" import { type RoomPackage, RoomPackageCodeEnum, } from "@/types/components/hotelReservation/selectRate/roomFilter" import type { Rate } from "@/types/components/hotelReservation/selectRate/selectRate" export const calculateTotalPrice = ( selectedRateSummary: Rate[], isUserLoggedIn: boolean, petRoomPackage: RoomPackage | undefined ) => { return selectedRateSummary.reduce( (total, room, idx) => { const rate = isUserLoggedIn && room.member && idx + 1 === 1 ? room.member : room.public if (!rate) { return total } const isPetRoom = room.features.find( (feature) => feature.code === RoomPackageCodeEnum.PET_ROOM ) let petRoomPrice = 0 if ( petRoomPackage && isPetRoom && room.package === RoomPackageCodeEnum.PET_ROOM ) { petRoomPrice = Number(petRoomPackage.localPrice.totalPrice) } const regularPrice = rate.localPrice.regularPricePerStay ? (total.local.regularPrice || 0) + (rate.localPrice.regularPricePerStay || 0) : undefined return { local: { currency: rate.localPrice.currency, price: total.local.price + rate.localPrice.pricePerStay + petRoomPrice, regularPrice, }, requested: rate.requestedPrice ? { currency: rate.requestedPrice.currency, price: (total.requested?.price ?? 0) + rate.requestedPrice.pricePerStay + petRoomPrice, } : undefined, } }, { local: { currency: (selectedRateSummary[0].public?.localPrice.currency || selectedRateSummary[0].member?.localPrice.currency)!, price: 0, regularPrice: undefined, }, requested: undefined, } ) }