'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 PhoneInput({ country, value, onCountryChange, onValueChange, className }: { country: CountryOption; value: string; onCountryChange: (country: CountryOption) => void; onValueChange: (digits: string) => void; className?: string; }) { const maskedValue = formatPhoneNumber(value, country); return (