import { create } from 'zustand'

interface Product {
  id: number
  name: string
  price: number
  discountedPrice: number | null
  images: { uri: string }[]
  unit: string
  unitValue: number
  storeId: number
  category: string
  isActive: boolean
  description: string | null
}

interface CentralProductStore {
  products: Product[]
  productsById: Record<number, Product>
  setProducts: (products: Product[]) => void
  refetchProducts: (() => void) | null
  setRefetchProducts: (fn: (() => void) | null) => void
}

export const useCentralProductStore = create<CentralProductStore>((set) => ({
  products: [],
  productsById: {},
  setProducts: (products) => {
    const productsById: Record<number, Product> = {}
    products.forEach((p) => {
      productsById[p.id] = p
    })
    set({ products, productsById })
  },
  refetchProducts: null,
  setRefetchProducts: (fn) => set({ refetchProducts: fn }),
}))
