{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chat",
  "type": "registry:ui",
  "title": "ChatThread، ChatDivider، ChatMessage، TypingIndicator",
  "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/Chat.tsx",
      "type": "registry:ui",
      "target": "components/kashi/Chat.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useRef, useState, type HTMLAttributes, type ReactNode } from \"react\";\nimport { formatRelative } from \"@kashi-ui/persian\";\nimport { cx } from \"./cx\";\nimport { useKashiLocale, useMessages } from \"./KashiProvider\";\nimport { faChat } from \"@kashi-ui/persian\";\n\n/* ---------- آیکون‌ها ---------- */\nconst svg = (d: ReactNode, w = 1.7) => (\n  <svg viewBox=\"0 0 20 20\" fill=\"none\" stroke=\"currentColor\" strokeWidth={w} strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">{d}</svg>\n);\nconst Tick = () => svg(<path d=\"m4.5 10.5 3 3 6-7\" />, 1.9);\nconst Ticks = () => svg(<><path d=\"m2.5 10.5 3 3 6-7\" /><path d=\"m9 13.2.3.3 6-7\" /></>, 1.9);\nconst Clock = () => svg(<><circle cx=\"10\" cy=\"10\" r=\"6.5\" /><path d=\"M10 6.5V10l2 1.5\" /></>);\nconst Alert = () => svg(<><circle cx=\"10\" cy=\"10\" r=\"7\" /><path d=\"M10 6.5v4M10 13.5h.01\" /></>);\nconst Copy = () => svg(<><rect x=\"7\" y=\"7\" width=\"10\" height=\"10\" rx=\"2\" /><path d=\"M13 7V5a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2h2\" /></>);\nconst Check = () => svg(<path d=\"m5 10.5 3.2 3L15 7\" />, 2);\nconst Retry = () => svg(<><path d=\"M4 10a6 6 0 1 0 1.8-4.3\" /><path d=\"M4 3.5V7h3.5\" /></>);\nconst Up = () => svg(<path d=\"M6.5 9 9.5 3.5a1.6 1.6 0 0 1 2.9 1.2L11.5 8h3.8a1.5 1.5 0 0 1 1.5 1.8l-1.2 5.5a1.5 1.5 0 0 1-1.5 1.2H6.5M6.5 9v7.5M6.5 9H4v7.5h2.5\" />);\nconst Down = () => svg(<path d=\"M13.5 11 10.5 16.5a1.6 1.6 0 0 1-2.9-1.2L8.5 12H4.7a1.5 1.5 0 0 1-1.5-1.8l1.2-5.5A1.5 1.5 0 0 1 5.9 3.5h7.6M13.5 11V3.5m0 7.5H16V3.5h-2.5\" />);\nconst Arrow = () => svg(<path d=\"M10 4v12m0 0-4.5-4.5M10 16l4.5-4.5\" />, 1.9);\n\n/* ---------- گفتگو ---------- */\nexport interface ChatThreadProps extends HTMLAttributes<HTMLDivElement> {\n  /** نام گفتگو برای صفحه‌خوان @default \"گفتگو\" */\n  label?: string;\n  /** با پیام تازه به پایین برود، اگر کاربر نزدیک پایین است @default true */\n  autoScroll?: boolean;\n  children: ReactNode;\n}\n\n/**\n * ظرف پیام‌ها: ناحیهٔ log که پیام تازه را برای صفحه‌خوان می‌خواند، با پیام تازه پایین می‌ماند\n * (مگر کاربر بالا رفته باشد) و دکمهٔ «رفتن به آخرین پیام» نشان می‌دهد.\n */\nexport function ChatThread({ label, autoScroll = true, className, children, ...rest }: ChatThreadProps) {\n  const M = useMessages({ chat: faChat });\n  const ref = useRef<HTMLDivElement>(null);\n  const stick = useRef(true);\n  const [away, setAway] = useState(false);\n\n  useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const toEnd = () => {\n      if (autoScroll && stick.current) el.scrollTop = el.scrollHeight;\n    };\n    toEnd();\n    const ro = typeof ResizeObserver === \"function\" ? new ResizeObserver(toEnd) : null;\n    const mo = new MutationObserver(toEnd);\n    for (const c of Array.from(el.children)) ro?.observe(c);\n    mo.observe(el, { childList: true, subtree: true, characterData: true });\n    return () => {\n      ro?.disconnect();\n      mo.disconnect();\n    };\n  }, [autoScroll]);\n\n  return (\n    <div className={cx(\"k-chat\", className)}>\n      <div\n        ref={ref}\n        className=\"k-chat__scroll\"\n        role=\"log\"\n        aria-live=\"polite\"\n        aria-label={label ?? M.chat.thread}\n        tabIndex={0}\n        onScroll={(e) => {\n          const el = e.currentTarget;\n          const near = el.scrollHeight - el.scrollTop - el.clientHeight < 48;\n          stick.current = near;\n          setAway(!near);\n        }}\n        {...rest}\n      >\n        {children}\n      </div>\n      <button\n        type=\"button\"\n        className=\"k-chat__jump\"\n        data-show={away ? \"\" : undefined}\n        tabIndex={away ? 0 : -1}\n        aria-hidden={!away}\n        aria-label={M.chat.scrollDown}\n        onClick={() => {\n          stick.current = true;\n          ref.current?.scrollTo({ top: ref.current.scrollHeight, behavior: \"smooth\" });\n        }}\n      >\n        <Arrow />\n      </button>\n    </div>\n  );\n}\n\n/** جداکنندهٔ روز یا بخش داخل گفتگو، مثل «امروز» */\nexport function ChatDivider({ children, className }: { children: ReactNode; className?: string }) {\n  return <div className={cx(\"k-chat__divider\", className)} role=\"separator\">{children}</div>;\n}\n\nexport type ChatRole = \"user\" | \"assistant\" | \"system\";\nexport type ChatStatus = \"sending\" | \"sent\" | \"delivered\" | \"read\" | \"failed\";\n\nexport interface ChatMessageProps extends Omit<HTMLAttributes<HTMLDivElement>, \"role\"> {\n  /** فرستنده: کاربر سمت پایان خط (در راست‌به‌چپ، چپ)، دستیار سمت شروع @default \"assistant\" */\n  from?: ChatRole;\n  /** نام فرستنده؛ برای صفحه‌خوان و گفتگوی گروهی */\n  name?: string;\n  /** آواتار یا هر عنصر کوچک کنار پیام */\n  avatar?: ReactNode;\n  /** زمان ارسال؛ Date به شکل نسبی نشان داده می‌شود */\n  time?: Date | string;\n  /** وضعیت پیام کاربر */\n  status?: ChatStatus;\n  /** پیام ویرایش شده است */\n  edited?: boolean;\n  /** متن ساده برای دکمهٔ کپی؛ با آن دکمهٔ کپی نشان داده می‌شود */\n  copyText?: string;\n  /** دکمهٔ «تلاش دوباره» برای پیام ناموفق یا پاسخ دستیار */\n  onRetry?: () => void;\n  /** دکمه‌های پسندیدن و نپسندیدن پاسخ دستیار */\n  onFeedback?: (value: \"up\" | \"down\") => void;\n  /** بازخورد انتخاب‌شده */\n  feedback?: \"up\" | \"down\" | null;\n  /** دکمه‌های دلخواه دیگر زیر پیام */\n  actions?: ReactNode;\n  children: ReactNode;\n}\n\n/** حباب پیام در گفتگو یا رابط هوش مصنوعی؛ جهت متن هر پیام خودکار است تا پیام انگلیسی هم درست چیده شود */\nexport function ChatMessage({\n  from = \"assistant\", name, avatar, time, status, edited, copyText, onRetry, onFeedback, feedback, actions, className, children, ...rest\n}: ChatMessageProps) {\n  const M = useMessages({ chat: faChat });\n  const { locale, digits } = useKashiLocale();\n  const C = M.chat;\n  const [copied, setCopied] = useState(false);\n  const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);\n  useEffect(() => () => clearTimeout(timer.current), []);\n  const who = name ?? (from === \"user\" ? C.you : from === \"assistant\" ? C.assistant : undefined);\n  const timeText = time instanceof Date ? formatRelative(time, { locale, digits }) : time;\n  const statusIcon = status === \"sending\" ? <Clock /> : status === \"failed\" ? <Alert /> : status === \"sent\" ? <Tick /> : status ? <Ticks /> : null;\n  const hasActions = copyText !== undefined || onRetry || onFeedback || actions;\n\n  if (from === \"system\") return <div className={cx(\"k-chat__system\", className)} {...rest}>{children}</div>;\n\n  return (\n    <div className={cx(\"k-msg\", className)} data-from={from} data-status={status} {...rest}>\n      {avatar && <div className=\"k-msg__avatar\">{avatar}</div>}\n      <div className=\"k-msg__main\">\n        {name && <div className=\"k-msg__name\">{name}</div>}\n        {/* نام فرستنده بیرون از حباب، تا جهت خودکار پیام انگلیسی را فارسی نکند */}\n        {who && !name && <span className=\"k-sr-only\">{who}: </span>}\n        <div className=\"k-msg__bubble\" dir=\"auto\">{children}</div>\n        {(timeText || status || edited) && (\n          <div className=\"k-msg__meta\">\n            {edited && <span>{C.edited}</span>}\n            {timeText && <time dateTime={time instanceof Date ? time.toISOString() : undefined}>{timeText}</time>}\n            {status && <span className=\"k-msg__status\" data-status={status} role=\"img\" aria-label={C[status]}>{statusIcon}</span>}\n          </div>\n        )}\n        {hasActions && (\n          <div className=\"k-msg__actions\" role=\"group\" aria-label={C.actions}>\n            {copyText !== undefined && (\n              <button\n                type=\"button\"\n                className=\"k-msg__action\"\n                aria-label={copied ? C.copied : C.copy}\n                data-done={copied ? \"\" : undefined}\n                onClick={async () => {\n                  try {\n                    await navigator.clipboard.writeText(copyText);\n                    setCopied(true);\n                    clearTimeout(timer.current);\n                    timer.current = setTimeout(() => setCopied(false), 2000);\n                  } catch {\n                    /* کلیپ‌بورد در دسترس نیست */\n                  }\n                }}\n              >\n                {copied ? <Check /> : <Copy />}\n              </button>\n            )}\n            {onRetry && <button type=\"button\" className=\"k-msg__action\" aria-label={C.retry} onClick={onRetry}><Retry /></button>}\n            {onFeedback && (\n              <>\n                <button type=\"button\" className=\"k-msg__action\" aria-label={C.like} aria-pressed={feedback === \"up\"} onClick={() => onFeedback(\"up\")}><Up /></button>\n                <button type=\"button\" className=\"k-msg__action\" aria-label={C.dislike} aria-pressed={feedback === \"down\"} onClick={() => onFeedback(\"down\")}><Down /></button>\n              </>\n            )}\n            {actions}\n            <span className=\"k-sr-only\" aria-live=\"polite\">{copied ? C.copied : \"\"}</span>\n          </div>\n        )}\n      </div>\n    </div>\n  );\n}\n\nexport interface TypingIndicatorProps {\n  /** نام کسی که می‌نویسد؛ برای صفحه‌خوان */\n  name?: string;\n  /** نمایش متن کنار نقطه‌ها */\n  showLabel?: boolean;\n  className?: string;\n}\n\n/** نشانگر «در حال نوشتن…» با سه نقطهٔ موجی؛ صفحه‌خوان یک بار می‌خواندش */\nexport function TypingIndicator({ name, showLabel, className }: TypingIndicatorProps) {\n  const M = useMessages({ chat: faChat });\n  const text = name ? M.chat.typing(name) : M.chat.typingShort;\n  return (\n    <div className={cx(\"k-typing\", className)} role=\"status\">\n      <span className=\"k-typing__dots\" aria-hidden=\"true\"><i /><i /><i /></span>\n      {showLabel ? <span className=\"k-typing__label\">{text}…</span> : <span className=\"k-sr-only\">{text}</span>}\n    </div>\n  );\n}\n"
    }
  ]
}