"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.freshsalesMobilePhoneToE164 = exports.FreshSalesMobilePhoneToE164ErrorMessage = void 0;
const lodash_1 = require("lodash");
const extensions_core_1 = require("@awell-health/extensions-core");
exports.FreshSalesMobilePhoneToE164ErrorMessage = "Could not convert the lead's phone number to the E164 format.";
/**
 * Converts a mobile phone number from the Freshsales format to the E164 format.
 *
 * Example:
 * 1-926-652-9503 -> +19266529503
 *
 * @param phone - The mobile phone number to convert.
 * @returns The E164 formatted phone number.
 */
const freshsalesMobilePhoneToE164 = (phone) => {
    if ((0, lodash_1.isNil)(phone) || (0, lodash_1.isEmpty)(phone))
        return undefined;
    /**
     * If the phone number is already in the E164 format, return it as is.
     */
    const initialResult = extensions_core_1.E164PhoneValidationSchema.safeParse(phone);
    if (initialResult.success)
        return initialResult.data;
    /**
     * If it's not in the E164 format, we'll attempt to clean it up.
     */
    const cleanedPhone = phone.replace(/\D/g, '');
    const withInternationalPrefix = `+${cleanedPhone}`;
    extensions_core_1.E164PhoneValidationSchema.parse(withInternationalPrefix, {
        errorMap: () => ({
            message: exports.FreshSalesMobilePhoneToE164ErrorMessage,
        }),
    });
    return withInternationalPrefix;
};
exports.freshsalesMobilePhoneToE164 = freshsalesMobilePhoneToE164;
//# sourceMappingURL=freshsalesMobilePhoneToE164.js.map