"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 customEvent_1 = __importDefault(require("./customEvent"));
const nonEcommCustomEvent_1 = __importDefault(require("./nonEcommCustomEvent"));
const upsertContact_1 = __importDefault(require("./upsertContact"));
const emailEvent_1 = __importDefault(require("./emailEvent"));
const utils_1 = require("./utils");
const singleProductFields = {
    ...(0, actions_core_1.defaultValues)(customEvent_1.default.fields),
    products: {
        '@arrayPath': [
            '$.properties',
            {
                product_id: {
                    '@path': '$.product_id'
                },
                qty: {
                    '@path': '$.quantity'
                }
            }
        ]
    }
};
const destination = {
    name: 'Optimizely Data Platform',
    slug: 'actions-optimizely-data-platform',
    mode: 'cloud',
    description: 'Sync Segment analytics events and user profile data to Optimizely Data Platform',
    authentication: {
        scheme: 'custom',
        fields: {
            apiKey: {
                label: 'Api Key',
                description: 'Api Key used for Optimizely API authorization when sending events',
                type: 'password',
                required: true
            },
            region: {
                label: 'Region',
                description: 'Optimizely Region to sync data to. Default is US',
                type: 'string',
                required: true,
                choices: [
                    { label: 'US', value: 'US' },
                    { label: 'Europe', value: 'EU' },
                    { label: 'Australia', value: 'AU' }
                ],
                default: 'US'
            }
        },
        testAuthentication: (request, { settings }) => {
            const host = utils_1.hosts[settings.region];
            return request(`${host}/auth`, {
                method: 'POST'
            });
        }
    },
    extendRequest({ settings }) {
        return {
            headers: { Authorization: `Bearer ${settings.apiKey}` }
        };
    },
    presets: [
        {
            name: 'Product Viewed',
            subscribe: 'type = "track" and event = "Product Viewed"',
            partnerAction: 'customEvent',
            mapping: {
                ...singleProductFields,
                event_type: 'product',
                event_action: 'detail'
            },
            type: 'automatic'
        },
        {
            name: 'Product Added',
            subscribe: 'type = "track" and event = "Product Added"',
            partnerAction: 'customEvent',
            mapping: {
                ...singleProductFields,
                event_type: 'product',
                event_action: 'add_to_cart'
            },
            type: 'automatic'
        },
        {
            name: 'Product Removed',
            subscribe: 'type = "track" and event = "Product Removed"',
            partnerAction: 'customEvent',
            mapping: {
                ...singleProductFields,
                event_type: 'product',
                event_action: 'remove_from_cart'
            },
            type: 'automatic'
        },
        {
            name: 'Order Completed',
            subscribe: 'type = "track" and event = "Order Completed"',
            partnerAction: 'customEvent',
            mapping: {
                ...(0, actions_core_1.defaultValues)(customEvent_1.default.fields),
                event_type: 'order',
                event_action: 'purchase'
            },
            type: 'automatic'
        },
        {
            name: 'Email Sent',
            subscribe: 'type = "track" and event = "Email Sent"',
            partnerAction: 'emailEvent',
            mapping: {
                ...(0, actions_core_1.defaultValues)(emailEvent_1.default.fields),
                event_action: 'sent'
            },
            type: 'automatic'
        },
        {
            name: 'Email Link Clicked',
            subscribe: 'type = "track" and event = "Email Link Clicked"',
            partnerAction: 'emailEvent',
            mapping: {
                ...(0, actions_core_1.defaultValues)(emailEvent_1.default.fields),
                event_action: 'click'
            },
            type: 'automatic'
        },
        {
            name: 'Email Opened',
            subscribe: 'type = "track" and event = "Email Opened"',
            partnerAction: 'emailEvent',
            mapping: {
                ...(0, actions_core_1.defaultValues)(emailEvent_1.default.fields),
                event_action: 'open'
            },
            type: 'automatic'
        },
        {
            name: 'Unsubscribed',
            subscribe: 'type = "track" and event = "Unsubscribed"',
            partnerAction: 'emailEvent',
            mapping: {
                ...(0, actions_core_1.defaultValues)(emailEvent_1.default.fields),
                event_action: 'unsubscribe'
            },
            type: 'automatic'
        },
        {
            name: 'Email Marked as Spam',
            subscribe: 'type = "track" and event = "Email Marked as Spam"',
            partnerAction: 'emailEvent',
            mapping: {
                ...(0, actions_core_1.defaultValues)(emailEvent_1.default.fields),
                event_action: 'spam_report'
            },
            type: 'automatic'
        }
    ],
    actions: {
        customEvent: customEvent_1.default,
        nonEcommCustomEvent: nonEcommCustomEvent_1.default,
        upsertContact: upsertContact_1.default,
        emailEvent: emailEvent_1.default
    }
};
exports.default = destination;
//# sourceMappingURL=index.js.map