{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "inventory",
  "title": "Inventory",
  "description": "Primitives for inventory: out of stock, low, in stock and overstock from on-hand stock and the reorder point, each as a coloured label, days of cover, a reorder quantity for the lead time plus a target, and a stock bar with a reorder point tick.",
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/status-label.json",
    "https://www.dashboardblocks.com/r/radix/use-reveal.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/inventory.tsx",
      "content": "'use client'\n\nimport {\n  StatusLabel,\n  type StatusLabelProps,\n  type StatusTone,\n} from '@/registry/components/dashboardblocks/status-label'\nimport { useReveal } from '@/registry/hooks/use-reveal'\n\nimport { cn } from '@/lib/utils'\n\ntype StockStatus = 'out' | 'low' | 'ok' | 'over'\n\ninterface StockInput {\n  /** Above this is overstock. */\n  maxStock?: number\n  onHand: number\n  /** At or below this, it's time to reorder. */\n  reorderPoint: number\n}\n\nfunction getStockStatus({ maxStock, onHand, reorderPoint }: StockInput): StockStatus {\n  if (onHand <= 0) return 'out'\n  if (onHand <= reorderPoint) return 'low'\n  if (maxStock !== undefined && onHand > maxStock) return 'over'\n  return 'ok'\n}\n\n/** Days until stock runs out at the current daily demand; `Infinity` with no demand. */\nfunction getDaysOfCover(onHand: number, dailyDemand: number) {\n  return dailyDemand > 0 ? Math.max(0, onHand) / dailyDemand : Infinity\n}\n\ninterface ReorderInput {\n  dailyDemand: number\n  leadTimeDays: number\n  onHand: number\n  onOrder?: number\n  targetDays?: number\n}\n\nfunction getReorderQuantity({\n  dailyDemand,\n  leadTimeDays,\n  onHand,\n  onOrder = 0,\n  targetDays = 30,\n}: ReorderInput) {\n  return Math.max(\n    0,\n    Math.ceil(dailyDemand * (leadTimeDays + targetDays) - onHand - onOrder),\n  )\n}\n\ninterface ProjectionInput {\n  dailyDemand: number\n  days: number\n  /** Orders arriving, by days from today. */\n  incoming?: { day: number; quantity: number }[]\n  onHand: number\n}\n\nfunction projectStock({ dailyDemand, days, incoming = [], onHand }: ProjectionInput) {\n  const levels = [onHand]\n  for (let day = 1; day <= days; day++) {\n    const arriving = incoming\n      .filter((order) => order.day === day)\n      .reduce((sum, order) => sum + order.quantity, 0)\n    levels.push(levels[day - 1] - dailyDemand + arriving)\n  }\n  const stockout = levels.findIndex((level) => level <= 0)\n  return { levels, stockoutDay: stockout === -1 ? null : stockout }\n}\n\nconst stockStatusConfig: Record<\n  StockStatus,\n  { color: string; label: string; tone: StatusTone }\n> = {\n  low: { color: 'var(--color-orange-500)', label: 'Low stock', tone: 'warning' },\n  ok: { color: 'var(--color-emerald-500)', label: 'In stock', tone: 'good' },\n  out: { color: 'var(--color-red-500)', label: 'Out of stock', tone: 'bad' },\n  over: { color: 'var(--color-sky-500)', label: 'Overstock', tone: 'info' },\n}\n\ninterface StockStatusLabelProps extends Omit<StatusLabelProps, 'children' | 'tone'> {\n  status: StockStatus\n}\n\nfunction StockStatusLabel({ status, ...props }: StockStatusLabelProps) {\n  const config = stockStatusConfig[status]\n  return (\n    <StatusLabel tone={config.tone} {...props}>\n      {config.label}\n    </StatusLabel>\n  )\n}\n\ninterface StockBarProps extends StockInput {\n  animated?: boolean\n  className?: string\n  capacity?: number\n}\n\nfunction StockBar({\n  animated = true,\n  capacity,\n  className,\n  maxStock,\n  onHand,\n  reorderPoint,\n}: StockBarProps) {\n  const { ref, revealed } = useReveal<HTMLDivElement>(animated)\n  const status = getStockStatus({ maxStock, onHand, reorderPoint })\n  const top = capacity ?? (Math.max(onHand, reorderPoint, maxStock ?? 0) * 1.1 || 1)\n  const at = (value: number) => `${Math.min(100, (Math.max(0, value) / top) * 100)}%`\n\n  return (\n    <div ref={ref} aria-hidden className={cn('relative h-2 w-full', className)}>\n      <div className='bg-muted h-full w-full overflow-hidden rounded-full'>\n        <div\n          className='h-full rounded-full transition-[width] duration-1000 ease-[cubic-bezier(0.33,1,0.68,1)] motion-reduce:transition-none'\n          style={{\n            backgroundColor: stockStatusConfig[status].color,\n            width: revealed ? at(onHand) : '0%',\n          }}\n        />\n      </div>\n      <span\n        className='bg-foreground ring-background absolute top-1/2 h-[calc(100%+8px)] w-0.5 -translate-x-1/2 -translate-y-1/2 rounded-full ring-2'\n        style={{ left: at(reorderPoint) }}\n      />\n    </div>\n  )\n}\n\nfunction ReorderPointKey({ className }: { className?: string }) {\n  return (\n    <span\n      className={cn(\n        'text-muted-foreground inline-flex items-center gap-1.5 text-xs',\n        className,\n      )}\n    >\n      <span aria-hidden className='bg-foreground h-3 w-0.5 rounded-full' />\n      Reorder point\n    </span>\n  )\n}\n\nexport {\n  ReorderPointKey,\n  StockBar,\n  StockStatusLabel,\n  getDaysOfCover,\n  getReorderQuantity,\n  getStockStatus,\n  projectStock,\n  stockStatusConfig,\n}\n\nexport type {\n  ProjectionInput,\n  ReorderInput,\n  StockBarProps,\n  StockInput,\n  StockStatus,\n  StockStatusLabelProps,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/inventory.tsx"
    }
  ],
  "type": "registry:component"
}
