"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.destination = void 0;
const tslib_1 = require("tslib");
const shim_1 = require("../../runtime/shim");
const group_1 = tslib_1.__importDefault(require("./group"));
const identify_1 = tslib_1.__importDefault(require("./identify"));
const track_1 = tslib_1.__importDefault(require("./track"));
const actions_core_1 = require("@segment/actions-core");
const init_script_1 = require("./init-script");
const page_1 = tslib_1.__importDefault(require("./page"));
const defaultVersion = 'latest';
exports.destination = {
    name: 'Ripe Device Mode (Actions)',
    slug: 'actions-ripe',
    mode: 'device',
    settings: {
        sdkVersion: {
            description: 'The version of the Ripe Widget SDK to use',
            label: 'SDK Version',
            type: 'string',
            choices: [
                {
                    value: 'latest',
                    label: 'latest'
                }
            ],
            default: defaultVersion,
            required: false
        },
        apiKey: {
            description: 'The Ripe API key found in the Ripe App',
            label: 'API Key',
            type: 'string',
            required: true
        },
        endpoint: {
            label: 'API Endpoint',
            description: `The Ripe API endpoint (do not change this unless you know what you're doing)`,
            type: 'string',
            format: 'uri',
            default: 'https://storage.getripe.com'
        }
    },
    initialize: async ({ settings }, deps) => {
        init_script_1.initScript();
        const { sdkVersion, apiKey } = settings;
        const version = sdkVersion ?? defaultVersion;
        const endpoint = settings.endpoint || 'https://storage.getripe.com';
        await deps
            .loadScript(`${endpoint}/sdk/${version}/sdk.umd.js`)
            .catch((err) => console.error('Unable to load Ripe SDK script', err));
        await deps.resolveWhen(() => Object.prototype.hasOwnProperty.call(window, 'Ripe'), 100);
        await window.Ripe.init(apiKey);
        return window.Ripe;
    },
    actions: {
        group: group_1.default,
        identify: identify_1.default,
        page: page_1.default,
        track: track_1.default
    },
    presets: [
        {
            name: 'Group user',
            subscribe: 'type = "group"',
            partnerAction: 'group',
            mapping: actions_core_1.defaultValues(group_1.default.fields)
        },
        {
            name: 'Identify user',
            subscribe: 'type = "identify"',
            partnerAction: 'identify',
            mapping: actions_core_1.defaultValues(identify_1.default.fields)
        },
        {
            name: 'Page view',
            subscribe: 'type = "page"',
            partnerAction: 'page',
            mapping: actions_core_1.defaultValues(page_1.default.fields)
        },
        {
            name: 'Track event',
            subscribe: 'type = "track"',
            partnerAction: 'track',
            mapping: actions_core_1.defaultValues(track_1.default.fields)
        }
    ]
};
exports.default = shim_1.browserDestination(exports.destination);
//# sourceMappingURL=index.js.map