import { create } from "zustand";
import { persist } from "zustand/middleware";

type Product = {
  id: number;
  title: string;
  href: string;
  price: number;
  imageSrc: string;
  imageAlt: string;
  details: string;
  formData?: Record<string, unknown> | null;
  slotId: number | null;
  centerName: string;
  timeRange: string;
  locale: string;
  language: string;
};

type CartStore = {
  bookingData: Product | null;
  bookingId?: number | null;
  setBookingId: (id?: number | null) => void;
  // currency: string;
  // setCurrency: (currency: string) => void;
  addToCart: (product: Product) => void;
  clearCart: () => void;
  subtotal: number;
  // convertedSubtotal: number;
  // setConvertedSubtotal: (value: number) => void;
};
export const useCartStore = create<CartStore>()(
  persist(
    (set, get) => ({
      bookingData: null,
      bookingId: null,
      setBookingId: (id) => set({ bookingId: id }),
      // currency: "usd",
      // setCurrency: (currency) => set({ currency }),
      addToCart: (product) =>
        set((state) => {
          return {
            ...state,
            // currency: "usd",
            bookingData: product,
            subtotal: product.price,
            // convertedSubtotal: product.price,
          };
        }),
      clearCart: () => set({ bookingData: null }),
      subtotal: get()?.bookingData?.price || 0,
      // convertedSubtotal: 0,
      // setConvertedSubtotal: (value) => set({ convertedSubtotal: value }),
    }),
    {
      name: "cart-storage",
    }
  )
);
