{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"weeks-of-cover","title":"Weeks of cover","description":"Weeks of stock on hand at the current rate of sale, on a track against the healthy band: short (stockout risk), in band, or over (aged cash).","registryDependencies":["https://ui.twelfth.ai/r/market-side.json","https://ui.twelfth.ai/r/retail-format.json"],"files":[{"path":"src/registry/catman/ui/weeks-of-cover.tsx","content":"import { cn } from \"@/lib/utils\"\nimport { formatWeeks } from \"@/registry/catman/lib/retail-format\"\nimport type { MarketSide } from \"@/registry/catman/ui/market-side\"\n\n/**\n * How many weeks the stock on hand lasts at the current rate of sale, against\n * the band the buyer wants it in. Under the band is a stockout risk, over it is\n * cash sat on a shelf; both are exposed, and the words say which.\n *\n * `weeks` is saleable units ÷ weekly rate of sale, or null when nothing sold\n * (cover is unbounded, which is its own kind of problem).\n *\n * Part of the twelfth catman registry (`@twelfth/weeks-of-cover`).\n */\nexport type CoverState = \"short\" | \"healthy\" | \"over\" | \"no-sales\"\n\nexport function coverState(weeks: number | null, { min = 0, max }: { min?: number; max: number }): CoverState {\n  if (weeks === null || !Number.isFinite(weeks)) return \"no-sales\"\n  if (weeks < min) return \"short\"\n  if (weeks > max) return \"over\"\n  return \"healthy\"\n}\n\nconst SIDE: Record<CoverState, MarketSide> = { short: \"exposed\", healthy: \"favourable\", over: \"exposed\", \"no-sales\": \"unknown\" }\n\nconst MARKER: Record<MarketSide, string> = {\n  favourable: \"bg-favourable-foreground\",\n  exposed: \"bg-exposed-foreground\",\n  level: \"bg-foreground\",\n  unknown: \"bg-muted-foreground\",\n}\n\nfunction stateLabel(state: CoverState, weeks: number | null, min: number, max: number): string {\n  if (state === \"no-sales\" || weeks === null) return \"No net sales\"\n  if (state === \"short\") return `${formatWeeks(min - weeks)} short`\n  if (state === \"over\") return `${formatWeeks(weeks - max)} over`\n  return \"In band\"\n}\n\nexport function WeeksOfCover({\n  weeks,\n  min = 0,\n  max,\n  scale,\n  showState = true,\n  className,\n}: {\n  weeks: number | null\n  /** Bottom of the healthy band, e.g. lead time plus safety stock. */\n  min?: number\n  /** Top of the healthy band; beyond it the stock is aged cash. */\n  max: number\n  /** Weeks the track spans. Defaults to half again past the band. */\n  scale?: number\n  /** Say \"4 wks over\" beside the figure. Off for a dense table cell. */\n  showState?: boolean\n  className?: string\n}) {\n  const state = coverState(weeks, { min, max })\n  const side = SIDE[state]\n  const span = Math.max(scale ?? max * 1.5, max)\n  const at = (value: number) => `${(Math.min(Math.max(value, 0), span) / span) * 100}%`\n  const clipped = weeks !== null && weeks > span\n  const label = stateLabel(state, weeks, min, max)\n  return (\n    <span className={cn(\"inline-flex items-center gap-2 text-xs tabular-nums\", className)}>\n      <span\n        role=\"meter\"\n        aria-label={weeks === null ? \"Weeks of cover: no net sales\" : `Weeks of cover: ${formatWeeks(weeks)}, band ${min} to ${max} weeks, ${label}`}\n        aria-valuemin={0}\n        aria-valuemax={span}\n        aria-valuenow={weeks === null ? undefined : Math.min(weeks, span)}\n        className=\"relative h-1.5 w-20 shrink-0 rounded-full bg-muted\"\n      >\n        <span aria-hidden className=\"absolute inset-y-0 rounded-full bg-favourable\" style={{ left: at(min), width: `calc(${at(max)} - ${at(min)})` }} />\n        {weeks !== null ? (\n          <span\n            aria-hidden\n            className={cn(\"absolute top-1/2 size-2.5 -translate-x-1/2 -translate-y-1/2 rounded-full ring-2 ring-background\", MARKER[side])}\n            style={{ left: at(weeks) }}\n          />\n        ) : null}\n      </span>\n      {weeks === null ? <span className=\"text-muted-foreground\">No net sales</span> : <span className=\"font-mono text-foreground\">{`${clipped ? \"›\" : \"\"}${formatWeeks(weeks)}`}</span>}\n      {showState && weeks !== null ? <span className={cn(state === \"healthy\" ? \"text-favourable-foreground\" : \"text-exposed-foreground\")}>{label}</span> : null}\n    </span>\n  )\n}\n","type":"registry:ui"}],"type":"registry:ui"}