"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UPSERT_ENDPOINT = exports.API_BASE = void 0;
exports.userProfilePayload = userProfilePayload;
exports.sendTrackEvent = sendTrackEvent;
exports.bulkUserProfilePayload = bulkUserProfilePayload;
exports.sendBulkTrackEvents = sendBulkTrackEvents;
exports.API_BASE = 'https://unification.useinsider.com/api/';
exports.UPSERT_ENDPOINT = 'user/v1/upsert';
const defaultAttributes = [
    'email',
    'phone',
    'age',
    'birthday',
    'name',
    'gender',
    'surname',
    'city',
    'country',
    'app_version',
    'idfa',
    'model',
    'last_ip',
    'carrier',
    'os_version',
    'platform',
    'timezone',
    'locale'
];
const defaultEvents = [
    'campaign_id',
    'campaign_name',
    'url',
    'product_id',
    'user_agent',
    'taxonomy',
    'name',
    'variant_id',
    'unit_sale_price',
    'unit_price',
    'quantity',
    'product_image_url',
    'event_group_id',
    'referrer',
    'currency'
];
function userProfilePayload(data) {
    const identifiers = {
        uuid: data.uuid,
        custom: {
            segment_anonymous_id: data.segment_anonymous_id
        }
    };
    if (data.custom_identifiers) {
        identifiers.custom = {
            ...identifiers.custom,
            ...data.custom_identifiers
        };
    }
    if (data.email_as_identifier) {
        identifiers['email'] = data.email;
    }
    if (data.phone_number_as_identifier) {
        identifiers['phone_number'] = data.phone;
    }
    return {
        users: [
            {
                identifiers,
                attributes: {
                    age: data.age,
                    birthday: data.birthday,
                    email: data.email,
                    name: data.firstName,
                    gender: data.gender,
                    surname: data.lastName,
                    phone_number: data.phone,
                    city: data.city,
                    country: data.country,
                    gdpr_optin: data.gdprOptin,
                    email_optin: data.emailOptin,
                    sms_optin: data.smsOptin,
                    whatsapp_optin: data.whatsappOptin,
                    language: data.language?.replace('-', '_'),
                    custom: data.custom
                },
                not_append: !data.append_arrays
            }
        ],
        platform: 'segment'
    };
}
function sendTrackEvent(data, event_name) {
    const addEventParameters = function (event, data, parameter) {
        parameter = parameter.toString().toLowerCase().trim().split(' ').join('_');
        if (parameter === 'taxonomy') {
            event.event_params[parameter] = [data[parameter]];
        }
        else if (defaultEvents.indexOf(parameter) > -1) {
            event.event_params[parameter] = data[parameter];
        }
        else if (data) {
            event.event_params.custom[parameter] = data[parameter];
        }
        return event;
    };
    const identifiers = {
        uuid: data.uuid,
        custom: {
            segment_anonymous_id: data.segment_anonymous_id
        }
    };
    if (data.custom_identifiers) {
        identifiers.custom = {
            ...identifiers.custom,
            ...data.custom_identifiers
        };
    }
    if (data.email_as_identifier && data?.attributes?.email) {
        identifiers['email'] = data?.attributes?.email;
    }
    if (data.phone_number_as_identifier && data?.attributes?.phone) {
        identifiers['phone_number'] = data?.attributes?.phone;
    }
    const payload = {
        identifiers,
        attributes: {
            custom: {}
        },
        events: []
    };
    for (const key of Object.keys(data.attributes || {})) {
        const attributeName = key.toString().toLowerCase().trim().split(' ').join('_').toString();
        if (attributeName === 'locale' && data.attributes) {
            payload.attributes[attributeName] = data.attributes[attributeName]
                ?.split('-')
                .join('_');
        }
        else if (defaultAttributes.indexOf(attributeName) > -1 && data.attributes) {
            payload.attributes[attributeName] = data.attributes[attributeName];
        }
        else if (data.attributes) {
            payload.attributes.custom[attributeName] =
                data.attributes[attributeName];
        }
    }
    let event = {
        event_name,
        timestamp: data.timestamp.toString(),
        event_params: {
            custom: {}
        }
    };
    for (const key of Object.keys(data.parameters || {})) {
        event = addEventParameters(event, data.parameters, key);
    }
    if (data.products) {
        for (const product of data.products) {
            let productEvent = {
                event_name,
                timestamp: data.timestamp.toString(),
                event_params: {
                    custom: {}
                }
            };
            for (const key of Object.keys(product || {})) {
                productEvent = addEventParameters(productEvent, product, key);
            }
            for (const key of Object.keys(data.parameters || {})) {
                productEvent = addEventParameters(productEvent, data.parameters, key);
            }
            payload.events.push(productEvent);
        }
    }
    else {
        payload.events.push(event);
    }
    payload.not_append = !data.append_arrays;
    return { users: [payload], platform: 'segment' };
}
function bulkUserProfilePayload(data) {
    const batchPayload = data.map((userPayload) => {
        const not_append = !userPayload.append_arrays;
        const identifiers = {
            uuid: userPayload.uuid,
            custom: {
                segment_anonymous_id: userPayload.segment_anonymous_id,
                ...userPayload.custom_identifiers
            }
        };
        if (userPayload.email_as_identifier) {
            identifiers['email'] = userPayload.email;
        }
        if (userPayload.phone_number_as_identifier) {
            identifiers['phone_number'] = userPayload.phone;
        }
        const attributes = {
            age: userPayload.age,
            birthday: userPayload.birthday,
            email: userPayload.email,
            name: userPayload.firstName,
            gender: userPayload.gender,
            surname: userPayload.lastName,
            phone_number: userPayload.phone,
            city: userPayload.city,
            country: userPayload.country,
            gdpr_optin: userPayload.gdprOptin,
            email_optin: userPayload.emailOptin,
            sms_optin: userPayload.smsOptin,
            whatsapp_optin: userPayload.whatsappOptin,
            language: userPayload.language?.replace('-', '_'),
            custom: userPayload.custom
        };
        Object.keys(attributes).forEach((key) => {
            if (attributes[key] === undefined) {
                delete attributes[key];
            }
        });
        Object.keys(identifiers).forEach((key) => {
            if (identifiers[key] === undefined) {
                delete identifiers[key];
            }
        });
        return { identifiers, attributes, not_append };
    });
    return { users: batchPayload, platform: 'segment' };
}
function sendBulkTrackEvents(dataArray, event_name) {
    const bulkPayload = [];
    dataArray.forEach((data) => {
        const addEventParameters = function (event, data, parameter) {
            parameter = parameter.toString().toLowerCase().trim().split(' ').join('_');
            if (parameter === 'taxonomy') {
                event.event_params[parameter] = [data[parameter]];
            }
            else if (defaultEvents.indexOf(parameter) > -1) {
                event.event_params[parameter] = data[parameter];
            }
            else if (data) {
                event.event_params.custom[parameter] = data[parameter];
            }
            return event;
        };
        const identifiers = {
            uuid: data.uuid,
            custom: {
                segment_anonymous_id: data.segment_anonymous_id
            }
        };
        if (data.custom_identifiers) {
            identifiers.custom = {
                ...identifiers.custom,
                ...data.custom_identifiers
            };
        }
        if (data.email_as_identifier && data?.attributes?.email) {
            identifiers['email'] = data?.attributes?.email;
        }
        if (data.phone_number_as_identifier && data?.attributes?.phone) {
            identifiers['phone_number'] = data?.attributes?.phone;
        }
        const payload = {
            identifiers,
            attributes: {
                custom: {}
            },
            not_append: true,
            events: []
        };
        for (const key of Object.keys(data.attributes || {})) {
            const attributeName = key.toString().toLowerCase().trim().split(' ').join('_').toString();
            if (attributeName === 'locale' && data.attributes) {
                payload.attributes[attributeName] = data.attributes[attributeName]
                    ?.split('-')
                    .join('_');
            }
            else if (defaultAttributes.indexOf(attributeName) > -1 && data.attributes) {
                payload.attributes[attributeName] = data.attributes[attributeName];
            }
            else if (data.attributes) {
                payload.attributes.custom[attributeName] =
                    data.attributes[attributeName];
            }
        }
        const eventName = event_name || data.event_name.toString().toLowerCase().trim().split(' ').join('_').toString();
        let event = {
            event_name: eventName,
            timestamp: data.timestamp.toString(),
            event_params: {
                custom: {}
            }
        };
        for (const key of Object.keys(data.parameters || {})) {
            event = addEventParameters(event, data.parameters, key);
        }
        if (data.products) {
            for (const product of data.products) {
                let productEvent = {
                    event_name: eventName,
                    timestamp: data.timestamp.toString(),
                    event_params: {
                        custom: {}
                    }
                };
                for (const key of Object.keys(product || {})) {
                    productEvent = addEventParameters(productEvent, product, key);
                }
                for (const key of Object.keys(data.parameters || {})) {
                    productEvent = addEventParameters(productEvent, data.parameters, key);
                }
                payload.events.push(productEvent);
            }
        }
        else {
            payload.events.push(event);
        }
        payload.not_append = !data.append_arrays;
        bulkPayload.push(payload);
    });
    return { users: bulkPayload, platform: 'segment' };
}
//# sourceMappingURL=insider-helpers.js.map