"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const dayjs_1 = __importDefault(require("../../../lib/dayjs"));
const shared_fields_1 = require("../shared-fields");
const utils_1 = require("../utils");
const transformIterableEventPayload = (payload) => {
    const formattedDataFields = (0, utils_1.convertDatesInObject)(payload.dataFields ?? {});
    return {
        ...payload,
        dataFields: formattedDataFields,
        createdAt: (0, dayjs_1.default)(payload.createdAt).unix()
    };
};
const action = {
    title: 'Custom Event',
    description: 'Track a custom event to a user profile',
    defaultSubscription: 'type = "track" and event != "Order Completed" and event != "Cart Updated"',
    fields: {
        email: {
            ...shared_fields_1.EMAIL_FIELD
        },
        userId: {
            ...shared_fields_1.USER_ID_FIELD
        },
        eventName: {
            label: 'Event name',
            description: 'Name of the event',
            type: 'string',
            required: true,
            default: { '@path': '$.event' }
        },
        dataFields: {
            ...shared_fields_1.EVENT_DATA_FIELDS
        },
        id: {
            label: 'Event ID',
            description: 'A unique ID. If an event exists with that id, the event will be updated',
            type: 'string',
            required: false,
            default: { '@path': '$.messageId' }
        },
        createdAt: {
            ...shared_fields_1.CREATED_AT_FIELD
        },
        campaignId: {
            ...shared_fields_1.CAMPAGIN_ID_FIELD
        },
        templateId: {
            ...shared_fields_1.TEMPLATE_ID_FIELD
        },
        enable_batching: {
            label: 'Enable Batching',
            description: 'When enabled, Segment will send data to Iterable in batches of up to 1001',
            type: 'boolean',
            required: false,
            default: true
        },
        batch_size: {
            label: 'Batch Size',
            description: 'Maximum number of events to include in each batch. Actual batch sizes may be lower.',
            type: 'number',
            unsafe_hidden: true,
            required: false,
            default: 1001
        }
    },
    perform: (request, { payload, settings }) => {
        if (!payload.email && !payload.userId) {
            throw new actions_core_1.PayloadValidationError('Must include email or userId.');
        }
        const trackEventRequest = transformIterableEventPayload(payload);
        const endpoint = (0, utils_1.getRegionalEndpoint)('trackEvent', settings.dataCenterLocation);
        return request(endpoint, {
            method: 'post',
            json: trackEventRequest,
            timeout: Math.max(30000, actions_core_1.DEFAULT_REQUEST_TIMEOUT)
        });
    },
    performBatch: async (request, { settings, payload }) => {
        const bulkTrackEventRequest = {
            events: payload.map(transformIterableEventPayload)
        };
        const endpoint = (0, utils_1.getRegionalEndpoint)('bulkTrackEvent', settings.dataCenterLocation);
        return request(endpoint, {
            method: 'post',
            json: bulkTrackEventRequest,
            timeout: Math.max(30000, actions_core_1.DEFAULT_REQUEST_TIMEOUT)
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map