"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utility_1 = require("../utility");
const action = {
    title: 'Track Event',
    description: `Sends Segment's track event to VWO`,
    defaultSubscription: 'type = "track"',
    fields: {
        name: {
            description: 'Name of the event',
            label: 'Name',
            required: true,
            type: 'string',
            default: {
                '@path': '$.event'
            }
        },
        properties: {
            description: 'JSON object containing additional properties that will be associated with the event.',
            label: 'Properties',
            required: false,
            type: 'object',
            default: {
                '@path': '$.properties'
            }
        },
        vwoUuid: {
            description: 'VWO UUID',
            label: 'VWO UUID',
            required: true,
            type: 'string',
            default: {
                '@path': '$.properties.vwo_uuid'
            }
        },
        page: {
            description: 'Contains context information regarding a webpage',
            label: 'Page',
            required: false,
            type: 'object',
            default: {
                '@path': '$.context.page'
            }
        },
        ip: {
            description: 'IP address of the user',
            label: 'IP Address',
            required: false,
            type: 'string',
            default: {
                '@path': '$.context.ip'
            }
        },
        userAgent: {
            description: 'User-Agent of the user',
            label: 'User Agent',
            required: false,
            type: 'string',
            default: {
                '@path': '$.context.userAgent'
            }
        },
        timestamp: {
            description: 'Timestamp on the event',
            label: 'Timestamp',
            required: false,
            type: 'string',
            default: {
                '@path': '$.timestamp'
            }
        }
    },
    perform: (request, { settings, payload }) => {
        const sanitisedEventName = (0, utility_1.sanitiseEventName)(payload.name);
        const { headers, structuredPayload } = (0, utility_1.formatPayload)(sanitisedEventName, payload, true, true, settings.apikey, settings.vwoAccountId);
        structuredPayload.d.event.props.vwoMeta['ogName'] = payload.name;
        const region = settings.region || "US";
        const host = utility_1.hosts[region];
        const endpoint = `${host}/events/t?en=${sanitisedEventName}&a=${settings.vwoAccountId}`;
        return request(endpoint, {
            method: 'POST',
            headers,
            json: structuredPayload
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map