"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utility_1 = require("../utility");
const action = {
    title: 'Identify User',
    description: "Maps Segment's visitor traits to the visitor attributes in VWO",
    defaultSubscription: 'type = "identify"',
    fields: {
        attributes: {
            description: `Visitor's attributes to be mapped`,
            label: 'attributes',
            required: true,
            type: 'object',
            default: {
                '@path': '$.traits'
            }
        },
        vwoUuid: {
            description: 'VWO UUID',
            label: 'VWO UUID',
            required: true,
            type: 'string',
            default: {
                '@path': '$.traits.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 eventName = 'vwo_syncVisitorProp';
        const attributes = payload.attributes;
        delete attributes['vwo_uuid'];
        const formattedAttributes = (0, utility_1.formatAttributes)(attributes);
        const visitor = { props: formattedAttributes };
        const { headers, structuredPayload } = (0, utility_1.formatPayload)(eventName, payload, true, false, settings.apikey, settings.vwoAccountId);
        if (structuredPayload.d.visitor && structuredPayload.d.event.props.$visitor) {
            structuredPayload.d.visitor.props = {
                ...structuredPayload.d.visitor.props,
                ...formattedAttributes
            };
        }
        else {
            structuredPayload.d.visitor = visitor;
            structuredPayload.d.event.props.$visitor = visitor;
        }
        const region = settings.region || "US";
        const host = utility_1.hosts[region];
        const endpoint = `${host}/events/t?en=${eventName}&a=${settings.vwoAccountId}`;
        return request(endpoint, {
            method: 'POST',
            json: structuredPayload,
            headers
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map