import { getCountryFromLocale } from '../../../common/locale';
import { findCountryByCode } from '../findCountryByCode';

/**
 * Default phone code, the UK one `+44`
 */
const DEFAULT_PHONE_CODE = '+44';

/**
 * Given a valid locale it returns the correspondent prefix if found or +44 otherwise.
 *
 * @param locale BCP 47 language tag of locale, e.g. `"es-ES"`.
 * @param countryCode Two-letter country code (ISO 3166-1 alpha-2).
 */
export const setDefaultPrefix = (locale: string, countryCode?: string) => {
  const country =
    (countryCode != null ? findCountryByCode(countryCode) : null) ??
    findCountryByCode(getCountryFromLocale(locale) ?? locale);
  return country?.phone ?? DEFAULT_PHONE_CODE;
};
