"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeEmpty = removeEmpty;
exports.getEndpoint = getEndpoint;
exports.sendPostEvent = sendPostEvent;
exports.sendBatchedPostEvent = sendBatchedPostEvent;
const actions_core_1 = require("@segment/actions-core");
function removeEmpty(obj) {
    if (!obj) {
        return obj;
    }
    const cleaned = (0, actions_core_1.removeUndefined)(obj);
    if (typeof cleaned === 'object' && Object.keys(cleaned).length > 0) {
        return cleaned;
    }
    return undefined;
}
function getEndpoint(url) {
    return 'https://integration-segment.svc-nue.pushwoosh.com' + url;
}
function sendPostEvent(request, settings, payload) {
    const endpoint = getEndpoint('/integration-segment/v1/post-events');
    return request(endpoint, {
        method: 'post',
        json: {
            events: [
                {
                    user_id: payload.external_id,
                    device_id: payload.device_id,
                    device_platform: payload.device_platform,
                    app_code: settings.pushwooshAppCode,
                    name: payload.name,
                    timestamp: payload.timestamp,
                    attributes: payload.properties
                }
            ]
        }
    });
}
function sendBatchedPostEvent(request, settings, payloads) {
    const payload = payloads.map((payload) => {
        return {
            user_id: payload.external_id,
            device_id: payload.device_id,
            device_platform: payload.device_platform,
            app_id: settings.pushwooshAppCode,
            name: payload.name,
            timestamp: payload.timestamp,
            attributes: payload.properties
        };
    });
    const endpoint = getEndpoint('/integration-segment/v1/post-events');
    return request(endpoint, {
        method: 'post',
        json: {
            events: payload
        }
    });
}
//# sourceMappingURL=utilities.js.map