{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ds-card-stat-gauge",
  "title": "Card Stat Gauge",
  "description": "A statistical card with a gauge visualization for showing metrics within a target range.",
  "dependencies": [
    "class-variance-authority",
    "lucide-react"
  ],
  "registryDependencies": [
    "@lema-ds/card-stat-shared",
    "@lema-ds/card",
    "@lema-ds/skeleton",
    "@lema-ds/badge",
    "@lema-ds/ui-i18n",
    "@lema-ds/ds-locale-provider"
  ],
  "files": [
    {
      "path": "components/ds/ds-card-stat-gauge.tsx",
      "content": "import * as React from \"react\"\nimport { cva } from \"class-variance-authority\"\n\nimport { cn } from \"@/lib/utils\"\nimport { UI_I18N, type UILocale } from \"@/lib/ui-i18n\"\nimport { useOptionalUILocale } from \"@/components/ui/ds-locale-provider\"\nimport { Badge } from \"@/components/ui/badge\"\nimport { Card, CardAction, CardContent, CardHeader } from \"@/components/ui/card\"\nimport { Skeleton } from \"@/components/ui/skeleton\"\n\nimport {\n  type CardStatSize,\n  type FormatOptions,\n  applyFormat,\n  cardStatLabelVariants,\n  cardStatDescriptionVariants,\n  cardStatHeaderIconVariants,\n  cardStatBadgePaddingVariants,\n} from \"@/lib/card-stats-shared\"\n\n// ── Types ──\nexport interface CardStatGaugeZone {\n  label: string\n  color: string\n  max: number\n}\n\nexport interface CardStatGaugeProps extends FormatOptions {\n  label: string\n  value: number\n  min?: number\n  max?: number\n  description?: string\n  zones?: CardStatGaugeZone[]\n  icon?: React.ElementType\n  size?: CardStatSize\n  className?: string\n  loading?: boolean\n  empty?: boolean\n  locale?: UILocale\n}\n\n// ── Variants ──\nexport const cardStatGaugeMaxWVariants = cva(\"\", {\n  variants: {\n    size: {\n      sm: \"max-w-32\",\n      md: \"max-w-40\",\n      lg: \"max-w-48\",\n    },\n  },\n  defaultVariants: { size: \"sm\" },\n})\n\nexport const cardStatGaugeValueVariants = cva(\"\", {\n  variants: {\n    size: {\n      sm: \"text-2xl font-semibold tracking-tight tabular-nums\",\n      md: \"text-3xl font-semibold tracking-tight tabular-nums\",\n      lg: \"text-4xl font-semibold tracking-tight tabular-nums\",\n    },\n  },\n  defaultVariants: { size: \"sm\" },\n})\n\n// ── Helpers ──\nconst DEFAULT_GAUGE_ZONES: CardStatGaugeZone[] = [\n  { label: \"Poor\", color: \"var(--color-risk-1)\", max: 25 },\n  { label: \"Fair\", color: \"var(--color-risk-2)\", max: 50 },\n  { label: \"Good\", color: \"var(--color-risk-3)\", max: 75 },\n  { label: \"Excellent\", color: \"var(--color-risk-4)\", max: 100 },\n]\n\nconst GCX = 50,\n  GCY = 52,\n  GR = 38\n\nfunction gaugePt(p: number): [number, number] {\n  const a = Math.PI * (1 - p)\n  return [GCX + GR * Math.cos(a), GCY - GR * Math.sin(a)]\n}\n\nfunction gaugeArc(p1: number, p2: number): string {\n  const [x1, y1] = gaugePt(p1)\n  const [x2, y2] = gaugePt(p2)\n  return `M ${x1.toFixed(2)} ${y1.toFixed(2)} A ${GR} ${GR} 0 0 1 ${x2.toFixed(2)} ${y2.toFixed(2)}`\n}\n\nfunction GaugeSvg({\n  percent,\n  zones,\n}: {\n  percent: number\n  zones: CardStatGaugeZone[]\n}) {\n  const p = Math.min(100, Math.max(0, percent)) / 100\n  const activeZone =\n    zones.find((z) => percent <= z.max) ?? zones[zones.length - 1]\n  const [dotX, dotY] = gaugePt(p)\n\n  return (\n    <svg viewBox=\"0 0 100 58\" className=\"w-full\" aria-hidden>\n      {zones.map((zone, i) => {\n        const p1 = (i === 0 ? 0 : zones[i - 1].max) / 100\n        const p2 = zone.max / 100\n        return (\n          <path\n            key={zone.label}\n            d={gaugeArc(p1, p2)}\n            fill=\"none\"\n            style={{ stroke: zone.color }}\n            strokeWidth=\"6\"\n            strokeLinecap=\"butt\"\n            opacity=\"0.18\"\n          />\n        )\n      })}\n\n      {p > 0 && (\n        <path\n          d={gaugeArc(0, p)}\n          fill=\"none\"\n          style={{ stroke: activeZone.color }}\n          strokeWidth=\"6\"\n          strokeLinecap=\"round\"\n        />\n      )}\n\n      {p > 0 && (\n        <circle\n          cx={dotX.toFixed(2)}\n          cy={dotY.toFixed(2)}\n          r=\"4.5\"\n          style={{ fill: activeZone.color }}\n          stroke=\"white\"\n          strokeWidth=\"1.5\"\n        />\n      )}\n\n      <circle\n        cx={gaugePt(0)[0].toFixed(2)}\n        cy={gaugePt(0)[1].toFixed(2)}\n        r=\"3\"\n        style={{ fill: \"var(--color-muted-foreground)\" }}\n        opacity=\"0.35\"\n      />\n    </svg>\n  )\n}\n\n// ── Component ──\nexport function CardStatGauge({\n  label,\n  value,\n  min = 0,\n  max = 100,\n  description,\n  zones: zonesProp,\n  icon: Icon,\n  size = \"sm\",\n  className,\n  loading,\n  empty,\n  locale: localeProp,\n  ...fmt\n}: CardStatGaugeProps) {\n  const locale = useOptionalUILocale(localeProp)\n  const zones =\n    zonesProp ??\n    (locale\n      ? [\n          {\n            label: UI_I18N[locale].cardStatGauge.poor,\n            color: \"var(--color-risk-1)\",\n            max: 25,\n          },\n          {\n            label: UI_I18N[locale].cardStatGauge.fair,\n            color: \"var(--color-risk-2)\",\n            max: 50,\n          },\n          {\n            label: UI_I18N[locale].cardStatGauge.good,\n            color: \"var(--color-risk-3)\",\n            max: 75,\n          },\n          {\n            label: UI_I18N[locale].cardStatGauge.excellent,\n            color: \"var(--color-risk-4)\",\n            max: 100,\n          },\n        ]\n      : DEFAULT_GAUGE_ZONES)\n  if (loading) {\n    return (\n      <Card size=\"sm\" className={className} data-slot=\"card-stat-gauge\">\n        <CardHeader className=\"flex flex-row items-center justify-between\">\n          <Skeleton className=\"h-3 w-24 rounded-md\" />\n          <Skeleton\n            className={cn(cardStatHeaderIconVariants({ size }), \"rounded-md\")}\n          />\n        </CardHeader>\n        <CardContent\n          className=\"flex flex-col items-center gap-2 pb-3\"\n          data-slot=\"card-stat-gauge-skeleton\"\n        >\n          <Skeleton\n            className={cn(\"w-full\", cardStatGaugeMaxWVariants({ size }))}\n            style={{\n              aspectRatio: \"2/1\",\n              borderRadius: \"50% 50% 0 0 / 100% 100% 0 0\",\n            }}\n          />\n          <Skeleton className=\"h-8 w-16\" />\n          <Skeleton className=\"h-5 w-20 rounded-full\" />\n          <Skeleton className=\"h-3 w-28 rounded-md\" />\n        </CardContent>\n      </Card>\n    )\n  }\n\n  if (empty) {\n    return (\n      <Card size=\"sm\" className={className} data-slot=\"card-stat-gauge\">\n        <CardHeader className=\"flex flex-row items-center justify-between\">\n          <span\n            className={cn(\n              \"truncate text-muted-foreground\",\n              cardStatLabelVariants({ size })\n            )}\n          >\n            {label}\n          </span>\n          {Icon && (\n            <CardAction className=\"text-muted-foreground\">\n              <Icon\n                className={cardStatHeaderIconVariants({ size })}\n                aria-hidden\n              />\n            </CardAction>\n          )}\n        </CardHeader>\n        <CardContent\n          className=\"flex flex-col items-center gap-1 pb-3\"\n          data-slot=\"card-stat-gauge\"\n        >\n          <div\n            className={cn(\"w-full\", cardStatGaugeMaxWVariants({ size }))}\n            data-slot=\"card-stat-gauge-svg\"\n          >\n            <GaugeSvg percent={0} zones={zones} />\n          </div>\n          <p\n            className={cn(\n              \"-mt-1 text-muted-foreground/25\",\n              cardStatGaugeValueVariants({ size })\n            )}\n          >\n            —\n          </p>\n          <Badge\n            variant=\"outline\"\n            className={cn(\n              \"text-muted-foreground/60\",\n              cardStatBadgePaddingVariants({ size }),\n              cardStatDescriptionVariants({ size })\n            )}\n          >\n            {locale ? UI_I18N[locale].cardStatGauge.noReading : \"No reading\"}\n          </Badge>\n          {description && (\n            <p\n              className={cn(\n                \"mt-1 text-center text-muted-foreground/40\",\n                cardStatDescriptionVariants({ size })\n              )}\n            >\n              {description}\n            </p>\n          )}\n        </CardContent>\n      </Card>\n    )\n  }\n\n  const display = applyFormat(value, { ...fmt, locale })\n  const pct = Math.min(100, Math.max(0, ((value - min) / (max - min)) * 100))\n  const activeZone = zones.find((z) => pct <= z.max) ?? zones[zones.length - 1]\n\n  return (\n    <Card size=\"sm\" className={className} data-slot=\"card-stat-gauge\">\n      <CardHeader className=\"flex flex-row items-center justify-between\">\n        <span\n          className={cn(\n            \"truncate text-muted-foreground\",\n            cardStatLabelVariants({ size })\n          )}\n        >\n          {label}\n        </span>\n        {Icon && (\n          <CardAction className=\"text-muted-foreground\">\n            <Icon className={cardStatHeaderIconVariants({ size })} />\n          </CardAction>\n        )}\n      </CardHeader>\n      <CardContent className=\"flex flex-col items-center gap-1 pb-3\">\n        <div\n          className={cn(\"w-full\", cardStatGaugeMaxWVariants({ size }))}\n          data-slot=\"card-stat-gauge-svg\"\n        >\n          <GaugeSvg percent={pct} zones={zones} />\n        </div>\n        <p className={cn(\"-mt-1\", cardStatGaugeValueVariants({ size }))}>\n          {display}\n        </p>\n        <span\n          className={cn(\n            \"rounded-full font-semibold text-primary-foreground\",\n            cardStatBadgePaddingVariants({ size }),\n            cardStatDescriptionVariants({ size })\n          )}\n          style={{ backgroundColor: activeZone.color, color: \"white\" }}\n          aria-hidden\n        >\n          {activeZone.label}\n        </span>\n        {description && (\n          <p\n            className={cn(\n              \"mt-1 text-center text-muted-foreground\",\n              cardStatDescriptionVariants({ size })\n            )}\n          >\n            {description}\n          </p>\n        )}\n      </CardContent>\n    </Card>\n  )\n}\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}
