"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generate_jwt = generate_jwt;
exports.validate_phone = validate_phone;
exports.gen_update_segment_payload = gen_update_segment_payload;
const constants_1 = require("./constants");
const hashing_utils_1 = require("../../lib/hashing-utils");
async function generate_jwt(client_identifier, request) {
    const url = `${constants_1.DELIVRAI_BASE_URL}${constants_1.DELIVRAI_GET_TOKEN}?client_identifier=${client_identifier}`;
    return await request(url, {
        method: 'GET'
    })
        .then((response) => {
        return response;
    })
        .catch((error) => {
        return error.response?.code;
    });
}
function validate_phone(phone) {
    const phone_num = phone.replace(/\D/g, '');
    if (phone_num.length <= 15 && phone_num.length >= 1) {
        return '+' + phone_num;
    }
    else {
        return '';
    }
}
function gen_update_segment_payload(payloads, client_identifier_id) {
    const data_groups = {};
    const data = [];
    let audience_key = '';
    for (const event of payloads) {
        let hashed_email = '';
        if (event.email) {
            hashed_email = (0, hashing_utils_1.processHashing)(event.email.toLowerCase(), 'sha256', 'hex');
        }
        let idfa = '';
        let gpsaid = '';
        if (event.advertising_id) {
            if (event.device_type) {
                switch (event.device_type) {
                    case 'ios':
                        idfa = event.advertising_id;
                        break;
                    case 'android':
                        gpsaid = event.advertising_id;
                        break;
                }
            }
            else {
                if (event.advertising_id === event.advertising_id.toUpperCase()) {
                    idfa = event.advertising_id;
                }
                else {
                    gpsaid = event.advertising_id;
                }
            }
        }
        let hashed_phone = '';
        if (event.phone) {
            const phone = validate_phone(event.phone);
            if (phone !== '') {
                hashed_phone = phone;
            }
        }
        if (hashed_email === '' && idfa === '' && gpsaid === '' && hashed_phone === '') {
            continue;
        }
        const ts = Math.floor(new Date().getTime() / 1000);
        let exp;
        const seg_id = event.segment_audience_id || '';
        audience_key = seg_id;
        const group_key = `${hashed_email}|${idfa}|${gpsaid}|${hashed_phone}`;
        if (!(group_key in data_groups)) {
            data_groups[group_key] = [];
        }
        data_groups[group_key].push({
            exp: String(exp),
            seg_id: seg_id,
            ts: String(ts)
        });
    }
    for (const [key] of Object.entries(data_groups)) {
        const [hashed_email, idfa, gpsaid, hashed_phone] = key.split('|');
        data.push({ email: hashed_email, advertising_id_ios: idfa, advertising_id_android: gpsaid, phone: hashed_phone });
    }
    const delivr_ai_payload = {
        audience_key: audience_key,
        data: data,
        client_identifier_id: client_identifier_id
    };
    return delivr_ai_payload;
}
//# sourceMappingURL=utils-rt.js.map