'use client'; import { ChevronDown, X } from 'lucide-react'; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from '@/components/ui/command'; import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; import { cn } from '@/lib/utils'; export interface CountryOption { id: string; code: string; label: string; maxDigits: number; /** Path to the country flag asset inside the public directory. */ flagSrc: string; } export const FLAG_WIDTH = 24; export const FLAG_HEIGHT = 18; export const phoneCountries: CountryOption[] = [ { id: 'RU', code: '+7', label: 'Россия', maxDigits: 10, flagSrc: '/flags/ru.svg' }, { id: 'BY', code: '+375', label: 'Беларусь', maxDigits: 9, flagSrc: '/flags/by.svg' }, { id: 'KZ', code: '+7', label: 'Казахстан', maxDigits: 10, flagSrc: '/flags/kz.svg' } ]; function FlagIcon({ src, className }: { src: string; className?: string }) { return ( ); } export function formatPhoneNumber(digits: string, country: CountryOption) { const clean = digits.replace(/\D/g, '').slice(0, country.maxDigits); if (country.code === '+375') { const part1 = clean.slice(0, 2); const part2 = clean.slice(2, 5); const part3 = clean.slice(5, 7); const part4 = clean.slice(7, 9); if (clean.length <= 2) return part1 ? `(${part1}` : ''; if (clean.length <= 5) return `(${part1}) ${part2}`; if (clean.length <= 7) return `(${part1}) ${part2}-${part3}`; return `(${part1}) ${part2}-${part3}-${part4}`; } const part1 = clean.slice(0, 3); const part2 = clean.slice(3, 6); const part3 = clean.slice(6, 8); const part4 = clean.slice(8, 10); if (clean.length <= 3) return part1 ? `(${part1}` : ''; if (clean.length <= 6) return `(${part1}) ${part2}`; if (clean.length <= 8) return `(${part1}) ${part2}-${part3}`; return `(${part1}) ${part2}-${part3}-${part4}`; } export function toE164(country: CountryOption, digits: string) { return `${country.code}${digits.replace(/\D/g, '').slice(0, country.maxDigits)}`; } export function parseE164Phone(e164: string): { country: CountryOption; digits: string } { if (!e164) { return { country: phoneCountries[0], digits: '' }; } const sorted = [...phoneCountries].sort((a, b) => b.code.length - a.code.length); for (const country of sorted) { if (e164.startsWith(country.code)) { return { country, digits: e164.slice(country.code.length).replace(/\D/g, '') }; } } return { country: phoneCountries[0], digits: e164.replace(/\D/g, '') }; } const phoneThemes = { dark: { shell: 'border-[#555762] bg-transparent text-white focus-within:border-[#7b7f8f] focus-within:ring-white/10', trigger: 'text-white hover:bg-[#2a2c36]', chevron: 'text-[#8f92a0]', input: 'text-white placeholder:text-[#8f92a0]', clear: 'text-[#8f92a0] hover:bg-[#2a2c36] hover:text-white' }, light: { shell: 'border-[#eceef4] bg-white text-[#111827] focus-within:border-[#c7cad6] focus-within:ring-[#eceef4]', trigger: 'text-[#111827] hover:bg-[#f4f5f8]', chevron: 'text-[#667085]', input: 'text-[#111827] placeholder:text-[#667085]', clear: 'text-[#667085] hover:bg-[#f4f5f8] hover:text-[#111827]' } } as const; export function PhoneInput({ country, value, onCountryChange, onValueChange, className, variant = 'dark', required = true }: { country: CountryOption; value: string; onCountryChange: (country: CountryOption) => void; onValueChange: (digits: string) => void; className?: string; variant?: keyof typeof phoneThemes; required?: boolean; }) { const maskedValue = formatPhoneNumber(value, country); const theme = phoneThemes[variant]; return (