import parsePhoneNumber, { AsYouType, formatIncompletePhoneNumber } from 'libphonenumber-js';
export const getPhoneNumberParts = (phoneNumber, codesList) => {
    if (!phoneNumber.startsWith('+') || phoneNumber.startsWith('+0'))
        return [undefined, phoneNumber];
    const countryCode = codesList.find(code => phoneNumber.startsWith(code));
    const subscriberNumber = phoneNumber.substring(countryCode?.length || 0);
    return [countryCode, subscriberNumber];
};
export const cleanSubscriberNumber = (phoneNumber) => {
    return phoneNumber.replace(/\D/g, '');
};
export const getCountry = (countryCode, number) => {
    const asYouTypeCode = new AsYouType();
    asYouTypeCode.input(countryCode + number);
    return countryCode === '+1' ? 'US' : asYouTypeCode.getNumber()?.country;
};
export const formatPhoneNumber = (numberToFormat, countryCode = '', showCountryCode = false) => {
    let formatted = cleanSubscriberNumber(numberToFormat);
    if (countryCode) {
        const country = getCountry(countryCode, formatted);
        formatted = formatIncompletePhoneNumber(showCountryCode ? countryCode + formatted : formatted, country);
        if (formatted.startsWith(countryCode)) {
            formatted = formatted.substring(countryCode.length).trim();
        }
    }
    return formatted;
};
export const convertToCallbackParameter = (codePart, numberPart) => {
    return [codePart, cleanSubscriberNumber(numberPart)].join('');
};
export const formatPhone = (value, { nationalFormat } = {}) => {
    return nationalFormat
        ? parsePhoneNumber(value)?.formatNational()
        : parsePhoneNumber(value)?.formatInternational();
};
export default getPhoneNumberParts;
//# sourceMappingURL=utils.js.map