// Country-specific validation removed in favor of a common rule
export const countryPhoneValidation: Record<string, { length: number; pattern?: RegExp; name: string }> = {};

/** App-wide default international dial code for phone fields (E.164-style with +). */
export const DEFAULT_PHONE_COUNTRY_CODE = '+971';

/**
 * Maps E.164 dial codes to ISO 3166-1 alpha-2 country codes for `react-phone-input-2`'s `country` prop.
 * Where multiple countries share a code, the primary market used in this app is chosen (e.g. +1 → us).
 */
export const DIAL_CODE_TO_ISO2: Record<string, string> = {
    '+1': 'us',
    '+7': 'ru',
    '+20': 'eg',
    '+27': 'za',
    '+30': 'gr',
    '+31': 'nl',
    '+32': 'be',
    '+33': 'fr',
    '+34': 'es',
    '+36': 'hu',
    '+39': 'it',
    '+40': 'ro',
    '+41': 'ch',
    '+43': 'at',
    '+44': 'gb',
    '+45': 'dk',
    '+46': 'se',
    '+47': 'no',
    '+48': 'pl',
    '+49': 'de',
    '+51': 'pe',
    '+52': 'mx',
    '+53': 'cu',
    '+54': 'ar',
    '+55': 'br',
    '+56': 'cl',
    '+57': 'co',
    '+58': 've',
    '+60': 'my',
    '+61': 'au',
    '+62': 'id',
    '+63': 'ph',
    '+64': 'nz',
    '+65': 'sg',
    '+66': 'th',
    '+81': 'jp',
    '+82': 'kr',
    '+84': 'vn',
    '+86': 'cn',
    '+90': 'tr',
    '+91': 'in',
    '+92': 'pk',
    '+93': 'af',
    '+94': 'lk',
    '+95': 'mm',
    '+98': 'ir',
    '+212': 'ma',
    '+213': 'dz',
    '+216': 'tn',
    '+218': 'ly',
    '+234': 'ng',
    '+254': 'ke',
    '+351': 'pt',
    '+353': 'ie',
    '+354': 'is',
    '+355': 'al',
    '+356': 'mt',
    '+357': 'cy',
    '+358': 'fi',
    '+359': 'bg',
    '+370': 'lt',
    '+371': 'lv',
    '+372': 'ee',
    '+380': 'ua',
    '+381': 'rs',
    '+385': 'hr',
    '+386': 'si',
    '+420': 'cz',
    '+421': 'sk',
    '+852': 'hk',
    '+853': 'mo',
    '+855': 'kh',
    '+880': 'bd',
    '+886': 'tw',
    '+966': 'sa',
    '+971': 'ae',
    '+972': 'il',
    '+973': 'bh',
    '+974': 'qa',
    '+975': 'bt',
    '+977': 'np',
    '+992': 'tj',
    '+993': 'tm',
    '+994': 'az',
    '+995': 'ge',
    '+996': 'kg',
    '+998': 'uz',
};

const DIAL_CODE_KEYS_BY_LENGTH = Object.keys(DIAL_CODE_TO_ISO2).sort((a, b) => b.length - a.length);

/** Ensures a leading + and digits only; empty/invalid input falls back to {@link DEFAULT_PHONE_COUNTRY_CODE}. */
export function normalizeDialCode(input: string | undefined | null): string {
    const raw = (input ?? '').trim();
    if (!raw) return DEFAULT_PHONE_COUNTRY_CODE;
    const digits = raw.replace(/^\+/, '').replace(/\D/g, '');
    if (!digits) return DEFAULT_PHONE_COUNTRY_CODE;
    return `+${digits}`;
}

/** ISO country for an already-normalized dial code (see {@link normalizeDialCode}). */
export function isoCountryFromNormalizedDial(normalizedDial: string): string {
    const direct = DIAL_CODE_TO_ISO2[normalizedDial];
    if (direct) return direct;
    for (const key of DIAL_CODE_KEYS_BY_LENGTH) {
        if (normalizedDial.startsWith(key)) return DIAL_CODE_TO_ISO2[key]!;
    }
    return 'us';
}

/** Resolves ISO country for `react-phone-input-2`; falls back to `us` when unknown (library still respects `value`). */
export function dialCodeToIsoCountry(dialCode: string | undefined | null): string {
    return isoCountryFromNormalizedDial(normalizeDialCode(dialCode));
}

export const validatePhoneNumber = (phone: string, _countryCode?: string): string => {
    const clean = (phone || '').replace(/\D/g, '');
    if (clean.length === 0) return 'Phone number is required';
    if (clean.length < 9 || clean.length > 15) return 'Phone number must be 9 to 15 digits';
    return '';
};

export const DEFAULT_ONLY_COUNTRIES = ['us', 'in', 'gb', 'au', 'cn', 'de', 'fr', 'jp', 'ru', 'ae'];
// Allow all codes by default (no restriction)
export const DEFAULT_VALID_CODES: string[] = [];
