{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bmi-card",
  "type": "registry:block",
  "title": "BMI Card",
  "description": "A configurable BMI indicator ring with thresholds and tooltip.",
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "tooltip"
  ],
  "files": [
    {
      "path": "registry/turbopills/blocks/bmi-card/bmi-card.tsx",
      "content": "\"use client\";\r\n\r\nimport { useEffect, useId, useMemo, useRef, useState } from \"react\";\r\nimport { CircleHelp, Check } from \"lucide-react\";\r\n\r\nimport { cn } from \"@/lib/utils\";\r\n\r\nimport {\r\n  Tooltip,\r\n  TooltipContent,\r\n  TooltipTrigger,\r\n} from \"@/components/ui/tooltip\";\r\n\r\nexport type BMIThreshold = {\r\n  maxBmi: number;\r\n  isTargetRange?: boolean;\r\n  title: string;\r\n  description: string;\r\n  color: string | [string, string];\r\n  statusMessage?: {\r\n    text: string;\r\n    className: string;\r\n  };\r\n};\r\n\r\nexport const DEFAULT_BMI_THRESHOLDS: BMIThreshold[] = [\r\n  {\r\n    maxBmi: 0,\r\n    title: \"BMI\",\r\n    description: \"Enter height and weight to calculate it.\",\r\n    color: \"#e5e7eb\",\r\n  },\r\n  {\r\n    maxBmi: 25,\r\n    isTargetRange: true,\r\n    title: \"Balanced\",\r\n    description: \"You're in a healthy range.\",\r\n    color: [\"#91f63e\", \"#19a349\"],\r\n    statusMessage: {\r\n      text: \"This treatment may not be for you.\",\r\n      className: \"bg-[#FFEDD8] text-[#DB913C]\",\r\n    },\r\n  },\r\n  {\r\n    maxBmi: 27.1,\r\n    title: \"At Risk\",\r\n    description: \"You're approaching an unhealthy range.\",\r\n    color: [\"#f6ac3e\", \"#e2e21b\"],\r\n    statusMessage: {\r\n      text: \"You may be eligible for treatment & promotional offers.\",\r\n      className: \"bg-[#E1FFE2] text-[#279329]\",\r\n    },\r\n  },\r\n  {\r\n    maxBmi: Infinity,\r\n    title: \"High Risk\",\r\n    description: \"Excess weight may be affecting your health.\",\r\n    color: [\"#f63e3e\", \"#ff712a\"],\r\n    statusMessage: {\r\n      text: \"You may be eligible for treatment & promotional offers.\",\r\n      className: \"bg-[#E1FFE2] text-[#279329]\",\r\n    },\r\n  },\r\n];\r\n\r\nexport interface BMICardProps {\r\n  bmi: number;\r\n  minValue?: number;\r\n  maxValue?: number;\r\n  shape?: \"circle\" | \"semicircle\" | \"arc\";\r\n  strokeWidth?: number;\r\n  showThresholdMarkers?: boolean;\r\n  valuePosition?: \"inside\" | \"outside\";\r\n  fillMode?: \"full\" | \"progress\";\r\n  animated?: boolean;\r\n  animationDuration?: number;\r\n  showStatusMessage?: boolean;\r\n  tooltipContent?: React.ReactNode;\r\n  thresholds?: BMIThreshold[];\r\n  size?: number;\r\n  label?: string;\r\n  className?: string;\r\n  showTargetMessage?: boolean;\r\n}\r\n\r\nconst SHAPE_CONFIG = {\r\n  circle: { degrees: 360, rotation: -90 },\r\n  semicircle: { degrees: 180, rotation: 180 },\r\n  arc: { degrees: 270, rotation: 135 },\r\n} as const;\r\n\r\nconst BG_COLOR = \"#e5e7eb\";\r\n\r\nfunction getThreshold(bmi: number, thresholds: BMIThreshold[]) {\r\n  if (bmi <= 0) return thresholds[0];\r\n  return thresholds.find((t) => bmi < t.maxBmi) ?? thresholds.at(-1)!;\r\n}\r\n\r\nfunction useAnimatedValue(target: number, duration: number, enabled: boolean) {\r\n  const [value, setValue] = useState(enabled ? 0 : target);\r\n  const frameRef = useRef<number>(undefined);\r\n\r\n  useEffect(() => {\r\n    if (!enabled) {\r\n      setValue(target);\r\n      return;\r\n    }\r\n\r\n    const start = value;\r\n    let startTime: number;\r\n\r\n    const animate = (time: number) => {\r\n      startTime ??= time;\r\n      const t = Math.min(1, (time - startTime) / duration);\r\n      const eased = 1 - Math.pow(1 - t, 3);\r\n      setValue(start + (target - start) * eased);\r\n      if (t < 1) frameRef.current = requestAnimationFrame(animate);\r\n    };\r\n\r\n    frameRef.current = requestAnimationFrame(animate);\r\n    return () => {\r\n      if (frameRef.current) cancelAnimationFrame(frameRef.current);\r\n    };\r\n  }, [target, duration, enabled]);\r\n\r\n  return value;\r\n}\r\n\r\nexport function BMICard({\r\n  bmi,\r\n  minValue = 5,\r\n  maxValue = 30,\r\n  shape = \"circle\",\r\n  strokeWidth = 10,\r\n  showThresholdMarkers = true,\r\n  valuePosition = \"inside\",\r\n  fillMode = \"progress\",\r\n  animated = true,\r\n  animationDuration = 800,\r\n  showStatusMessage = true,\r\n  tooltipContent =\r\n  \"Eligibility is typically BMI 30+, or 27+ with a related condition such as type 2 diabetes, high blood pressure, or high cholesterol. In some cases, people with BMI above 25 may qualify based on medical history.\",\r\n  thresholds = DEFAULT_BMI_THRESHOLDS,\r\n  size = 132,\r\n  label = \"Your BMI\",\r\n  className,\r\n  showTargetMessage = true,\r\n}: BMICardProps) {\r\n  const displayBmi = Math.max(0, bmi);\r\n\r\n  const threshold = useMemo(\r\n    () => getThreshold(bmi, thresholds),\r\n    [bmi, thresholds],\r\n  );\r\n\r\n  const isSmall = size <= 100;\r\n\r\n  const range = maxValue - minValue;\r\n  const targetProgress =\r\n    fillMode === \"full\"\r\n      ? 1\r\n      : Math.min(1, Math.max(0, (displayBmi - minValue) / range));\r\n  const progress = useAnimatedValue(targetProgress, animationDuration, animated);\r\n\r\n  const radius = (size - strokeWidth) / 2;\r\n  const circumference = 2 * Math.PI * radius;\r\n  const center = size / 2;\r\n  const { degrees, rotation } = SHAPE_CONFIG[shape];\r\n  const viewBoxHeight = shape === \"semicircle\" ? size / 2 + strokeWidth : size;\r\n\r\n  const gradientId = useId();\r\n  const isGradient = Array.isArray(threshold.color);\r\n  const strokeColor = isGradient\r\n    ? `url(#${gradientId})`\r\n    : (threshold.color as string);\r\n\r\n  const thresholdMarkers = showThresholdMarkers\r\n    ? thresholds\r\n      .filter((t) => t.maxBmi !== Infinity && t.maxBmi > 0)\r\n      .map((t) => {\r\n        const ratio = (t.maxBmi - minValue) / range;\r\n        const angle = ((rotation + degrees * ratio) * Math.PI) / 180;\r\n        const tickLength = strokeWidth + 4;\r\n\r\n        const innerR = radius - tickLength / 2;\r\n        const outerR = radius + tickLength / 2;\r\n        return {\r\n          x1: center + innerR * Math.cos(angle),\r\n          y1: center + innerR * Math.sin(angle),\r\n          x2: center + outerR * Math.cos(angle),\r\n          y2: center + outerR * Math.sin(angle),\r\n          bmi: t.maxBmi,\r\n        };\r\n      })\r\n    : [];\r\n\r\n  const arcLength = circumference * (degrees / 360);\r\n  const filledLength = arcLength * progress;\r\n  const bgDasharray = `${arcLength} ${circumference}`;\r\n  const dasharray = `${filledLength} ${circumference}`;\r\n\r\n  const isInTargetRange = Boolean(threshold.isTargetRange) && bmi > 0;\r\n\r\n  return (\r\n    <div\r\n      className={cn(\r\n        \"w-full flex flex-col items-center gap-6 mb-10 mt-4 sm:mt-11\",\r\n        className,\r\n      )}\r\n    >\r\n      <div className=\"flex justify-center items-center gap-6\">\r\n        <div className=\"relative\" style={{ width: size, height: viewBoxHeight }}>\r\n          <svg\r\n            width={size}\r\n            height={viewBoxHeight}\r\n            viewBox={`0 0 ${size} ${viewBoxHeight}`}\r\n            className=\"text-background\"\r\n          >\r\n            {isGradient && (\r\n              <defs>\r\n                <linearGradient id={gradientId} x1=\"0%\" y1=\"0%\" x2=\"100%\" y2=\"100%\">\r\n                  <stop\r\n                    offset=\"0%\"\r\n                    stopColor={(threshold.color as [string, string])[0]}\r\n                  />\r\n                  <stop\r\n                    offset=\"100%\"\r\n                    stopColor={(threshold.color as [string, string])[1]}\r\n                  />\r\n                </linearGradient>\r\n              </defs>\r\n            )}\r\n\r\n            <circle\r\n              cx={center}\r\n              cy={center}\r\n              r={radius}\r\n              fill=\"none\"\r\n              stroke={BG_COLOR}\r\n              strokeWidth={strokeWidth}\r\n              strokeDasharray={bgDasharray}\r\n              strokeLinecap=\"round\"\r\n              transform={`rotate(${rotation} ${center} ${center})`}\r\n            />\r\n\r\n            {progress > 0 && (\r\n              <circle\r\n                cx={center}\r\n                cy={center}\r\n                r={radius}\r\n                fill=\"none\"\r\n                stroke={strokeColor}\r\n                strokeWidth={strokeWidth}\r\n                strokeDasharray={dasharray}\r\n                strokeLinecap=\"round\"\r\n                transform={`rotate(${rotation} ${center} ${center})`}\r\n              />\r\n            )}\r\n\r\n            {thresholdMarkers.map((marker, i) => (\r\n              <line\r\n                key={i}\r\n                x1={marker.x1}\r\n                y1={marker.y1}\r\n                x2={marker.x2}\r\n                y2={marker.y2}\r\n                stroke=\"currentColor\"\r\n                strokeWidth={2}\r\n                strokeLinecap=\"round\"\r\n              />\r\n            ))}\r\n          </svg>\r\n\r\n          {valuePosition === \"inside\" && (\r\n            <div\r\n              className=\"absolute inset-0 flex flex-col items-center justify-center\"\r\n              style={{ paddingTop: shape === \"semicircle\" ? size * 0.25 : 0 }}\r\n            >\r\n              <span\r\n                className={cn(\r\n                  \"font-bold leading-none\",\r\n                  shape === \"circle\"\r\n                    ? isSmall\r\n                      ? \"text-[20px]\"\r\n                      : \"text-[34px]\"\r\n                    : isSmall\r\n                      ? \"text-[16px]\"\r\n                      : \"text-[28px]\",\r\n                )}\r\n              >\r\n                {displayBmi}\r\n              </span>\r\n              {!isSmall && (\r\n                <span\r\n                  className={cn(\r\n                    \"font-semibold text-muted-foreground\",\r\n                    shape === \"circle\" ? \"text-sm\" : \"text-xs\",\r\n                  )}\r\n                >\r\n                  {label}\r\n                </span>\r\n              )}\r\n            </div>\r\n          )}\r\n        </div>\r\n\r\n        <div className=\"flex flex-col gap-1\">\r\n          <div className=\"flex items-center gap-2\">\r\n            <h3 className=\"text-xl font-semibold tracking-tight\">{threshold.title}</h3>\r\n            {tooltipContent && (\r\n              <Tooltip>\r\n                <TooltipTrigger asChild>\r\n                  <button className=\"text-muted-foreground hover:text-primary transition-colors\">\r\n                    <CircleHelp className=\"size-4\" />\r\n                  </button>\r\n                </TooltipTrigger>\r\n                <TooltipContent side=\"bottom\" className=\"max-w-70 p-3\">\r\n                  {tooltipContent}\r\n                </TooltipContent>\r\n              </Tooltip>\r\n            )}\r\n          </div>\r\n          <p className=\"text-sm text-muted-foreground max-w-37.5\">\r\n            {threshold.description}\r\n          </p>\r\n\r\n          {valuePosition === \"outside\" && (\r\n            <div className=\"flex items-baseline gap-1.5\">\r\n              <span className=\"text-base font-medium text-muted-foreground\">{label}</span>\r\n              <span className=\"text-xl font-bold\">{displayBmi}</span>\r\n            </div>\r\n          )}\r\n\r\n          {showTargetMessage && bmi > 0 && isInTargetRange && (\r\n            <div className=\"mt-2 text-sm flex items-center gap-1.5\">\r\n              <Check className=\"size-4 text-green-500\" />\r\n              <span className=\"text-green-600 font-medium\">Target reached!</span>\r\n            </div>\r\n          )}\r\n        </div>\r\n      </div>\r\n\r\n      {showStatusMessage && bmi > 0 && threshold.statusMessage && (\r\n        <div\r\n          className={cn(\r\n            \"text-sm px-5 py-2 rounded-md font-semibold\",\r\n            threshold.statusMessage.className,\r\n          )}\r\n        >\r\n          {threshold.statusMessage.text}\r\n        </div>\r\n      )}\r\n    </div>\r\n  );\r\n}\r\n",
      "type": "registry:block",
      "target": "components/turbopills/blocks/bmi-card.tsx"
    }
  ]
}