{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "country-select",
  "title": "Country Select",
  "description": "Searchable country picker with ISO 3166-1 codes, localized names from Intl.DisplayNames, preferred countries listed first, and optional flags.",
  "dependencies": [
    "country-flag-icons@^1.6.14"
  ],
  "registryDependencies": [
    "https://dentil.dev/r/select-search.json?via=country-select"
  ],
  "files": [
    {
      "path": "components/dentil/country-select/countries.ts",
      "content": "/** ISO 3166-1 alpha-2 codes, plus XK (Kosovo). Names come from `Intl.DisplayNames`, so they follow the locale. */\n// prettier-ignore\nexport const countryCodes = [\n  'AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ',\n  'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BQ', 'BR', 'BS',\n  'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN',\n  'CO', 'CR', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE',\n  'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF',\n  'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM',\n  'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM',\n  'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC',\n  'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MF', 'MG', 'MH', 'MK',\n  'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA',\n  'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG',\n  'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW',\n  'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'SS',\n  'ST', 'SV', 'SX', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO',\n  'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI',\n  'VN', 'VU', 'WF', 'WS', 'XK', 'YE', 'YT', 'ZA', 'ZM', 'ZW',\n] as const;\n\nexport interface Country {\n  code: string;\n  name: string;\n}\n\nconst cache = new Map<string, Country[]>();\n\n/** Every country, named in `locale` and sorted by name. Cached per locale. */\nexport function getCountries(locale = 'en'): Country[] {\n  const cached = cache.get(locale);\n  if (cached) return cached;\n\n  const names = new Intl.DisplayNames([locale], { type: 'region', fallback: 'code' });\n  const countries = countryCodes\n    .map((code) => ({ code, name: names.of(code) ?? code }))\n    .sort((a, b) => a.name.localeCompare(b.name, locale));\n  cache.set(locale, countries);\n  return countries;\n}\n\nexport function getCountryName(code: string, locale = 'en'): string {\n  return new Intl.DisplayNames([locale], { type: 'region', fallback: 'code' }).of(code) ?? code;\n}\n",
      "type": "registry:lib",
      "target": "components/dentil/country-select/countries.ts"
    },
    {
      "path": "components/dentil/country-select/country-select.tsx",
      "content": "'use client';\n\nimport * as flagIcons from 'country-flag-icons/react/3x2';\nimport * as React from 'react';\n\nimport { SelectSearch, type SelectSearchOption, type SelectSearchProps } from '../select-search/select-search';\nimport { getCountries, type Country } from './countries';\n\ntype FlagIcon = (props: { title?: string; className?: string }) => React.JSX.Element;\n\nconst flags = flagIcons as Record<string, FlagIcon>;\n\nfunction CountryFlag({ code, name }: { code: string; name: string }) {\n  const Flag = flags[code];\n  return (\n    <span className=\"flex h-3.5 w-5 shrink-0 overflow-hidden rounded-[2px]\" aria-hidden=\"true\">\n      {Flag ? <Flag title={name} className=\"size-full\" /> : <span className=\"bg-muted size-full\" />}\n    </span>\n  );\n}\n\nexport interface CountrySelectProps extends Omit<SelectSearchProps, 'options' | 'value' | 'onChange' | 'groups'> {\n  /** ISO 3166-1 alpha-2 code, e.g. `'GB'`. */\n  value: string | null | undefined;\n  onChange: (value: string) => void;\n  /** Only list these codes. Lists every country when omitted. */\n  allowedCountries?: string[];\n  /** ISO codes listed first, in this order, ahead of the alphabetical list. */\n  preferredCountries?: string[];\n  /** Draw a flag beside each country, in the list and on the trigger. */\n  showFlags?: boolean;\n  /** Locale for country names. */\n  locale?: string;\n}\n\nfunction partitionCountries(countries: Country[], preferredCountries?: string[]) {\n  const rank = new Map<string, number>();\n  for (const code of preferredCountries ?? []) {\n    const key = code.toUpperCase();\n    if (!rank.has(key)) rank.set(key, rank.size);\n  }\n  if (rank.size === 0) return { preferred: [], rest: countries };\n\n  const preferred: Country[] = [];\n  const rest: Country[] = [];\n  for (const country of countries) {\n    if (rank.has(country.code)) preferred.push(country);\n    else rest.push(country);\n  }\n  preferred.sort((a, b) => (rank.get(a.code) ?? 0) - (rank.get(b.code) ?? 0));\n  return { preferred, rest };\n}\n\nexport function CountrySelect({\n  value,\n  onChange,\n  allowedCountries,\n  preferredCountries,\n  showFlags = false,\n  locale = 'en',\n  placeholder = 'Select a country...',\n  searchPlaceholder = 'Search countries...',\n  emptyText = 'No country found.',\n  ...props\n}: CountrySelectProps) {\n  const { options, groups } = React.useMemo(() => {\n    const countries = getCountries(locale).filter(\n      (country) => !allowedCountries || allowedCountries.includes(country.code)\n    );\n    const { preferred, rest } = partitionCountries(countries, preferredCountries);\n    const toOption = (country: Country): SelectSearchOption => ({\n      label: country.name,\n      value: country.code,\n      icon: showFlags ? <CountryFlag code={country.code} name={country.name} /> : undefined,\n    });\n    const preferredOptions = preferred.map(toOption);\n    const restOptions = rest.map(toOption);\n    return {\n      options: [...preferredOptions, ...restOptions],\n      groups: preferredOptions.length > 0 && restOptions.length > 0 ? [preferredOptions, restOptions] : undefined,\n    };\n  }, [locale, allowedCountries, preferredCountries, showFlags]);\n\n  return (\n    <SelectSearch\n      data-slot=\"country-select\"\n      options={options}\n      value={value}\n      onChange={onChange}\n      placeholder={placeholder}\n      searchPlaceholder={searchPlaceholder}\n      emptyText={emptyText}\n      {...props}\n      groups={groups}\n    />\n  );\n}\n",
      "type": "registry:component",
      "target": "components/dentil/country-select/country-select.tsx"
    }
  ],
  "type": "registry:component"
}
