{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ds-radial-chart",
  "title": "Radial Chart",
  "description": "A responsive radial bar chart with concentric rings, single-metric gauge mode, and legend support.",
  "dependencies": [
    "recharts",
    "class-variance-authority",
    "lucide-react"
  ],
  "registryDependencies": [
    "@lema-ds/skeleton",
    "@lema-ds/ui-i18n",
    "@lema-ds/format-utils",
    "@lema-ds/ds-locale-provider"
  ],
  "files": [
    {
      "path": "components/ds/ds-radial-chart.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport {\n  Legend,\n  PolarAngleAxis,\n  RadialBar,\n  RadialBarChart as RechartsRadialBarChart,\n  ResponsiveContainer,\n  Tooltip,\n} from \"recharts\"\nimport { cva } from \"class-variance-authority\"\nimport { Gauge } from \"lucide-react\"\nimport { cn } from \"@/lib/utils\"\nimport { Skeleton } from \"@/components/ui/skeleton\"\nimport { UI_I18N, type UILocale } from \"@/lib/ui-i18n\"\nimport { useUILocale } from \"@/components/ui/ds-locale-provider\"\nimport { formatChartValue, type FormatPreset } from \"@/lib/format-utils\"\n\n// ── Types ──────────────────────────────────────────────────────────────────\n\nexport type LegendPosition = \"top\" | \"bottom\" | \"left\" | \"right\"\n\nexport interface RadialChartItem {\n  name: string\n  value: number\n  /** Overrides the auto --chart-N color for this bar */\n  color?: string\n}\n\nexport interface RadialChartProps extends React.HTMLAttributes<HTMLDivElement> {\n  data: RadialChartItem[]\n  title?: string\n  subtitle?: string\n  footer?: React.ReactNode\n  height?: number\n  /**\n   * Show a faint full-arc track behind each bar so the max extent is visible.\n   * @default true\n   */\n  showTrack?: boolean\n  showLegend?: boolean\n  /** @default \"bottom\" */\n  legendPosition?: LegendPosition\n  showTooltip?: boolean\n  /**\n   * Text label rendered at the center of the chart.\n   * When a single item is provided the item's value is shown above the label.\n   */\n  innerLabel?: string\n  /**\n   * Clamps the angular domain so all bars share the same maximum.\n   * Ideal for percentage charts — pass `100` to keep all bars on a 0–100 scale.\n   */\n  maxValue?: number\n  /** Start angle in degrees. 90 = top, 180 = left. @default 90 */\n  startAngle?: number\n  /** End angle in degrees. -270 = full circle CW, 0 = right. @default -270 */\n  endAngle?: number\n  /** Format bar values in the tooltip and center label */\n  valueFormatter?: (value: number) => string\n  format?: FormatPreset\n  decimals?: number\n  currency?: string\n  abbreviate?: boolean\n  /** Show animated skeleton in place of the chart while data loads */\n  loading?: boolean\n  locale?: UILocale\n}\n\n// ── Constants ──────────────────────────────────────────────────────────────\n\nconst CHART_COLORS = [\n  \"var(--chart-1)\",\n  \"var(--chart-2)\",\n  \"var(--chart-3)\",\n  \"var(--chart-4)\",\n  \"var(--chart-5)\",\n]\n\nconst LEGEND_LAYOUT: Record<\n  LegendPosition,\n  {\n    verticalAlign: \"top\" | \"middle\" | \"bottom\"\n    align: \"left\" | \"center\" | \"right\"\n    layout: \"horizontal\" | \"vertical\"\n  }\n> = {\n  top: { verticalAlign: \"top\", align: \"center\", layout: \"horizontal\" },\n  bottom: { verticalAlign: \"bottom\", align: \"center\", layout: \"horizontal\" },\n  left: { verticalAlign: \"middle\", align: \"left\", layout: \"vertical\" },\n  right: { verticalAlign: \"middle\", align: \"right\", layout: \"vertical\" },\n}\n\n// ── Skeleton ───────────────────────────────────────────────────────────────\n\nfunction radialArcPath(\n  cx: number,\n  cy: number,\n  r: number,\n  fraction: number\n): string {\n  const startAngle = -Math.PI / 2 // top\n  const endAngle = startAngle + fraction * 2 * Math.PI\n  const x1 = cx + r * Math.cos(startAngle)\n  const y1 = cy + r * Math.sin(startAngle)\n  const x2 = cx + r * Math.cos(endAngle)\n  const y2 = cy + r * Math.sin(endAngle)\n  const largeArc = fraction > 0.5 ? 1 : 0\n  return `M ${x1} ${y1} A ${r} ${r} 0 ${largeArc} 1 ${x2} ${y2}`\n}\n\ninterface RadialChartSkeletonProps {\n  height?: number\n  hasTitle?: boolean\n  hasSubtitle?: boolean\n  hasFooter?: boolean\n  hasInnerLabel?: boolean\n  className?: string\n}\n\nfunction RadialChartSkeleton({\n  height = 320,\n  hasTitle = false,\n  hasSubtitle = false,\n  hasFooter = false,\n  hasInnerLabel = false,\n  className,\n}: RadialChartSkeletonProps) {\n  const arcs = [\n    { r: 42, fraction: 0.75, sw: 7 },\n    { r: 32, fraction: 0.55, sw: 6 },\n    { r: 22, fraction: 0.85, sw: 5 },\n    { r: 12, fraction: 0.4, sw: 4 },\n  ]\n\n  return (\n    <div\n      className={cn(\"flex w-full flex-col\", className)}\n      data-slot=\"radial-chart-skeleton\"\n    >\n      {(hasTitle || hasSubtitle) && (\n        <div className=\"flex flex-col gap-1.5 px-1 pb-4\">\n          {hasTitle && (\n            <Skeleton\n              className=\"h-3.5 w-44 rounded-md\"\n              style={{ animationDelay: \"0s\" }}\n            />\n          )}\n          {hasSubtitle && (\n            <Skeleton\n              className=\"mt-0.5 h-2.5 w-28 rounded-md\"\n              style={{ animationDelay: \"0.1s\" }}\n            />\n          )}\n        </div>\n      )}\n\n      <div\n        className=\"relative flex items-center justify-center\"\n        style={{ height }}\n      >\n        <svg\n          viewBox=\"0 0 100 100\"\n          width={Math.min(height, 260)}\n          height={Math.min(height, 260)}\n        >\n          {arcs.map(({ r, fraction, sw }, i) => (\n            <g key={i}>\n              {/* Track (full circle) */}\n              <circle\n                cx=\"50\"\n                cy=\"50\"\n                r={r}\n                fill=\"none\"\n                stroke=\"var(--muted)\"\n                strokeWidth={sw}\n                opacity={0.2}\n              />\n              {/* Filled arc */}\n              <path\n                d={radialArcPath(50, 50, r, fraction)}\n                fill=\"none\"\n                stroke=\"var(--muted)\"\n                strokeWidth={sw}\n                strokeLinecap=\"round\"\n                className=\"motion-safe:animate-pulse\"\n                style={{ animationDelay: `${i * 0.15}s` }}\n              />\n            </g>\n          ))}\n          {hasInnerLabel && (\n            <circle cx=\"50\" cy=\"50\" r=\"6\" fill=\"var(--muted)\" opacity={0.4} />\n          )}\n        </svg>\n      </div>\n\n      {hasFooter && (\n        <div className=\"mt-4 flex items-center gap-2 border-t border-border px-1 pt-3\">\n          <Skeleton\n            className=\"h-3.5 w-3.5 shrink-0 rounded-full\"\n            style={{ animationDelay: \"0.7s\" }}\n          />\n          <Skeleton\n            className=\"h-2.5 w-36 rounded-md\"\n            style={{ animationDelay: \"0.8s\" }}\n          />\n          <Skeleton\n            className=\"ml-auto h-2.5 w-20 rounded-md\"\n            style={{ animationDelay: \"0.9s\" }}\n          />\n        </div>\n      )}\n    </div>\n  )\n}\n\n// ── Variants ───────────────────────────────────────────────────────────────\n\nconst chartWrapperVariants = cva(\"flex w-full flex-col\")\n\nconst chartHeaderVariants = cva(\"flex flex-col px-1 pb-4\")\n\nconst chartTitleVariants = cva(\n  \"text-sm leading-tight font-semibold text-foreground\"\n)\n\nconst chartSubtitleVariants = cva(\"mt-0.5 text-xs text-muted-foreground\")\n\nconst chartFooterVariants = cva(\n  \"mt-4 flex items-center gap-2 border-t border-border px-1 pt-3 text-xs text-muted-foreground\"\n)\n\n// ── Custom Tooltip ─────────────────────────────────────────────────────────\n\ninterface TooltipEntry {\n  name?: string\n  value?: number\n  fill?: string\n  payload?: Record<string, unknown>\n}\n\nfunction ChartTooltip({\n  active,\n  payload,\n  fmt,\n}: {\n  active?: boolean\n  payload?: TooltipEntry[]\n  fmt: (v: number) => string\n}) {\n  if (!active || !payload?.length) return null\n\n  const entry = payload[0]\n  const name = entry.name ?? (entry.payload?.name as string | undefined) ?? \"\"\n  const value = entry.value ?? (entry.payload?.value as number | undefined)\n  const color =\n    entry.fill ??\n    (entry.payload?.fill as string | undefined) ??\n    \"var(--chart-1)\"\n\n  return (\n    <div className=\"min-w-36 rounded-lg border border-border bg-card px-3 py-2 shadow-md\">\n      <p className=\"mb-1.5 text-xs font-semibold text-foreground\">{name}</p>\n      {value !== undefined && (\n        <div className=\"flex items-center gap-2\">\n          <span\n            className=\"inline-block size-2 shrink-0 rounded-full\"\n            style={{ backgroundColor: color }}\n          />\n          <span className=\"ml-auto text-xs font-semibold text-foreground tabular-nums\">\n            {fmt(value)}\n          </span>\n        </div>\n      )}\n    </div>\n  )\n}\n\n// ── Custom Legend ──────────────────────────────────────────────────────────\n\ninterface LegendItem {\n  name: string\n  color: string\n}\n\nfunction ChartLegend({\n  items,\n  hiddenItems,\n  onToggle,\n  vertical = false,\n}: {\n  items: LegendItem[]\n  hiddenItems: Set<string>\n  onToggle: (name: string) => void\n  vertical?: boolean\n}) {\n  if (!items.length) return null\n\n  return (\n    <div\n      className={cn(\n        \"flex gap-y-1\",\n        vertical\n          ? \"flex-col px-2 py-1\"\n          : \"flex-wrap items-center justify-center gap-x-4 pt-3\"\n      )}\n    >\n      {items.map((item) => {\n        const hidden = hiddenItems.has(item.name)\n        return (\n          <div\n            key={item.name}\n            role=\"button\"\n            tabIndex={0}\n            aria-pressed={hidden}\n            aria-label={item.name}\n            onClick={() => onToggle(item.name)}\n            onKeyDown={(e) =>\n              (e.key === \"Enter\" || e.key === \" \") && onToggle(item.name)\n            }\n            className={cn(\n              \"flex cursor-pointer items-center gap-1.5 rounded px-1 py-0.5 select-none\",\n              \"transition-opacity focus-visible:ring-1 focus-visible:ring-ring focus-visible:outline-none\",\n              hidden ? \"opacity-40\" : \"hover:opacity-70\"\n            )}\n          >\n            <span\n              className=\"inline-block size-2 shrink-0 rounded-full\"\n              style={{ backgroundColor: item.color }}\n            />\n            <span className=\"text-xs text-muted-foreground\">{item.name}</span>\n          </div>\n        )\n      })}\n    </div>\n  )\n}\n\n// ── Helpers ────────────────────────────────────────────────────────────────\n\nfunction normalizeItems(\n  raw: RadialChartItem[]\n): (RadialChartItem & { fill: string })[] {\n  return raw.map((item, i) => ({\n    ...item,\n    fill: item.color ?? CHART_COLORS[i % CHART_COLORS.length],\n  }))\n}\n\n// ── RadialChart ────────────────────────────────────────────────────────────\n\nexport function RadialChart({\n  data,\n  title,\n  subtitle,\n  footer,\n  height = 320,\n  showTrack = true,\n  showLegend = false,\n  legendPosition = \"bottom\",\n  showTooltip = true,\n  innerLabel,\n  maxValue,\n  startAngle = 90,\n  endAngle = -270,\n  valueFormatter,\n  format,\n  decimals,\n  currency,\n  abbreviate,\n  loading = false,\n  locale: localeProp,\n  className,\n  ...props\n}: RadialChartProps) {\n  const locale = useUILocale(localeProp)\n  // Hooks must be called unconditionally before any early returns\n  const fmt = React.useCallback(\n    (v: number) =>\n      formatChartValue(v, {\n        format,\n        decimals,\n        locale,\n        currency,\n        abbreviate,\n        valueFormatter,\n      }),\n    [valueFormatter, format, decimals, locale, currency, abbreviate]\n  )\n\n  const items = React.useMemo(() => normalizeItems(data), [data])\n\n  const [hiddenItems, setHiddenItems] = React.useState<Set<string>>(new Set())\n  const toggleItem = React.useCallback((name: string) => {\n    setHiddenItems((prev) => {\n      const next = new Set(prev)\n      if (next.has(name)) next.delete(name)\n      else next.add(name)\n      return next\n    })\n  }, [])\n\n  const visibleItems = React.useMemo(\n    () => items.filter((d) => !hiddenItems.has(d.name)),\n    [items, hiddenItems]\n  )\n\n  const legendItems: LegendItem[] = items.map((d) => ({\n    name: d.name,\n    color: d.fill,\n  }))\n\n  if (loading) {\n    return (\n      <RadialChartSkeleton\n        height={height}\n        hasTitle={!!title}\n        hasSubtitle={!!subtitle}\n        hasFooter={!!footer}\n        hasInnerLabel={!!innerLabel}\n        className={className}\n      />\n    )\n  }\n\n  if (data.length === 0) {\n    return (\n      <div\n        className={cn(chartWrapperVariants(), className)}\n        data-slot=\"radial-chart\"\n        {...props}\n      >\n        {(title || subtitle) && (\n          <div\n            className={chartHeaderVariants()}\n            data-slot=\"radial-chart-header\"\n          >\n            {title && <p className={chartTitleVariants()}>{title}</p>}\n            {subtitle && <p className={chartSubtitleVariants()}>{subtitle}</p>}\n          </div>\n        )}\n        <div\n          className=\"flex flex-col items-center justify-center gap-3 rounded-lg border border-dashed border-border text-muted-foreground\"\n          data-slot=\"radial-chart-empty\"\n          style={{ height }}\n        >\n          <div className=\"flex size-10 items-center justify-center rounded-full bg-muted\">\n            <Gauge className=\"size-5\" />\n          </div>\n          <div className=\"text-center\">\n            <p className=\"text-sm font-medium text-foreground\">\n              {UI_I18N[locale].emptyState.noData}\n            </p>\n            <p className=\"mt-0.5 text-xs\">\n              {UI_I18N[locale].emptyState.dataWillAppear}\n            </p>\n          </div>\n        </div>\n        {footer && (\n          <div\n            className={chartFooterVariants()}\n            data-slot=\"radial-chart-footer\"\n          >\n            {footer}\n          </div>\n        )}\n      </div>\n    )\n  }\n\n  // Center label: show formatted value only for single-item charts\n  const centerValue =\n    innerLabel && visibleItems.length === 1\n      ? fmt(visibleItems[0].value)\n      : undefined\n\n  const isVerticalLegend =\n    legendPosition === \"left\" || legendPosition === \"right\"\n\n  return (\n    <div\n      className={cn(chartWrapperVariants(), className)}\n      data-slot=\"radial-chart\"\n      {...props}\n    >\n      {(title || subtitle) && (\n        <div className={chartHeaderVariants()} data-slot=\"radial-chart-header\">\n          {title && <p className={chartTitleVariants()}>{title}</p>}\n          {subtitle && <p className={chartSubtitleVariants()}>{subtitle}</p>}\n        </div>\n      )}\n\n      <div className=\"relative\">\n        <ResponsiveContainer\n          width=\"100%\"\n          height={height}\n          data-slot=\"radial-chart-chart\"\n        >\n          <RechartsRadialBarChart\n            data={visibleItems}\n            innerRadius=\"25%\"\n            outerRadius=\"90%\"\n            startAngle={startAngle}\n            endAngle={endAngle}\n            barSize={18}\n            cx=\"50%\"\n            cy=\"50%\"\n          >\n            {/* Fixed domain so every bar is scaled to the same max */}\n            {maxValue !== undefined && (\n              <PolarAngleAxis\n                type=\"number\"\n                domain={[0, maxValue]}\n                dataKey=\"value\"\n                angleAxisId={0}\n                tick={false}\n              />\n            )}\n\n            {showTooltip && (\n              <Tooltip cursor={false} content={<ChartTooltip fmt={fmt} />} />\n            )}\n\n            {showLegend && (\n              <Legend\n                {...LEGEND_LAYOUT[legendPosition]}\n                content={() => (\n                  <ChartLegend\n                    items={legendItems}\n                    hiddenItems={hiddenItems}\n                    onToggle={toggleItem}\n                    vertical={isVerticalLegend}\n                  />\n                )}\n              />\n            )}\n\n            <RadialBar\n              dataKey=\"value\"\n              background={showTrack ? { fill: \"var(--muted)\" } : false}\n              cornerRadius={6}\n              isAnimationActive\n              animationDuration={600}\n            />\n          </RechartsRadialBarChart>\n        </ResponsiveContainer>\n\n        {/* Center label overlay */}\n        {innerLabel && (\n          <div className=\"pointer-events-none absolute inset-0 flex flex-col items-center justify-center gap-0.5\">\n            {centerValue !== undefined && (\n              <span className=\"text-2xl leading-none font-semibold text-foreground\">\n                {centerValue}\n              </span>\n            )}\n            <span\n              className={cn(\n                \"text-muted-foreground\",\n                centerValue !== undefined ? \"text-xs\" : \"text-sm font-medium\"\n              )}\n            >\n              {innerLabel}\n            </span>\n          </div>\n        )}\n      </div>\n\n      {footer && (\n        <div className={chartFooterVariants()} data-slot=\"radial-chart-footer\">\n          {footer}\n        </div>\n      )}\n    </div>\n  )\n}\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}
