"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processPayload = exports.DELETE_ATTRIBUTE_ENDPOINT = exports.UPSERT_ENDPOINT = exports.API_BASE = void 0;
exports.API_BASE = 'https://unification.useinsider.com/api/';
exports.UPSERT_ENDPOINT = 'user/v1/upsert';
exports.DELETE_ATTRIBUTE_ENDPOINT = 'user/v1/attribute/delete';
const AUDIENCE_TYPE = 'audience';
const IDENTIFY = 'identify';
const TRACK = 'track';
const COMPUTED_TRAIT_TYPE = 'trait';
class PayloadValidationError extends Error {
    constructor(message) {
        super(message);
        this.name = 'PayloadValidationError';
    }
}
const computedTraitsPayloadForIdentifyCall = function (data, request) {
    const identifiers = getIdentifiers(data);
    const attributes = getTraitAttributes(data);
    const payload = {
        users: [
            {
                identifiers,
                attributes
            }
        ],
        not_append: !data.append_arrays,
        platform: 'segment'
    };
    return request(exports.API_BASE + exports.UPSERT_ENDPOINT, {
        method: 'POST',
        json: payload
    });
};
const computedTraitsPayloadForTrackCall = function (data, request) {
    const identifiers = getIdentifiers(data);
    const events = getEvents(data);
    const payload = {
        users: [
            {
                identifiers,
                events
            }
        ],
        not_append: !data.append_arrays,
        platform: 'segment'
    };
    return request(exports.API_BASE + exports.UPSERT_ENDPOINT, {
        method: 'POST',
        json: payload
    });
};
const computedAudiencesPayloadForIdentifyCall = function (data, request) {
    if (data.traits_or_props[data.custom_audience_name] === false) {
        return request(exports.API_BASE + exports.DELETE_ATTRIBUTE_ENDPOINT, {
            method: 'POST',
            json: deleteAttributePartial(data)
        });
    }
    const identifiers = getIdentifiers(data);
    const attributes = getAttributes(data);
    const payload = {
        users: [
            {
                identifiers,
                attributes
            }
        ],
        not_append: !data.append_arrays,
        platform: 'segment'
    };
    return request(exports.API_BASE + exports.UPSERT_ENDPOINT, {
        method: 'POST',
        json: payload
    });
};
const computedAudiencePayloadForTrackCall = function (data, request) {
    const identifiers = getIdentifiers(data);
    const events = getEvents(data);
    const payload = {
        users: [
            {
                identifiers,
                events
            }
        ],
        not_append: !data.append_arrays,
        platform: 'segment'
    };
    return request(exports.API_BASE + exports.UPSERT_ENDPOINT, {
        method: 'POST',
        json: payload
    });
};
const deleteAttributePartial = function (data) {
    const identifiers = getIdentifiers(data);
    const computationKey = data.custom_audience_name;
    return {
        users: [
            {
                identifiers,
                custom: {
                    partial: {
                        segment_audience_name: [computationKey]
                    }
                }
            }
        ],
        not_append: !data.append_arrays,
        platform: 'segment'
    };
};
const getIdentifiers = function (data) {
    const identifiers = {};
    if (data.email) {
        identifiers['email'] = data.email;
    }
    if (data.user_id) {
        identifiers['uuid'] = data.user_id;
    }
    if (data.anonymous_id) {
        identifiers['custom'] = {
            segment_anonymous_id: data.anonymous_id
        };
    }
    if (data.phone) {
        identifiers['phone_number'] = data.phone;
    }
    return identifiers;
};
const getAttributes = function (data) {
    const computationKey = data.custom_audience_name;
    return {
        custom: {
            segment_audience_name: [computationKey]
        }
    };
};
const getTraitAttributes = function (data) {
    const computationKey = 'segment_io_' + data.custom_audience_name;
    const attributes = { custom: {} };
    attributes.custom[computationKey] = data.traits_or_props[data.custom_audience_name];
    return attributes;
};
const getEvents = function (data) {
    return [
        {
            event_name: data.event_name?.toLowerCase().replace(/ /g, '_'),
            timestamp: data.timestamp,
            event_params: {
                custom: {
                    segment_engage_name: data.custom_audience_name
                }
            }
        }
    ];
};
const handleAudienceData = async function (data, request) {
    if (data.event_type === IDENTIFY) {
        return computedAudiencesPayloadForIdentifyCall(data, request);
    }
    if (data.event_type === TRACK) {
        return computedAudiencePayloadForTrackCall(data, request);
    }
    throw new PayloadValidationError('API call must be a track() or identify() call');
};
const handleComputationData = async function (data, request) {
    if (data.event_type === IDENTIFY) {
        return computedTraitsPayloadForIdentifyCall(data, request);
    }
    if (data.event_type === TRACK) {
        return computedTraitsPayloadForTrackCall(data, request);
    }
    throw new PayloadValidationError('API call must be a track() or identify() call');
};
const processPayload = async function (request, payload) {
    const promises = payload.map((data) => {
        if (data.segment_computation_action === AUDIENCE_TYPE) {
            return handleAudienceData(data, request);
        }
        if (data.segment_computation_action === COMPUTED_TRAIT_TYPE) {
            return handleComputationData(data, request);
        }
        throw new PayloadValidationError('API call must be an Audience or Computed Trait track() or identify() call');
    });
    return Promise.all(promises);
};
exports.processPayload = processPayload;
//# sourceMappingURL=insider-helpers.js.map