"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 customEvents_1 = __importDefault(require("./customEvents"));
const ecommEvent_1 = __importDefault(require("./ecommEvent"));
const upsertUserAttributes_1 = __importDefault(require("./upsertUserAttributes"));
const subscribeUser_1 = __importDefault(require("./subscribeUser"));
const destination = {
    name: 'Attentive',
    slug: 'actions-attentive',
    mode: 'cloud',
    description: 'Send Segment analytics events to Attentive.',
    authentication: {
        scheme: 'custom',
        fields: {
            apiKey: {
                label: 'API Key',
                description: 'Your Attentive API Key.',
                type: 'password',
                required: true
            }
        },
        testAuthentication: async (request, { settings }) => {
            return request('https://api.attentivemobile.com/v1/me', {
                method: 'GET',
                headers: {
                    Authorization: `Bearer ${settings.apiKey}`
                }
            });
        }
    },
    extendRequest({ settings }) {
        return {
            headers: {
                Authorization: `Bearer ${settings.apiKey}`,
                'Content-Type': 'application/json'
            }
        };
    },
    presets: [
        {
            name: 'Subscribe User',
            subscribe: 'type = "track" and event = "User Subscribed"',
            partnerAction: 'subscribeUser',
            mapping: (0, actions_core_1.defaultValues)(subscribeUser_1.default.fields),
            type: 'automatic'
        },
        {
            name: 'Upsert User Attributes',
            subscribe: 'type = "identify"',
            partnerAction: 'upsertUserAttributes',
            mapping: (0, actions_core_1.defaultValues)(upsertUserAttributes_1.default.fields),
            type: 'automatic'
        },
        {
            name: 'View Item',
            subscribe: 'event = "Product Viewed"',
            partnerAction: 'ecommEvent',
            mapping: {
                ...(0, actions_core_1.defaultValues)(ecommEvent_1.default.fields),
                eventType: 'view-item',
                items: {
                    productId: { '@path': '$.properties.product_id' },
                    productVariantId: { '@path': '$.properties.variant' },
                    value: { '@path': '$.properties.price' },
                    productImage: { '@path': '$.properties.image_url' },
                    productUrl: { '@path': '$.properties.url' },
                    name: { '@path': '$.properties.name' },
                    currency: { '@path': '$.properties.currency' },
                    quantity: { '@path': '$.properties.quantity' }
                }
            },
            type: 'automatic'
        },
        {
            name: 'Add to Cart',
            subscribe: 'event = "Product Added"',
            partnerAction: 'ecommEvent',
            mapping: {
                ...(0, actions_core_1.defaultValues)(ecommEvent_1.default.fields),
                eventType: 'add-to-cart',
                items: {
                    productId: { '@path': '$.properties.product_id' },
                    productVariantId: { '@path': '$.properties.variant' },
                    value: { '@path': '$.properties.price' },
                    productImage: { '@path': '$.properties.image_url' },
                    productUrl: { '@path': '$.properties.url' },
                    name: { '@path': '$.properties.name' },
                    currency: { '@path': '$.properties.currency' },
                    quantity: { '@path': '$.properties.quantity' }
                }
            },
            type: 'automatic'
        },
        {
            name: 'Purchase',
            subscribe: 'event = "Order Completed"',
            partnerAction: 'ecommEvent',
            mapping: {
                ...(0, actions_core_1.defaultValues)(ecommEvent_1.default.fields),
                eventType: 'purchase'
            },
            type: 'automatic'
        }
    ],
    actions: {
        customEvents: customEvents_1.default,
        ecommEvent: ecommEvent_1.default,
        upsertUserAttributes: upsertUserAttributes_1.default,
        subscribeUser: subscribeUser_1.default
    }
};
exports.default = destination;
//# sourceMappingURL=index.js.map