{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chat-composer",
  "type": "registry:ui",
  "title": "ChatComposer",
  "description": "کادر پیام",
  "dependencies": [
    "@kashi-ui/persian@^2.26.0"
  ],
  "registryDependencies": [
    "https://kashi.sepantapouya.com/r/cx.json",
    "https://kashi.sepantapouya.com/r/file-upload.json",
    "https://kashi.sepantapouya.com/r/kashi-provider.json",
    "https://kashi.sepantapouya.com/r/utils.json"
  ],
  "files": [
    {
      "path": "registry/kashi/ChatComposer.tsx",
      "type": "registry:ui",
      "target": "components/kashi/ChatComposer.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useId, useRef, useState, type ReactNode } from \"react\";\nimport { cx } from \"./cx\";\nimport { formatBytes } from \"./FileUpload\";\nimport { useKashiLocale, useMessages } from \"./KashiProvider\";\nimport { faComposer } from \"@kashi-ui/persian\";\nimport { useIsomorphicLayoutEffect } from \"./utils\";\n\nexport interface ChatComposerSend {\n  text: string;\n  files: File[];\n}\n\nexport interface ChatComposerProps {\n  /** ارسال با Enter یا دکمه؛ متن بدون فاصلهٔ اول و آخر */\n  onSend: (message: ChatComposerSend) => void;\n  /** پاسخ در حال نوشتن است: دکمهٔ ارسال «توقف» می‌شود */\n  streaming?: boolean;\n  /** با دکمهٔ توقف */\n  onStop?: () => void;\n  /** متن کنترل‌شده */\n  value?: string;\n  onValueChange?: (value: string) => void;\n  /** دکمهٔ پیوست؛ مقدار accept فایل‌ها، مثلاً \"image/*,.pdf\" یا true برای همه */\n  attach?: boolean | string;\n  /** حداکثر تعداد پیوست @default 5 */\n  maxFiles?: number;\n  /** حداکثر حجم هر فایل به بایت */\n  maxSize?: number;\n  /** حداکثر طول متن؛ شمارنده نزدیک سقف نشان داده می‌شود */\n  maxLength?: number;\n  /** بیشترین ارتفاع پیش از اسکرول، به ردیف @default 8 */\n  maxRows?: number;\n  placeholder?: string;\n  /** @default \"پیام\" */\n  label?: string;\n  /** راهنمای کلیدها زیر کادر @default false */\n  showHint?: boolean;\n  /** دکمه‌های دلخواه کنار پیوست، مثل انتخاب مدل */\n  tools?: ReactNode;\n  disabled?: boolean;\n  autoFocus?: boolean;\n  className?: string;\n}\n\nconst svg = (d: ReactNode) => (\n  <svg viewBox=\"0 0 20 20\" fill=\"none\" stroke=\"currentColor\" strokeWidth={1.8} strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">{d}</svg>\n);\nconst Clip = () => svg(<path d=\"m15.5 9.5-5.6 5.6a3.5 3.5 0 0 1-5-5l6-6a2.3 2.3 0 0 1 3.3 3.3l-6 6a1.2 1.2 0 0 1-1.7-1.7l5.5-5.5\" />);\nconst Send = () => svg(<path d=\"M10 16V4m0 0L5 9m5-5 5 5\" />);\nconst Stop = () => <svg viewBox=\"0 0 20 20\" aria-hidden=\"true\"><rect x=\"6\" y=\"6\" width=\"8\" height=\"8\" rx=\"1.5\" fill=\"currentColor\" /></svg>;\nconst X = () => svg(<path d=\"M6 6l8 8M14 6l-8 8\" />);\nconst Doc = () => svg(<><path d=\"M11.5 3H6a1.5 1.5 0 0 0-1.5 1.5v11A1.5 1.5 0 0 0 6 17h8a1.5 1.5 0 0 0 1.5-1.5V7z\" /><path d=\"M11.5 3v4h4\" /></>);\n\n/**\n * کادر نوشتن پیام برای گفتگو و دستیار هوش مصنوعی: بلند شدن خودکار، Enter برای ارسال و Shift+Enter برای خط تازه\n * (هنگام تایپ با IME ارسال نمی‌کند)، پیوست با دکمه یا چسباندن تصویر، و دکمهٔ توقف هنگام پاسخ.\n */\nexport function ChatComposer({\n  onSend, streaming, onStop, value, onValueChange, attach, maxFiles = 5, maxSize, maxLength, maxRows = 8,\n  placeholder, label, showHint, tools, disabled, autoFocus, className,\n}: ChatComposerProps) {\n  const M = useMessages({ composer: faComposer });\n  const { locale, n } = useKashiLocale();\n  const C = M.composer;\n  const [inner, setInner] = useState(\"\");\n  const text = value ?? inner;\n  const [files, setFiles] = useState<File[]>([]);\n  const [error, setError] = useState(\"\");\n  const area = useRef<HTMLTextAreaElement>(null);\n  const picker = useRef<HTMLInputElement>(null);\n  const id = useId();\n  const [urls, setUrls] = useState<string[]>([]);\n\n  const setText = (t: string) => {\n    if (value === undefined) setInner(t);\n    onValueChange?.(t);\n  };\n\n  // بلند شدن خودکار تا maxRows\n  useIsomorphicLayoutEffect(() => {\n    const el = area.current;\n    if (!el) return;\n    el.style.blockSize = \"auto\";\n    const line = parseFloat(getComputedStyle(el).lineHeight) || 24;\n    el.style.blockSize = `${Math.min(el.scrollHeight, line * maxRows + 2)}px`;\n  }, [text, maxRows]);\n\n  // پیش‌نمایش تصویرها\n  useEffect(() => {\n    const list = files.map((f) => (f.type.startsWith(\"image/\") && typeof URL.createObjectURL === \"function\" ? URL.createObjectURL(f) : \"\"));\n    setUrls(list);\n    return () => list.forEach((u) => u && URL.revokeObjectURL(u));\n  }, [files]);\n\n  const add = (list: File[]) => {\n    setError(\"\");\n    const ok: File[] = [];\n    for (const f of list) {\n      if (maxSize && f.size > maxSize) setError(C.tooLarge(f.name, formatBytes(maxSize, locale)));\n      else ok.push(f);\n    }\n    const next = [...files, ...ok];\n    if (next.length > maxFiles) setError(C.tooMany(n(maxFiles)));\n    setFiles(next.slice(0, maxFiles));\n  };\n\n  const canSend = !disabled && !streaming && (text.trim().length > 0 || files.length > 0) && (!maxLength || text.length <= maxLength);\n  const send = () => {\n    if (!canSend) return;\n    onSend({ text: text.trim(), files });\n    setText(\"\");\n    setFiles([]);\n    setError(\"\");\n    area.current?.focus();\n  };\n\n  const near = maxLength && text.length > maxLength * 0.8;\n  const hintId = `${id}-hint`;\n  const errId = `${id}-err`;\n\n  return (\n    <div className={cx(\"k-composer\", className)} data-disabled={disabled ? \"\" : undefined} data-invalid={error ? \"\" : undefined}>\n      {files.length > 0 && (\n        <ul className=\"k-composer__files\" aria-label={C.attachments}>\n          {files.map((f, i) => (\n            <li key={`${f.name}-${i}`} className=\"k-composer__file\">\n              {urls[i] ? <img src={urls[i]} alt=\"\" className=\"k-composer__thumb\" /> : <span className=\"k-composer__thumb\"><Doc /></span>}\n              <span className=\"k-composer__file-text\">\n                <span className=\"k-composer__file-name\" dir=\"auto\">{f.name}</span>\n                <span className=\"k-composer__file-size\">{formatBytes(f.size, locale)}</span>\n              </span>\n              <button type=\"button\" className=\"k-composer__remove\" aria-label={C.removeAttachment(f.name)} onClick={() => setFiles(files.filter((_, j) => j !== i))}><X /></button>\n            </li>\n          ))}\n        </ul>\n      )}\n      <textarea\n        ref={area}\n        className=\"k-composer__input\"\n        rows={1}\n        dir=\"auto\"\n        value={text}\n        disabled={disabled}\n        autoFocus={autoFocus}\n        placeholder={placeholder ?? C.placeholder}\n        aria-label={label ?? C.label}\n        aria-describedby={[showHint ? hintId : \"\", error ? errId : \"\"].filter(Boolean).join(\" \") || undefined}\n        aria-invalid={maxLength && text.length > maxLength ? true : undefined}\n        onChange={(e) => setText(e.target.value)}\n        onKeyDown={(e) => {\n          if (e.key === \"Enter\" && !e.shiftKey && !e.nativeEvent.isComposing && e.keyCode !== 229) {\n            e.preventDefault();\n            send();\n          }\n        }}\n        onPaste={(e) => {\n          if (!attach) return;\n          const pasted = Array.from(e.clipboardData.files);\n          if (pasted.length) {\n            e.preventDefault();\n            add(pasted);\n          }\n        }}\n      />\n      <div className=\"k-composer__bar\">\n        {attach && (\n          <>\n            <button type=\"button\" className=\"k-composer__tool\" aria-label={C.attach} disabled={disabled || files.length >= maxFiles} onClick={() => picker.current?.click()}>\n              <Clip />\n            </button>\n            <input\n              ref={picker}\n              type=\"file\"\n              hidden\n              multiple={maxFiles > 1}\n              accept={typeof attach === \"string\" ? attach : undefined}\n              onChange={(e) => {\n                add(Array.from(e.target.files ?? []));\n                e.target.value = \"\";\n              }}\n            />\n          </>\n        )}\n        {tools}\n        {near ? <span className=\"k-composer__count\" data-over={text.length > maxLength! ? \"\" : undefined}>{n(text.length)}/{n(maxLength!)}</span> : null}\n        {streaming ? (\n          <button type=\"button\" className=\"k-composer__send\" data-stop=\"\" aria-label={C.stop} onClick={onStop}><Stop /></button>\n        ) : (\n          <button type=\"button\" className=\"k-composer__send\" aria-label={C.send} disabled={!canSend} onClick={send}><Send /></button>\n        )}\n      </div>\n      {error && <p className=\"k-hint k-hint--error k-composer__error\" id={errId} role=\"alert\">{error}</p>}\n      {showHint && <p className=\"k-composer__hint\" id={hintId}>{C.hint}</p>}\n    </div>\n  );\n}\n"
    }
  ]
}