"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__ΩPhoneNumberNANP = exports.__ΩPhoneNumber = void 0;
exports.cleanPhone = cleanPhone;
exports.formatPhoneFriendly = formatPhoneFriendly;
function __assignType(fn, args) {
    fn.__type = args;
    return fn;
}
/*@ts-ignore*/
var { __ΩTypeAnnotation } = require('@deepkit/core');
/*@ts-ignore*/
var { __ΩValidate } = require('@deepkit/type');
const type_1 = require("@deepkit/type");
const google_libphonenumber_1 = require("google-libphonenumber");
const helpers_1 = require("../helpers");
const PhoneFormatter = google_libphonenumber_1.PhoneNumberUtil.getInstance();
const InvalidPhoneSymbol = '¡InvalidPhone¡'; //Symbol('InvalidPhone');
function cleanPhone(value, country = 'US') {
    const cleaned = cleanPhoneInternal(value, country);
    return cleaned === InvalidPhoneSymbol ? null : cleaned;
}
cleanPhone.__type = ['value', 'country', () => "US", 'cleanPhone', 'P&2!&2">#P&,J/$'];
function cleanPhoneInternal(value, country = 'US', stripUSPrefix = false) {
    if (typeof value !== 'string')
        return InvalidPhoneSymbol;
    const number = (0, helpers_1.tryOrErrorSync)(() => PhoneFormatter.parseAndKeepRawInput(value, country));
    if (number instanceof Error)
        return InvalidPhoneSymbol;
    if (!PhoneFormatter.isValidNumber(number))
        return InvalidPhoneSymbol;
    const result = PhoneFormatter.format(number, google_libphonenumber_1.PhoneNumberFormat.E164);
    return stripUSPrefix ? result.replace(/^\+1/, '') : result;
}
cleanPhoneInternal.__type = ['value', 'country', () => "US", 'stripUSPrefix', 'cleanPhoneInternal', 'P&2!&2">#"2$&/%'];
function formatPhoneFriendly(value, country) {
    const number = (0, helpers_1.tryOrErrorSync)(() => PhoneFormatter.parse(value, country));
    if (number instanceof Error)
        return null;
    if (!PhoneFormatter.isValidNumber(number))
        return null;
    return PhoneFormatter.format(number, google_libphonenumber_1.PhoneNumberFormat.NATIONAL);
}
formatPhoneFriendly.__type = ['value', 'country', 'formatPhoneFriendly', 'P&2!&2"8P&,J/#'];
function _validatePhone(value) {
    if (value === InvalidPhoneSymbol) {
        return new type_1.ValidatorError('invalidPhone', 'The phone number is invalid.');
    }
}
_validatePhone.__type = ['value', '_validatePhone', 'P#2!"/"'];
type_1.serializer.deserializeRegistry.addDecorator(__assignType(t => {
    const typeType = type_1.typeAnnotation.getType(t, 'dksf:type');
    return typeType?.kind === type_1.ReflectionKind.literal && typeType.literal === 'phone';
}, ['t', '', 'P"2!"/"']), __assignType((_type, state) => {
    state.setContext({ cleanPhoneInternal });
    state.addCodeForSetter(`
            ${state.setter} = cleanPhoneInternal(${state.accessor});
        `);
}, ['_type', 'state', '', 'P"2!"2""/#']));
type_1.serializer.deserializeRegistry.addDecorator(__assignType(t => {
    const typeType = type_1.typeAnnotation.getType(t, 'dksf:type');
    return typeType?.kind === type_1.ReflectionKind.literal && typeType.literal === 'phoneNanp';
}, ['t', '', 'P"2!"/"']), __assignType((_type, state) => {
    state.setContext({ cleanPhoneInternal });
    state.addCodeForSetter(`
            ${state.setter} = cleanPhoneInternal(${state.accessor}, 'US', true);
        `);
}, ['_type', 'state', '', 'P"2!"2""/#']));
const __ΩPhoneNumber = [() => __ΩValidate, () => _validatePhone, () => __ΩTypeAnnotation, "dksf:type", "phone", 'PhoneNumber', 'P&i"o!".$.%o##Kw&y'];
exports.__ΩPhoneNumber = __ΩPhoneNumber;
const __ΩPhoneNumberNANP = [() => __ΩValidate, () => _validatePhone, () => __ΩTypeAnnotation, "dksf:type", "phoneNanp", 'PhoneNumberNANP', 'P&i"o!".$.%o##Kw&y'];
exports.__ΩPhoneNumberNANP = __ΩPhoneNumberNANP;
//# sourceMappingURL=phone.js.map