{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-field",
  "type": "registry:lib",
  "title": "core/field",
  "description": "Framework-free behaviour shared by the ui-* components (field); no DOM, no lit. Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [],
  "registryDependencies": [],
  "files": [
    {
      "path": "src/core/field.ts",
      "type": "registry:lib",
      "target": "components/ui/core/field.ts",
      "content": "/**\n * Form field rules shared by every field: which single message shows under it, and the\n * character counter of a soft `maxCount` limit.\n */\n\nexport type MessageKind = 'error' | 'warn' | 'helper';\n\nexport interface FieldMessage {\n    kind: MessageKind;\n    text: string;\n}\n\nexport interface MessageChoice {\n    showError: boolean;\n    errorText: string;\n    showWarn: boolean;\n    warnText: string;\n    helperText: string;\n    /** Warning content other than `warnText` exists (slotted markup, a React node). */\n    hasWarn?: boolean;\n    /** Helper content other than `helperText` exists. */\n    hasHelper?: boolean;\n}\n\n/** The single message under a field, by priority: error > warning > helper; null for none. */\nexport function pickMessage(c: MessageChoice): FieldMessage | null {\n    if (c.showError) return { kind: 'error', text: c.errorText };\n    if (c.showWarn && (c.warnText || c.hasWarn)) return { kind: 'warn', text: c.warnText };\n    if (c.helperText || c.hasHelper) return { kind: 'helper', text: c.helperText };\n    return null;\n}\n\n/**\n * A soft character limit (Carbon `maxCount`, typing past it isn't blocked): `over` is an error,\n * `near` (10% or less left) is when screen readers hear that the limit is close.\n */\nexport function characterCount(length: number, max: number | undefined): { over: boolean; near: boolean } {\n    if (max === undefined) return { over: false, near: false };\n    const over = length > max;\n    return { over, near: !over && max - length <= Math.ceil(max * 0.1) };\n}\n\n/** What a counter counts (Carbon `counterMode`). */\nexport type CounterMode = 'character' | 'word';\n\nconst segmenters = new Map<string, Intl.Segmenter>();\n\n/**\n * Words in `text` for a word counter. Carbon counts runs of letters (`/\\p{L}+/gu`), which cuts Thai\n * apart at every vowel and tone mark (\"สวัสดีครับ\" → 4); this asks the browser's word segmenter for\n * the language (Thai has no spaces between words) and, like Carbon, counts only words with a letter\n * (numbers don't count). Every segment with a letter counts, not only `isWordLike` ones: Firefox's\n * segmenter marks some Thai words (\"สวัสดี\", \"ครับ\") as not word-like. Without `Intl.Segmenter`, Carbon's rule.\n */\nexport function wordCount(text: string, lang = 'en'): number {\n    if (typeof Intl === 'undefined' || !('Segmenter' in Intl)) return text.match(/\\p{L}+/gu)?.length ?? 0;\n    let segmenter = segmenters.get(lang);\n    if (!segmenter) {\n        segmenter = new Intl.Segmenter(lang, { granularity: 'word' });\n        segmenters.set(lang, segmenter);\n    }\n    let words = 0;\n    for (const { segment } of segmenter.segment(text)) if (/\\p{L}/u.test(segment)) words++;\n    return words;\n}\n\n/** What a counter shows: the characters or the words in `text`. */\nexport function countOf(text: string, mode: CounterMode = 'character', lang = 'en'): number {\n    return mode === 'word' ? wordCount(text, lang) : text.length;\n}\n\n/**\n * A value as text: null and undefined are empty, an array joins its items with commas (as `String`),\n * a plain object shows as JSON (instead of \"[object Object]\"), anything else as `String` would.\n */\nexport function valueText(value: unknown): string {\n    switch (typeof value) {\n        case 'undefined':\n            return '';\n        case 'string':\n            return value;\n        case 'object':\n            if (value === null) return '';\n            if (Array.isArray(value)) return value.map(valueText).join(',');\n            return value instanceof Date ? value.toString() : JSON.stringify(value);\n        case 'number':\n        case 'boolean':\n        case 'bigint':\n        case 'symbol':\n        case 'function':\n            return value.toString();\n    }\n}\n"
    }
  ]
}
