"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatPhones = exports.formatAdvertisingId = exports.formatEmails = void 0;
exports.formatUserIds = formatUserIds;
exports.formatString = formatString;
exports.formatAddress = formatAddress;
const hashing_utils_1 = require("../../../lib/hashing-utils");
const formatEmails = (email_addresses) => {
    const result = [];
    if (email_addresses) {
        email_addresses.forEach((email) => {
            result.push(hashAndEncode(email.toLowerCase()));
        });
    }
    return result;
};
exports.formatEmails = formatEmails;
const formatAdvertisingId = (id, lowerCase) => {
    if (!id)
        return undefined;
    const processed = lowerCase ? id.toLocaleLowerCase().trim() : id.trim();
    return hashAndEncode(processed);
};
exports.formatAdvertisingId = formatAdvertisingId;
const formatPhones = (phone_numbers) => {
    const result = [];
    if (!phone_numbers)
        return result;
    phone_numbers.forEach((phone) => {
        result.push(hashAndEncode(phone, cleanPhoneNumber));
    });
    return result;
};
exports.formatPhones = formatPhones;
const cleanPhoneNumber = (phone) => {
    const validatedPhone = phone.match(/[0-9]{0,14}/g);
    if (validatedPhone === null) {
        throw new Error(`${phone} is not a valid E.164 phone number.`);
    }
    const formattedPhone = `+${phone.replace(/[^0-9]/g, '')}`;
    return formattedPhone.substring(0, 15);
};
function formatUserIds(userIds) {
    const result = [];
    if (userIds) {
        userIds.forEach((userId) => {
            result.push(hashAndEncode(userId.toLowerCase()));
        });
    }
    return result;
}
function formatString(str) {
    if (!str)
        return '';
    return hashAndEncode(str.replace(/\s/g, '').toLowerCase());
}
function formatAddress(address) {
    if (!address)
        return '';
    return address.replace(/[^A-Za-z0-9]/g, '').toLowerCase();
}
function hashAndEncode(property, cleaningFunction) {
    return (0, hashing_utils_1.processHashing)(property, 'sha256', 'hex', cleaningFunction);
}
//# sourceMappingURL=formatter.js.map