"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__tsfTypeAliases = void 0;
exports.cleanPhone = cleanPhone;
exports.formatPhoneFriendly = formatPhoneFriendly;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-server-foundation/type-metadata-runtime"));
const reflection_1 = require("../reflection");
const google_libphonenumber_1 = require("google-libphonenumber");
const type_annotations_1 = require("./type-annotations");
const phoneFormatter = google_libphonenumber_1.PhoneNumberUtil.getInstance();
const InvalidPhoneSymbol = '¡InvalidPhone¡';
function cleanPhone(value, country = 'US') {
    const cleaned = cleanPhoneInternal(value, country);
    return cleaned === InvalidPhoneSymbol ? null : cleaned;
}
function cleanPhoneInternal(value, country = 'US', stripUSPrefix = false) {
    if (typeof value !== 'string')
        return InvalidPhoneSymbol;
    const number = 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);
    if (!stripUSPrefix)
        return result;
    return result.startsWith('+1') ? result.slice(2) : InvalidPhoneSymbol;
}
function formatPhoneFriendly(value, country) {
    const number = 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);
}
function validatePhone(value) {
    if (value === InvalidPhoneSymbol) {
        return new reflection_1.ValidatorError('invalidPhone', 'The phone number is invalid.');
    }
}
reflection_1.validationRegistry.register('phone', validatePhone);
reflection_1.validationRegistry.register('phoneNanp', validatePhone);
reflection_1.deserializer.addDecorator(t => {
    const typeType = (0, type_annotations_1.getFirstTypeAnnotation)(t, 'tsf:type');
    return typeType?.kind === reflection_1.ReflectionKind.literal && typeType.literal === 'phone';
}, (_type, state) => {
    state.addTransform(value => cleanPhoneInternal(value));
});
reflection_1.deserializer.addDecorator(t => {
    const typeType = (0, type_annotations_1.getFirstTypeAnnotation)(t, 'tsf:type');
    return typeType?.kind === reflection_1.ReflectionKind.literal && typeType.literal === 'phoneNanp';
}, (_type, state) => {
    state.addTransform(value => cleanPhoneInternal(value, 'US', true));
});
function tryOrErrorSync(fn) {
    try {
        return fn();
    }
    catch (error) {
        return error instanceof Error ? error : new Error(String(error));
    }
}
exports.__tsfTypeAliases = _a.decodeCompactMetadataV1("[1,{\"PhoneNumber\":{\"kind\":13,\"types\":[{\"kind\":6},{\"kind\":2,\"typeName\":\"Validator\",\"validation\":[{\"name\":\"validator\",\"args\":[{\"kind\":10,\"literal\":\"phone\"}]}]},{\"kind\":2,\"typeName\":\"TypeAnnotation\",\"annotations\":{\"tsf:type\":{\"kind\":10,\"literal\":\"phone\"}}}],\"typeName\":\"PhoneNumber\"},\"PhoneNumberNANP\":{\"kind\":13,\"types\":[{\"kind\":6},{\"kind\":2,\"typeName\":\"Validator\",\"validation\":[{\"name\":\"validator\",\"args\":[{\"kind\":10,\"literal\":\"phoneNanp\"}]}]},{\"kind\":2,\"typeName\":\"TypeAnnotation\",\"annotations\":{\"tsf:type\":{\"kind\":10,\"literal\":\"phoneNanp\"}}}],\"typeName\":\"PhoneNumberNANP\"}}]", []);
//# sourceMappingURL=phone.js.map