"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validatePayload = validatePayload;
exports.sendEvents = sendEvents;
const actions_core_1 = require("@segment/actions-core");
function validatePayload(payload, settings) {
    if (!payload.app_token && !settings.default_app_token) {
        throw new actions_core_1.IntegrationError('One of app_token field or default_app_token setting fields must have a value.', 'APP_TOKEN_VALIDATION_FAILED', 400);
    }
    if (!payload.event_token && !settings.default_event_token) {
        throw new actions_core_1.IntegrationError('One of event_token field or default_event_token setting fields must have a value.', 'EVENT_TOKEN_VALIDATION_FAILED', 400);
    }
    const adjustPayload = {
        app_token: String(payload.app_token || settings.default_app_token),
        event_token: String(payload.event_token || settings.default_event_token),
        environment: settings.environment,
        s2s: 1,
        callback_params: JSON.stringify(payload),
        created_at_unix: payload.timestamp
            ? parseInt((new Date(String(payload.timestamp)).getTime() / 1000).toFixed(0))
            : undefined
    };
    return adjustPayload;
}
async function sendEvents(request, events) {
    return await Promise.all(events.map((event) => request('https://s2s.adjust.com/event', {
        method: 'POST',
        body: JSON.stringify(event)
    })));
}
//# sourceMappingURL=functions.js.map