import countries from "i18n-iso-countries";
import ru from "i18n-iso-countries/langs/ru.json";
import { type CountryCode, getCountries } from "libphonenumber-js";
import getFlagEmoji from "@/components/ui/PhoneInput/utils/getFlagEmoji";
import { getCountryCallingCode } from "libphonenumber-js/max";
import { Country } from "@/components/ui/PhoneInput/types";

countries.registerLocale(ru);

const libIsoCountries = countries.getNames("ru", { select: "official" });
const libPhoneNumberCountries = getCountries();

const countryOptionsDataMap = Object.fromEntries(
    libPhoneNumberCountries
        .map(code => {
            const name = libIsoCountries[code];
            const emoji = getFlagEmoji(code);
            if (!name || !emoji) return null;
            const callingCode = getCountryCallingCode(code);
            return [
                code,
                {
                    code,
                    name,
                    emoji,
                    callingCode,
                },
            ] as [CountryCode, Country];
        })
        .filter(o => !!o)
);

export default countryOptionsDataMap;
