import { useCentralSlotStore } from '../lib/stores/central-slot-store'
import { useMemo } from 'react'

export function useProductSlotIdentifier() {
  const slots = useCentralSlotStore((state) => state.slots)
  const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap)

  const getQuickestSlot = useMemo(() => {
    return (productId: number): number | null => {
      const productSlotInfo = productSlotsMap[productId]
      if (!productSlotInfo || productSlotInfo.isOutOfStock) {
        return null
      }

      const availableSlots = productSlotInfo.slots || []
      if (availableSlots.length === 0) {
        return null
      }

      // Return the first (quickest) slot
      return availableSlots[0].id
    }
  }, [productSlotsMap])

  return {
    getQuickestSlot,
    slots,
    productSlotsMap,
  }
}
