"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 recombeeApiClient_1 = require("./recombeeApiClient");
const addBookmark_1 = __importDefault(require("./addBookmark"));
const addCartAddition_1 = __importDefault(require("./addCartAddition"));
const addDetailView_1 = __importDefault(require("./addDetailView"));
const addPurchase_1 = __importDefault(require("./addPurchase"));
const addRating_1 = __importDefault(require("./addRating"));
const deleteCartAddition_1 = __importDefault(require("./deleteCartAddition"));
const mergeUsers_1 = __importDefault(require("./mergeUsers"));
const setViewPortion_1 = __importDefault(require("./setViewPortion"));
const setViewPortionFromWatchTime_1 = __importDefault(require("./setViewPortionFromWatchTime"));
const deleteBookmark_1 = __importDefault(require("./deleteBookmark"));
const commonFields_1 = require("./commonFields");
const destination = {
    name: 'Recombee',
    slug: 'actions-recombee',
    mode: 'cloud',
    authentication: {
        scheme: 'custom',
        fields: {
            databaseId: {
                label: 'Database ID',
                description: 'The ID of the Recombee Database into which the interactions will be sent.',
                type: 'string',
                required: true
            },
            privateToken: {
                label: 'Private Token',
                description: 'The private token for the Recombee Database used.',
                type: 'password',
                required: true
            },
            databaseRegion: {
                label: 'Database Region',
                description: 'The Recombee cluster where your Database is located. [Learn more](https://docs.recombee.com/regions)',
                type: 'string',
                required: true,
                format: 'hostname',
                default: 'eu-west',
                choices: [
                    { value: 'eu-west', label: 'EU' },
                    { value: 'ca-east', label: 'Canada (East Coast)' },
                    { value: 'ap-se', label: 'Australia' },
                    { value: 'us-west', label: 'US (West Coast)' },
                    { value: 'custom', label: 'Custom' }
                ]
            },
            apiUri: {
                label: 'API URI',
                description: 'URI of the Recombee API that should be used. *Keep this field empty unless you are calling the Recombee cluster based in a specific region or you were assigned a custom URI by the Recombee Support team.*',
                type: 'string',
                required: false,
                depends_on: {
                    conditions: [
                        {
                            fieldKey: 'databaseRegion',
                            operator: 'is',
                            value: 'custom'
                        }
                    ]
                }
            }
        },
        testAuthentication: (request, { settings }) => {
            const client = new recombeeApiClient_1.RecombeeApiClient(settings, request);
            return client.send(new recombeeApiClient_1.Batch([]));
        }
    },
    presets: [
        {
            name: 'Page - Viewed',
            subscribe: 'type = "page"',
            partnerAction: 'addDetailView',
            mapping: {
                ...(0, actions_core_1.defaultValues)(addDetailView_1.default.fields),
                itemId: { '@path': '$.name' }
            },
            type: 'automatic'
        },
        {
            name: 'Ecommerce - Product Viewed',
            subscribe: 'type = "track" and event = "Product Viewed"',
            partnerAction: 'addDetailView',
            mapping: {
                ...(0, actions_core_1.defaultValues)(addDetailView_1.default.fields),
                ...commonFields_1.ecommerceIdMapping
            },
            type: 'automatic'
        },
        {
            name: 'Ecommerce - Product Added',
            subscribe: 'type = "track" and event = "Product Added"',
            partnerAction: 'addCartAddition',
            mapping: (0, actions_core_1.defaultValues)(addCartAddition_1.default.fields),
            type: 'automatic'
        },
        {
            name: 'Ecommerce - Product Removed',
            subscribe: 'type = "track" and event = "Product Removed"',
            partnerAction: 'deleteCartAddition',
            mapping: {
                ...(0, actions_core_1.defaultValues)(deleteCartAddition_1.default.fields),
                ...commonFields_1.ecommerceIdMapping
            },
            type: 'automatic'
        },
        {
            name: 'Ecommerce - Order Completed',
            subscribe: 'type = "track" and event = "Order Completed"',
            partnerAction: 'addPurchase',
            mapping: (0, actions_core_1.defaultValues)(addPurchase_1.default.fields),
            type: 'automatic'
        },
        {
            name: 'Ecommerce - Product Added to Wishlist',
            subscribe: 'type = "track" and event = "Product Added to Wishlist"',
            partnerAction: 'addBookmark',
            mapping: {
                ...(0, actions_core_1.defaultValues)(addBookmark_1.default.fields),
                ...commonFields_1.ecommerceIdMapping
            },
            type: 'automatic'
        },
        {
            name: 'Ecommerce - Product Removed from Wishlist',
            subscribe: 'type = "track" and event = "Product Removed from Wishlist"',
            partnerAction: 'deleteBookmark',
            mapping: {
                ...(0, actions_core_1.defaultValues)(deleteBookmark_1.default.fields),
                ...commonFields_1.ecommerceIdMapping
            },
            type: 'automatic'
        },
        {
            name: 'Ecommerce - Product Shared',
            subscribe: 'type = "track" and event = "Product Shared"',
            partnerAction: 'addBookmark',
            mapping: {
                ...(0, actions_core_1.defaultValues)(addBookmark_1.default.fields),
                ...commonFields_1.ecommerceIdMapping
            },
            type: 'automatic'
        },
        {
            name: 'Video - Playback Started',
            subscribe: 'type = "track" and event = "Video Playback Started"',
            partnerAction: 'setViewPortion',
            mapping: {
                ...(0, actions_core_1.defaultValues)(setViewPortion_1.default.fields),
                ...commonFields_1.videoIdMapping,
                portion: 0
            },
            type: 'automatic'
        },
        {
            name: 'Video - Content Playing',
            subscribe: 'type = "track" and event = "Video Content Playing"',
            partnerAction: 'setViewPortionFromWatchTime',
            mapping: {
                ...(0, actions_core_1.defaultValues)(setViewPortionFromWatchTime_1.default.fields),
                ...commonFields_1.videoIdMapping
            },
            type: 'automatic'
        },
        {
            name: 'Video - Playback Paused',
            subscribe: 'type = "track" and event = "Video Playback Paused"',
            partnerAction: 'setViewPortionFromWatchTime',
            mapping: {
                ...(0, actions_core_1.defaultValues)(setViewPortionFromWatchTime_1.default.fields),
                itemId: { '@path': '$.properties.content_asset_id' }
            },
            type: 'automatic'
        },
        {
            name: 'Video - Playback Completed',
            subscribe: 'type = "track" and event = "Video Playback Completed"',
            partnerAction: 'setViewPortion',
            mapping: {
                ...(0, actions_core_1.defaultValues)(setViewPortion_1.default.fields),
                ...commonFields_1.videoIdMapping,
                portion: 1
            },
            type: 'automatic'
        },
        {
            name: 'Screen - Viewed',
            subscribe: 'type = "screen"',
            partnerAction: 'addDetailView',
            mapping: {
                ...(0, actions_core_1.defaultValues)(addDetailView_1.default.fields),
                itemId: { '@path': '$.name' }
            },
            type: 'automatic'
        },
        {
            name: 'Alias',
            subscribe: 'type = "alias"',
            partnerAction: 'mergeUsers',
            mapping: (0, actions_core_1.defaultValues)(mergeUsers_1.default.fields),
            type: 'automatic'
        }
    ],
    onDelete: async (request, { settings, payload }) => {
        if (!payload.userId)
            return;
        const client = new recombeeApiClient_1.RecombeeApiClient(settings, request);
        return client.send(new recombeeApiClient_1.DeleteUser(payload.userId));
    },
    actions: {
        addBookmark: addBookmark_1.default,
        addCartAddition: addCartAddition_1.default,
        addDetailView: addDetailView_1.default,
        addPurchase: addPurchase_1.default,
        addRating: addRating_1.default,
        deleteBookmark: deleteBookmark_1.default,
        deleteCartAddition: deleteCartAddition_1.default,
        mergeUsers: mergeUsers_1.default,
        setViewPortion: setViewPortion_1.default,
        setViewPortionFromWatchTime: setViewPortionFromWatchTime_1.default
    }
};
exports.default = destination;
//# sourceMappingURL=index.js.map