import { create } from "zustand" interface RoomPrice { id: string totalPrice: number currencyCode: string isMainBooking?: boolean } interface MyStayTotalPriceState { rooms: RoomPrice[] totalPrice: number currencyCode: string // Add a single room price addRoomPrice: (room: RoomPrice) => void // Get the calculated total getTotalPrice: () => number } export const useMyStayTotalPriceStore = create( (set, get) => ({ rooms: [], totalPrice: 0, currencyCode: "", 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 || "" // Calculate total (only same currency for now) const total = newRooms.reduce((sum, r) => { if (r.currencyCode === currencyCode) { return sum + r.totalPrice } return sum }, 0) return { rooms: newRooms, totalPrice: total, currencyCode, } }) }, getTotalPrice: () => { return get().totalPrice }, }) )