"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const action = {
    title: 'Track Event',
    description: 'Submit an event to Intercom.',
    defaultSubscription: 'type = "track"',
    platform: 'web',
    fields: {
        event_name: {
            description: 'The name of the event.',
            label: 'Event Name',
            type: 'string',
            required: true,
            default: {
                '@path': '$.event'
            }
        },
        revenue: {
            description: 'The amount associated with a purchase. Segment will multiply by 100 as Intercom requires the amount in cents.',
            label: 'Revenue',
            type: 'number',
            required: false,
            default: {
                '@path': '$.properties.revenue'
            }
        },
        currency: {
            description: 'The currency of the purchase amount. Segment will default to USD if revenue is provided without a currency.',
            label: 'Currency',
            type: 'string',
            required: false,
            default: {
                '@path': '$.properties.currency'
            }
        },
        event_metadata: {
            description: 'Optional metadata describing the event.',
            label: 'Event Metadata',
            type: 'object',
            required: false,
            default: {
                '@path': '$.properties'
            }
        }
    },
    perform: (Intercom, event) => {
        const { event_name, event_metadata, ...rest } = event.payload;
        const payload = { ...rest };
        const richLinkProperties = Intercom.richLinkProperties ? Intercom.richLinkProperties : [];
        const richLinkObjects = {};
        if (event_metadata && richLinkProperties.length != 0) {
            Object.entries(event_metadata).forEach(([key, value]) => {
                if (richLinkProperties.includes(key)) {
                    richLinkObjects[key] = value;
                }
            });
        }
        const filteredMetadata = utils_1.filterCustomTraits(event_metadata);
        let price = {};
        if (payload.revenue) {
            price = {
                amount: payload.revenue * 100,
                currency: payload.currency ?? 'USD'
            };
            delete filteredMetadata.revenue;
            delete filteredMetadata.currency;
        }
        Intercom('trackEvent', event_name, {
            ...filteredMetadata,
            ...richLinkObjects,
            ...(!utils_1.isEmpty(price) && { price })
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map