"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const dayjs_1 = __importDefault(require("../../../lib/dayjs"));
const constants_1 = require("../constants");
const flat_1 = require("../flat");
const heapUtils_1 = require("../heapUtils");
const actions_core_1 = require("@segment/actions-core");
const action = {
    title: 'Track Event',
    description: 'Send an event to Heap.',
    defaultSubscription: 'type = "track" or type = "page" or type = "screen"',
    fields: {
        message_id: {
            label: 'Message ID',
            type: 'string',
            description: 'Unique event ID generated by Segment.',
            required: true,
            default: {
                '@path': '$.messageId'
            }
        },
        identity: {
            label: 'Identity',
            type: 'string',
            allowNull: true,
            description: 'a string that uniquely identifies a user, such as an email, handle, or username. This means no two users in one environment may share the same identity. More on identify: https://developers.heap.io/docs/using-identify'
        },
        anonymous_id: {
            label: 'Anonymous ID',
            type: 'string',
            allowNull: true,
            description: 'The generated anonymous ID for the user.',
            default: {
                '@path': '$.anonymousId'
            }
        },
        event: {
            label: 'Track Event Type',
            type: 'string',
            description: 'Name of the user action. This only exists on track events. Limited to 1024 characters.',
            default: {
                '@path': '$.event'
            }
        },
        properties: {
            label: 'Event Properties',
            type: 'object',
            description: 'An object with key-value properties you want associated with the event. Each key and property must either be a number or string with fewer than 1024 characters.',
            default: {
                '@path': '$.properties'
            }
        },
        timestamp: {
            label: 'Timestamp',
            type: 'datetime',
            description: 'Defaults to the current time if not provided.',
            default: {
                '@path': '$.timestamp'
            }
        },
        type: {
            label: 'Type',
            type: 'string',
            description: 'The type of call. Can be track, page, or screen.',
            default: {
                '@path': '$.type'
            }
        },
        name: {
            label: 'Page or Screen Name',
            type: 'string',
            description: 'The name of the page or screen being viewed. This only exists for page and screen events.',
            default: {
                '@path': '$.name'
            }
        },
        traits: {
            label: 'User Properties',
            type: 'object',
            description: 'An object with key-value properties you want associated with the user. Each property must either be a number or string with fewer than 1024 characters.',
            default: {
                '@path': '$.context.traits'
            }
        }
    },
    perform: (request, { payload, settings }) => {
        const requests = [];
        if (!payload.anonymous_id && !payload.identity) {
            throw new actions_core_1.IntegrationError('Either Anonymous id or Identity should be specified.', 'MISSING_REQUIRED_FIELD', 400);
        }
        const flattenedProperties = (0, flat_1.flat)(payload.properties || {});
        const event = {
            event: (0, heapUtils_1.getEventName)(payload),
            custom_properties: {
                segment_library: constants_1.HEAP_SEGMENT_CLOUD_LIBRARY_NAME,
                ...flattenedProperties,
                ...((0, heapUtils_1.isDefined)(payload.name) && { name: payload.name })
            },
            idempotency_key: payload.message_id
        };
        event.user_identifier = (0, heapUtils_1.getUserIdentifier)({ identity: payload.identity, anonymous_id: payload.anonymous_id });
        if (payload.timestamp && dayjs_1.default.utc(payload.timestamp).isValid()) {
            event.timestamp = dayjs_1.default.utc(payload.timestamp).toISOString();
        }
        const trackPayload = {
            app_id: settings.appId,
            events: [event],
            library: 'server'
        };
        if ((0, heapUtils_1.isDefined)(payload.identity) && ((0, heapUtils_1.isDefined)(payload.anonymous_id) || (0, heapUtils_1.isDefined)(payload.traits))) {
            const userPropertiesPayload = {
                app_id: settings.appId,
                identity: payload.identity,
                properties: {
                    ...((0, heapUtils_1.isDefined)(payload.anonymous_id) && { anonymous_id: payload.anonymous_id }),
                    ...(payload.traits && Object.keys(payload.traits).length !== 0 && (0, flat_1.flat)(payload.traits))
                }
            };
            const addUserPropertiesRequest = request('https://heapanalytics.com/api/add_user_properties', {
                method: 'post',
                json: userPropertiesPayload
            });
            requests.push(addUserPropertiesRequest);
        }
        requests.push(request('https://heapanalytics.com/api/integrations/track', {
            method: 'post',
            json: trackPayload
        }));
        return Promise.all(requests);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map