import { create } from "zustand" import { CurrencyEnum } from "@/types/enums/currency" interface RoomPrice { id: string totalPrice: number currencyCode: CurrencyEnum isMainBooking?: boolean roomPoints: number } interface MyStayTotalPriceState { rooms: RoomPrice[] totalPrice: number | null currencyCode: CurrencyEnum totalPoints: number actions: { // Add a single room price addRoomPrice: (room: RoomPrice) => void } } export const useMyStayTotalPriceStore = create( (set) => ({ rooms: [], totalPrice: null, totalPoints: 0, totalCheques: 0, totalVouchers: 0, currencyCode: CurrencyEnum.Unknown, actions: { addRoomPrice: (room) => { set((state) => { // Check if room with this ID already exists const existingIndex = state.rooms.findIndex((r) => r.id === room.id) let newRooms = [...state.rooms] if (existingIndex >= 0) { // Update existing room newRooms[existingIndex] = room } else { // Add new room newRooms.push(room) } // Get currency from main booking or first room const mainRoom = newRooms.find((r) => r.isMainBooking) || newRooms[0] const currencyCode = mainRoom?.currencyCode ?? CurrencyEnum.Unknown // Calculate total (only same currency for now) const total = newRooms.reduce((sum, r) => { if (r.currencyCode === currencyCode) { return sum + r.totalPrice } return sum }, 0) const totalPoints = newRooms.reduce((sum, r) => { return sum + r.roomPoints }, 0) return { rooms: newRooms, totalPrice: total, currencyCode, totalPoints, } }) }, }, }) )