import { create } from "zustand" import type { BreakfastPackage } from "@/types/components/hotelReservation/breakfast" import type { BedTypeSchema } from "@/types/components/hotelReservation/enterDetails/bedType" import type { RoomPrice } from "@/types/components/hotelReservation/enterDetails/details" import type { PriceType } from "@/types/components/hotelReservation/myStay/myStay" import type { Child } from "@/types/components/hotelReservation/selectRate/selectRate" import { CurrencyEnum } from "@/types/enums/currency" import type { Packages } from "@/types/requests/packages" import type { BookingConfirmation } from "@/types/trpc/routers/booking/confirmation" export type Room = Pick< BookingConfirmation["booking"], | "hotelId" | "adults" | "checkInDate" | "checkOutDate" | "childrenAges" | "createDateTime" | "rateDefinition" | "guaranteeInfo" | "linkedReservations" | "confirmationNumber" | "cancellationNumber" | "bookingCode" | "cheques" | "vouchers" | "isCancelable" | "multiRoom" | "canChangeDate" | "guest" | "roomTypeCode" | "currencyCode" | "vatPercentage" | "roomPoints" > & { roomName: string roomNumber: number | null isCancelled: boolean childrenInRoom: Child[] childrenAsString: string terms: string | null packages: Packages | null bedType: BedTypeSchema roomPrice: RoomPrice breakfast: BreakfastPackage | null mainRoom: boolean isPrePaid: boolean priceType: PriceType } interface MyStayRoomDetailsState { bookedRoom: Room linkedReservationRooms: Room[] actions: { addBookedRoom: (room: Room) => void updateBookedRoom: (room: Room) => void addLinkedReservationRoom: (room: Room) => void updateLinkedReservationRoom: (room: Room) => void } } export const useMyStayRoomDetailsStore = create( (set) => ({ bookedRoom: { hotelId: "", roomTypeCode: "", adults: 0, childrenAges: [], checkInDate: new Date(), checkOutDate: new Date(), confirmationNumber: "", cancellationNumber: null, bookingCode: null, cheques: 0, vouchers: 0, currencyCode: CurrencyEnum.Unknown, guest: { email: "", firstName: "", lastName: "", membershipNumber: "", phoneNumber: "", countryCode: "", }, rateDefinition: { breakfastIncluded: false, cancellationRule: null, cancellationText: null, generalTerms: [], isMemberRate: false, mustBeGuaranteed: false, rateCode: "", title: null, }, roomPoints: 0, roomPrice: { perNight: { requested: { price: 0, currency: CurrencyEnum.Unknown, }, local: { price: 0, currency: CurrencyEnum.Unknown, }, }, perStay: { requested: { price: 0, currency: CurrencyEnum.Unknown, }, local: { price: 0, currency: CurrencyEnum.Unknown, }, }, }, vatPercentage: 0, vatAmount: 0, totalPriceExVat: 0, createDateTime: new Date(), canChangeDate: false, multiRoom: false, mainRoom: false, roomName: "", roomNumber: null, isCancelled: false, childrenInRoom: [], childrenAsString: "", terms: null, packages: null, bedType: { description: "", roomTypeCode: "", }, breakfast: null, linkedReservations: [], isCancelable: false, isPrePaid: false, priceType: "money", }, linkedReservationRooms: [], actions: { addBookedRoom: (room) => { set({ bookedRoom: room }) }, updateBookedRoom: (room) => { set({ bookedRoom: room }) }, addLinkedReservationRoom: (room) => { set((state) => { // Check if room exists in bookedRooms const existsInBookedRoom = state.bookedRoom.confirmationNumber === room.confirmationNumber if (existsInBookedRoom) { return state } // Check if room with this ID already exists in linkedReservationRooms const existingIndex = state.linkedReservationRooms.findIndex( (r) => r.confirmationNumber === room.confirmationNumber ) let newRooms = [...state.linkedReservationRooms] if (existingIndex >= 0) { // Update existing room newRooms[existingIndex] = room } else { // Add new room newRooms.push(room) } return { linkedReservationRooms: newRooms, } }) }, updateLinkedReservationRoom: (room) => { set((state) => { const existingIndex = state.linkedReservationRooms.findIndex( (r) => r.confirmationNumber === room.confirmationNumber ) let newRooms = [...state.linkedReservationRooms] if (existingIndex >= 0) { newRooms[existingIndex] = room } return { linkedReservationRooms: newRooms, } }) }, }, }) )