/**
 * ANNOTATED COPY — every line explained
 * Source: src/store/cart.ts
 * NOT used by the app — read this to learn how the real file works
 */
// Zustand — simple global state store (cart, auth, locale)
import { create } from 'zustand';
// Zustand — simple global state store (cart, auth, locale)
import { persist } from 'zustand/middleware';
// Import project module (@/ alias = src/ folder in tsconfig)
import { CartItem, Product, ProductCustomization } from '@/types';
// Import project module (@/ alias = src/ folder in tsconfig)
import { getCartItemKey } from '@/lib/customization';
// (blank line — separates logical blocks for readability)
// TypeScript interface — contract for object properties and methods
interface CartState {
  // Line 7: items: CartItem[];
  items: CartItem[];
  // Line 8: addItem: (
  addItem: (
    // Property or array item — trailing comma allowed in TypeScript
    product: Product,
    // Property or array item — trailing comma allowed in TypeScript
    customization: ProductCustomization,
    // Property or array item — trailing comma allowed in TypeScript
    customizationLabel: string,
    // Line 12: quantity?: number
    quantity?: number
  // Line 13: ) => void;
  ) => void;
  // Line 14: removeItem: (id: string) => void;
  removeItem: (id: string) => void;
  // Line 15: updateQuantity: (id: string, quantity: number) => void;
  updateQuantity: (id: string, quantity: number) => void;
  // Line 16: clearCart: () => void;
  clearCart: () => void;
  // Line 17: getTotal: () => number;
  getTotal: () => number;
  // Line 18: getItemCount: () => number;
  getItemCount: () => number;
// Closing brace — end of block (function, if, object, JSX)
}
// (blank line — separates logical blocks for readability)
// Named export constant — shared config/data imported elsewhere
export const useCartStore = create<CartState>()(
  // Zustand persist — save store to localStorage between visits
  persist(
    // Line 23: (set, get) => ({
    (set, get) => ({
      // Property or array item — trailing comma allowed in TypeScript
      items: [],
// (blank line — separates logical blocks for readability)
      // Line 26: addItem: (product, customization, customizationLabel, qua...
      addItem: (product, customization, customizationLabel, quantity = 1) => {
        // Line 27: const id = getCartItemKey(product.id, customization);
        const id = getCartItemKey(product.id, customization);
        // Line 28: const label = customizationLabel;
        const label = customizationLabel;
        // Array.find — get first matching item or undefined
        const existing = get().items.find((item) => item.id === id);
// (blank line — separates logical blocks for readability)
        // Conditional branch — different behavior based on runtime value
        if (existing) {
          // Line 32: set({
          set({
            // Array.map — transform each item (often render a list of components)
            items: get().items.map((item) =>
              // Line 34: item.id === id
              item.id === id
                // Line 35: ? { ...item, quantity: item.quantity + quantity }
                ? { ...item, quantity: item.quantity + quantity }
                // Line 36: : item
                : item
            // Property or array item — trailing comma allowed in TypeScript
            ),
          // Closing brace — end of block (function, if, object, JSX)
          });
        // Closing brace — end of block (function, if, object, JSX)
        } else {
          // Line 40: set({
          set({
            // Line 41: items: [
            items: [
              // Property or array item — trailing comma allowed in TypeScript
              ...get().items,
              // Line 43: {
              {
                // Property or array item — trailing comma allowed in TypeScript
                id,
                // Property or array item — trailing comma allowed in TypeScript
                product,
                // Property or array item — trailing comma allowed in TypeScript
                quantity,
                // Property or array item — trailing comma allowed in TypeScript
                customization,
                // Property or array item — trailing comma allowed in TypeScript
                customizationLabel: label,
              // Closing brace — end of block (function, if, object, JSX)
              },
            // End of array literal
            ],
          // Closing brace — end of block (function, if, object, JSX)
          });
        // Closing brace — end of block (function, if, object, JSX)
        }
      // Closing brace — end of block (function, if, object, JSX)
      },
// (blank line — separates logical blocks for readability)
      // Line 55: removeItem: (id) => {
      removeItem: (id) => {
        // Array.filter — keep items matching condition (search, category)
        set({ items: get().items.filter((item) => item.id !== id) });
      // Closing brace — end of block (function, if, object, JSX)
      },
// (blank line — separates logical blocks for readability)
      // Line 59: updateQuantity: (id, quantity) => {
      updateQuantity: (id, quantity) => {
        // Conditional branch — different behavior based on runtime value
        if (quantity <= 0) {
          // Line 61: get().removeItem(id);
          get().removeItem(id);
          // Line 62: return;
          return;
        // Closing brace — end of block (function, if, object, JSX)
        }
        // Line 64: set({
        set({
          // Array.map — transform each item (often render a list of components)
          items: get().items.map((item) =>
            // Line 66: item.id === id ? { ...item, quantity } : item
            item.id === id ? { ...item, quantity } : item
          // Property or array item — trailing comma allowed in TypeScript
          ),
        // Closing brace — end of block (function, if, object, JSX)
        });
      // Closing brace — end of block (function, if, object, JSX)
      },
// (blank line — separates logical blocks for readability)
      // Property or array item — trailing comma allowed in TypeScript
      clearCart: () => set({ items: [] }),
// (blank line — separates logical blocks for readability)
      // Line 73: getTotal: () =>
      getTotal: () =>
        // Array.reduce — accumulate single value (cart total, item count)
        get().items.reduce(
          // Property or array item — trailing comma allowed in TypeScript
          (sum, item) => sum + item.product.price * item.quantity,
          // Line 76: 0
          0
        // Property or array item — trailing comma allowed in TypeScript
        ),
// (blank line — separates logical blocks for readability)
      // Line 79: getItemCount: () =>
      getItemCount: () =>
        // Array.reduce — accumulate single value (cart total, item count)
        get().items.reduce((sum, item) => sum + item.quantity, 0),
    // Closing brace — end of block (function, if, object, JSX)
    }),
    // Line 82: { name: 'kott-gard-cart' }
    { name: 'kott-gard-cart' }
  // Line 83: )
  )
// Line 84: );
);
