{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "gulf-phone-input",
  "type": "registry:ui",
  "title": "Gulf phone input",
  "description": "A phone number from any of the six GCC countries. Reads Arabic-Indic digits, returns E.164 and stays left to right in Arabic.",
  "dependencies": [
    "@dualkit/format"
  ],
  "registryDependencies": [
    "input",
    "https://dualkit.dev/r/gulf-field.json"
  ],
  "files": [
    {
      "path": "gulf/gulf-phone-input.tsx",
      "type": "registry:ui",
      "target": "components/gulf/gulf-phone-input.tsx",
      "content": "import {\n  parseGulfPhone, toWesternDigits,\n  type GulfCountry, type GulfPhone, type GulfPhoneError, type Numerals, type Result,\n} from '@dualkit/format'\nimport { useState } from 'react'\nimport { Input } from '@/components/ui/input'\nimport { GulfField, cx, LTR_PLACEHOLDER, useMaskedInput, useShownError, withNumerals, type GulfFieldText, type Mask } from './gulf-field'\n\nexport type GulfPhoneResult = Result<GulfPhone, GulfPhoneError>\n\n/** The six GCC calling codes, in the order the picker lists them. */\nexport const GULF_DIAL_CODES: Readonly<Record<GulfCountry, string>> = {\n  AE: '971', SA: '966', QA: '974', KW: '965', BH: '973', OM: '968',\n}\nconst COUNTRIES = Object.keys(GULF_DIAL_CODES) as GulfCountry[]\n\n/**\n * Reads what was typed with the picked country. A number typed with + or 00 keeps its own country code;\n * anything else is read as a national number of the picked country. Parsing is format's parseGulfPhone.\n */\nexport function readGulfPhone(value: string, country: GulfCountry): GulfPhoneResult {\n  const typed = toWesternDigits(value).trim()\n  if (typed === '' || typed.startsWith('+') || typed.startsWith('00')) return parseGulfPhone(typed)\n  return parseGulfPhone(`+${GULF_DIAL_CODES[country]} ${typed}`)\n}\n\nexport type GulfPhoneInputProps = GulfFieldText<GulfPhoneError> & {\n  /** What was typed, with Western digits. */\n  value: string\n  onChange: (value: string) => void\n  /** format's result for the new value, on every change of number or country. */\n  onResult?: (result: GulfPhoneResult) => void\n  /** The picked country, when the app controls it. */\n  country?: GulfCountry\n  defaultCountry?: GulfCountry\n  onCountryChange?: (country: GulfCountry) => void\n  /** The accessible name of the country picker. */\n  countryLabel: string\n  /** Names shown before each code. Without one the ISO code shows, as in \"AE +971\". */\n  countryNames?: Partial<Record<GulfCountry, string>>\n  /** Digits on screen. The value is always Western. */\n  numerals?: Numerals\n}\n\nconst PHONE_MASK: Mask = {\n  normalize: (raw) => toWesternDigits(raw).replace(/[^\\d\\s()+.-]/g, '').slice(0, 20),\n  display: (value) => value,\n}\n\nconst SELECT = cx(\n  'border-input h-9 shrink-0 rounded-md border bg-transparent ps-2 pe-1 text-base shadow-xs outline-none md:text-sm',\n  'dark:bg-input/30 focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]',\n  'disabled:cursor-not-allowed disabled:opacity-50',\n)\n\n/** A GCC phone number: country picker and number, kept left to right as one unit in both languages. */\nexport function GulfPhoneInput(props: GulfPhoneInputProps) {\n  const {\n    value, onChange, onResult, country, defaultCountry = 'AE', onCountryChange, countryLabel, countryNames,\n    numerals = 'latn', label, help, error, errorMessages, id, name, placeholder, disabled, required, className,\n  } = props\n  const [ownCountry, setOwnCountry] = useState<GulfCountry>(defaultCountry)\n  const picked = country ?? ownCountry\n  const result = readGulfPhone(value, picked)\n  const shown = useShownError(result, error, errorMessages)\n  const mask: Mask = { ...PHONE_MASK, display: (v) => withNumerals(v, numerals) }\n  const input = useMaskedInput(value, mask, (next) => {\n    onChange(next)\n    onResult?.(readGulfPhone(next, picked))\n  })\n\n  const pickCountry = (next: GulfCountry) => {\n    if (country === undefined) setOwnCountry(next)\n    onCountryChange?.(next)\n    onResult?.(readGulfPhone(value, next))\n  }\n\n  return (\n    <GulfField id={id} label={label} help={help} error={shown.error} className={className}>\n      {(aria) => (\n        <div dir=\"ltr\" className=\"flex gap-2\">\n          <select aria-label={countryLabel} className={SELECT} value={picked} disabled={disabled}\n            onChange={(e) => pickCountry(e.currentTarget.value as GulfCountry)}>\n            {COUNTRIES.map((c) => (\n              <option key={c} value={c}>{`${countryNames?.[c] ?? c} +${GULF_DIAL_CODES[c]}`}</option>\n            ))}\n          </select>\n          <Input {...aria} {...input} dir=\"ltr\" type=\"tel\" inputMode=\"tel\" autoComplete=\"tel-national\"\n            name={name} placeholder={placeholder} disabled={disabled} required={required}\n            className={cx('text-start', LTR_PLACEHOLDER)} onBlur={shown.onBlur} />\n        </div>\n      )}\n    </GulfField>\n  )\n}\n"
    }
  ],
  "docs": "Needs a shadcn project (Tailwind and the shadcn CSS variables). Works with and without `shadcn init --rtl`: set dir=\"rtl\" on <html> or a parent for Arabic. Pass every label, help text and error message as props."
}
