{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "time-clock",
  "type": "registry:ui",
  "title": "TimeClock",
  "description": "انتخاب ساعت",
  "dependencies": [
    "@kashi-ui/persian@^2.26.0"
  ],
  "registryDependencies": [
    "https://kashi.sepantapouya.com/r/cx.json",
    "https://kashi.sepantapouya.com/r/kashi-provider.json"
  ],
  "files": [
    {
      "path": "registry/kashi/TimeClock.tsx",
      "type": "registry:ui",
      "target": "components/kashi/TimeClock.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useRef, useState, type KeyboardEvent, type PointerEvent } from \"react\";\nimport { formatTime, parseTime, toFaDigits } from \"@kashi-ui/persian\";\nimport { cx } from \"./cx\";\nimport { useKashiLocale, useMessages } from \"./KashiProvider\";\nimport { faTimePicker } from \"@kashi-ui/persian\";\n\nexport type TimeClockView = \"hour\" | \"minute\";\n\nexport interface TimeClockProps {\n  /** \"HH:MM\" با ارقام لاتین */\n  value?: string | null;\n  defaultValue?: string | null;\n  /** با هر انتخاب (کلیک، کشیدن یا کیبورد) */\n  onChange?: (value: string) => void;\n  /** وقتی دقیقه انتخاب شد (رها کردن یا Enter روی دقیقه)؛ TimePicker با آن فوکوس را به «تأیید» می‌برد */\n  onComplete?: (value: string) => void;\n  view?: TimeClockView;\n  /** @default \"hour\" */\n  defaultView?: TimeClockView;\n  onViewChange?: (view: TimeClockView) => void;\n  /** گام دقیقه‌ها به دقیقه @default 5 */\n  step?: number;\n  /** \"08:00\" */\n  min?: string;\n  max?: string;\n  /** @default \"fa\" */\n  digits?: \"fa\" | \"latin\";\n  /** @default \"انتخاب ساعت\" */\n  \"aria-label\"?: string;\n  labels?: { hour?: string; minute?: string };\n  autoFocus?: boolean;\n  className?: string;\n}\n\nconst DAY = 24 * 60;\nconst pad = (n: number) => String(n).padStart(2, \"0\");\nconst toMin = (s?: string | null) => {\n  const t = s ? parseTime(s) : null;\n  return t ? t.hours * 60 + t.minutes : null;\n};\nconst fmt = (m: number) => formatTime({ hours: Math.floor(m / 60), minutes: m % 60 });\n\n// شعاع حلقه‌ها به درصدِ پهنای صفحه (مرکز ۵۰٪)\nconst OUTER = 40;\nconst INNER = 26;\n/** ساعت‌های ۱ تا ۱۲ حلقهٔ داخلی‌اند و ۰۰ و ۱۳ تا ۲۳ حلقهٔ بیرونی (مثل ساعت‌های ۲۴ساعتهٔ رایج) */\nconst isInner = (h: number) => h >= 1 && h <= 12;\nconst hourAngle = (h: number) => (h % 12) * 30;\n\n/**\n * صفحهٔ ساعت عقربه‌ای ۲۴ساعته: اول ساعت، بعد دقیقه.\n * با کلیک یا کشیدن روی صفحه، یا با کیبورد (↑↓←→، Home/End، Enter) کار می‌کند.\n */\nexport function TimeClock({\n  value, defaultValue = null, onChange, onComplete, view, defaultView = \"hour\", onViewChange, step = 5, min, max, digits,\n  \"aria-label\": ariaLabel, labels = {}, autoFocus, className,\n}: TimeClockProps) {\n  const M = useMessages({ timePicker: faTimePicker });\n  const { digits: D } = useKashiLocale();\n  digits ??= D;\n  ariaLabel ??= M.timePicker.clock;\n  const { hour: hourLabel = M.timePicker.hour, minute: minuteLabel = M.timePicker.minute } = labels;\n  const f = (s: string) => (digits === \"fa\" ? toFaDigits(s) : s);\n\n  const [inner, setInner] = useState<string | null>(defaultValue);\n  const current = toMin(value !== undefined ? value : inner);\n  const [innerView, setInnerView] = useState<TimeClockView>(defaultView);\n  const mode = view ?? innerView;\n  const dial = useRef<HTMLDivElement>(null);\n  const dragging = useRef(false);\n\n  const lo = toMin(min) ?? 0;\n  const hi = toMin(max) ?? DAY - 1;\n  const hourOk = (h: number) => h * 60 + 59 >= lo && h * 60 <= hi;\n  const minuteOk = (h: number, m: number) => h * 60 + m >= lo && h * 60 + m <= hi;\n  const h = current === null ? null : Math.floor(current / 60);\n  const m = current === null ? null : current % 60;\n\n  useEffect(() => {\n    if (autoFocus) dial.current?.focus();\n  }, []);\n\n  const setMode = (v: TimeClockView) => {\n    if (view === undefined) setInnerView(v);\n    onViewChange?.(v);\n  };\n  const set = (mins: number) => {\n    const v = fmt(Math.min(hi, Math.max(lo, mins)));\n    if (value === undefined) setInner(v);\n    if (toMin(v) !== current) onChange?.(v);\n    return v;\n  };\n  const pickHour = (hh: number) => {\n    if (!hourOk(hh)) return;\n    // دقیقهٔ فعلی می‌ماند؛ اگر با ساعت تازه بیرون از بازه شد، به نزدیک‌ترین دقیقهٔ مجاز می‌رود\n    set(hh * 60 + (m ?? 0));\n  };\n  const pickMinute = (mm: number) => {\n    const hh = h ?? Math.floor(lo / 60);\n    if (!minuteOk(hh, mm)) return null;\n    return set(hh * 60 + mm);\n  };\n\n  /** نقطهٔ اشاره‌گر ← ساعت یا دقیقه */\n  const fromPoint = (e: PointerEvent) => {\n    const r = dial.current!.getBoundingClientRect();\n    const dx = e.clientX - (r.left + r.width / 2);\n    const dy = e.clientY - (r.top + r.height / 2);\n    const angle = (Math.atan2(dx, -dy) * 180) / Math.PI + 360;\n    const dist = (Math.hypot(dx, dy) / r.width) * 100;\n    if (mode === \"hour\") {\n      const i = Math.round(angle / 30) % 12;\n      return dist < (OUTER + INNER) / 2 ? (i === 0 ? 12 : i) : i === 0 ? 0 : i + 12;\n    }\n    return (Math.round(Math.round(angle / 6) / step) * step) % 60;\n  };\n  const pointer = (e: PointerEvent) => {\n    const v = fromPoint(e);\n    return mode === \"hour\" ? (pickHour(v), null) : pickMinute(v);\n  };\n\n  const onKeyDown = (e: KeyboardEvent) => {\n    const k = e.key;\n    if (k === \"Enter\" || k === \" \") {\n      e.preventDefault();\n      if (current === null) return;\n      if (mode === \"hour\") setMode(\"minute\");\n      else onComplete?.(fmt(current));\n      return;\n    }\n    const base = current ?? lo;\n    let next: number;\n    const up = k === \"ArrowUp\" || k === \"ArrowRight\";\n    const down = k === \"ArrowDown\" || k === \"ArrowLeft\";\n    if (mode === \"hour\") {\n      const hh = Math.floor(base / 60);\n      if (up || down) {\n        // ساعت مجاز بعدی یا قبلی؛ در ۲۳ به ۰۰ برمی‌گردد\n        let n = hh;\n        for (let i = 0; i < 24; i++) {\n          n = (n + (up ? 1 : 23)) % 24;\n          if (hourOk(n)) break;\n        }\n        next = n * 60 + (base % 60);\n      } else if (k === \"Home\") next = lo;\n      else if (k === \"End\") next = Math.floor(hi / 60) * 60 + (base % 60);\n      else return;\n    } else {\n      const mm = base % 60;\n      const hh = base - mm;\n      if (up) next = hh + ((Math.floor(mm / step) * step + step) % 60);\n      else if (down) next = hh + ((Math.ceil(mm / step) * step - step + 60) % 60);\n      else if (k === \"Home\") next = hh;\n      else if (k === \"End\") next = hh + Math.floor(59 / step) * step;\n      else return;\n    }\n    e.preventDefault();\n    set(next);\n  };\n\n  // اعداد روی صفحه\n  const nums =\n    mode === \"hour\"\n      ? Array.from({ length: 24 }, (_, hh) => ({\n          v: hh,\n          label: hh === 0 ? \"00\" : String(hh),\n          angle: hourAngle(hh),\n          r: isInner(hh) ? INNER : OUTER,\n          ring: isInner(hh) ? \"inner\" : \"outer\",\n          on: h === hh,\n          off: !hourOk(hh),\n        }))\n      : Array.from({ length: 12 }, (_, i) => ({\n          v: i * 5,\n          label: pad(i * 5),\n          angle: i * 30,\n          r: OUTER,\n          ring: \"outer\",\n          on: m === i * 5,\n          off: h !== null && !minuteOk(h, i * 5),\n        }));\n\n  const handAngle = current === null ? null : mode === \"hour\" ? hourAngle(h!) : m! * 6;\n  const handR = mode === \"hour\" && h !== null && isInner(h) ? INNER : OUTER;\n  const timeText = current === null ? \"\" : f(fmt(current));\n\n  return (\n    <div className={cx(\"k-clock\", className)} role=\"group\" aria-label={ariaLabel}>\n      <div className=\"k-clock__header\" dir=\"ltr\">\n        <button\n          type=\"button\"\n          className=\"k-clock__unit\"\n          aria-pressed={mode === \"hour\"}\n          aria-label={h === null ? hourLabel : `${hourLabel} ${f(pad(h))}`}\n          onClick={() => setMode(\"hour\")}\n        >\n          {h === null ? \"--\" : f(pad(h))}\n        </button>\n        <span className=\"k-clock__sep\" aria-hidden=\"true\">:</span>\n        <button\n          type=\"button\"\n          className=\"k-clock__unit\"\n          aria-pressed={mode === \"minute\"}\n          aria-label={m === null ? minuteLabel : `${minuteLabel} ${f(pad(m))}`}\n          onClick={() => setMode(\"minute\")}\n        >\n          {m === null ? \"--\" : f(pad(m))}\n        </button>\n      </div>\n\n      <div\n        ref={dial}\n        className=\"k-clock__dial\"\n        data-view={mode}\n        role=\"slider\"\n        tabIndex={0}\n        dir=\"ltr\"\n        aria-label={mode === \"hour\" ? hourLabel : minuteLabel}\n        aria-valuemin={0}\n        aria-valuemax={mode === \"hour\" ? 23 : 59}\n        aria-valuenow={(mode === \"hour\" ? h : m) ?? undefined}\n        aria-valuetext={timeText || undefined}\n        onKeyDown={onKeyDown}\n        onPointerDown={(e) => {\n          if (e.button !== 0) return;\n          e.preventDefault();\n          dial.current?.focus();\n          dial.current?.setPointerCapture?.(e.pointerId);\n          dragging.current = true;\n          pointer(e);\n        }}\n        onPointerMove={(e) => {\n          if (dragging.current) pointer(e);\n        }}\n        onPointerUp={(e) => {\n          if (!dragging.current) return;\n          dragging.current = false;\n          const v = pointer(e);\n          if (mode === \"hour\") setMode(\"minute\");\n          else if (v) onComplete?.(v);\n        }}\n        onPointerCancel={() => (dragging.current = false)}\n      >\n        <span className=\"k-clock__center\" aria-hidden=\"true\" />\n        {handAngle !== null && (\n          <span\n            className=\"k-clock__hand\"\n            aria-hidden=\"true\"\n            data-free={mode === \"minute\" && m! % 5 !== 0 ? \"\" : undefined}\n            style={{ height: `${handR}%`, rotate: `${handAngle}deg` }}\n          />\n        )}\n        {nums.map((n) => {\n          const rad = (n.angle * Math.PI) / 180;\n          return (\n            <span\n              key={`${mode}-${n.v}`}\n              className=\"k-clock__num\"\n              data-ring={n.ring}\n              data-selected={n.on ? \"\" : undefined}\n              data-disabled={n.off ? \"\" : undefined}\n              aria-hidden=\"true\"\n              style={{ left: `${+(50 + Math.sin(rad) * n.r).toFixed(2)}%`, top: `${+(50 - Math.cos(rad) * n.r).toFixed(2)}%` }}\n            >\n              {f(n.label)}\n            </span>\n          );\n        })}\n      </div>\n    </div>\n  );\n}\n"
    }
  ]
}