"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const utils_1 = require("./utils");
const fields_1 = require("./fields");
const action = {
    title: 'Track Conversion',
    description: 'Send conversion event data to Amazon Events API',
    defaultSubscription: 'type = "track"',
    fields: fields_1.fields,
    perform: async (request, { payload, settings }) => {
        const eventData = (0, utils_1.prepareEventData)(payload, settings);
        const response = await (0, utils_1.sendEventsRequest)(request, settings, eventData);
        return (0, utils_1.handleResponse)(response);
    },
    performBatch: async (request, { settings, payload: payloads }) => {
        const multiStatusResponse = new actions_core_1.MultiStatusResponse();
        const validPayloads = [];
        const validPayloadIndicesBitmap = [];
        payloads.forEach((payload, index) => {
            try {
                const eventData = (0, utils_1.prepareEventData)(payload, settings);
                validPayloads.push(eventData);
                validPayloadIndicesBitmap.push(index);
            }
            catch (error) {
                multiStatusResponse.setErrorResponseAtIndex(index, {
                    status: error?.status || 400,
                    errortype: error?.code || 'PAYLOAD_VALIDATION_FAILED',
                    errormessage: error?.message || 'Validation failed'
                });
            }
        });
        if (validPayloads.length === 0) {
            return multiStatusResponse;
        }
        const response = await (0, utils_1.sendEventsRequest)(request, settings, validPayloads);
        return (0, utils_1.handleBatchResponse)(response, validPayloads, validPayloadIndicesBitmap, multiStatusResponse);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map