"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.destination = void 0;
const tslib_1 = require("tslib");
const shim_1 = require("../../runtime/shim");
const actions_core_1 = require("@segment/actions-core");
const identifyUser_1 = tslib_1.__importDefault(require("./identifyUser"));
const trackEvent_1 = tslib_1.__importDefault(require("./trackEvent"));
const pageView_1 = tslib_1.__importDefault(require("./pageView"));
const identifyCompany_1 = tslib_1.__importDefault(require("./identifyCompany"));
exports.destination = {
    name: 'Userpilot Web (Actions)',
    slug: 'actions-userpilot-web',
    mode: 'device',
    presets: [
        {
            name: 'Identify User',
            subscribe: 'type = "identify"',
            partnerAction: 'identifyUser',
            mapping: actions_core_1.defaultValues(identifyUser_1.default.fields)
        },
        {
            name: 'Track Event',
            subscribe: 'type = "track"',
            partnerAction: 'trackEvent',
            mapping: actions_core_1.defaultValues(trackEvent_1.default.fields)
        },
        {
            name: 'Page View',
            subscribe: 'type = "page"',
            partnerAction: 'pageView',
            mapping: actions_core_1.defaultValues(pageView_1.default.fields)
        }
    ],
    settings: {
        token: {
            label: 'App Token',
            description: 'Your Userpilot app token, you can find it in the [Userpilot installation](https://run.userpilot.io/installation) dashboard.',
            required: true,
            type: 'string'
        },
        endpoint: {
            label: 'The API endpoint the SDK would connect to',
            description: 'By default, Userpilot would use a service discovery mechanism to determine the API endpoint to connect to. If you are using a proxy or a firewall, you can specify the API endpoint here.',
            required: false,
            type: 'string'
        },
        shouldSegmentLoadSDK: {
            label: 'Segment Loads Userpilot JS',
            description: 'By default, Segment will load the Userpilot JS snippet onto the page. If you are already loading the Userpilot JS onto the page then disable this setting and Segment will detect the Userpilot JS on the page',
            required: true,
            type: 'boolean',
            default: true
        }
    },
    initialize: async ({ settings }, deps) => {
        const shouldLoadSDK = settings.shouldSegmentLoadSDK ?? true;
        if (shouldLoadSDK) {
            window.userpilotSettings = {
                token: settings.token,
                endpoint: settings.endpoint,
                shouldSegmentLoadSDK: shouldLoadSDK
            };
            await deps.loadScript('//js.userpilot.io/sdk/latest.js');
            await deps.resolveWhen(() => Object.prototype.hasOwnProperty.call(window, 'userpilot'), 100);
        }
        else {
            await deps.resolveWhen(() => window.userpilot !== undefined, 100);
        }
        return window.userpilot;
    },
    actions: {
        identifyUser: identifyUser_1.default,
        trackEvent: trackEvent_1.default,
        pageView: pageView_1.default,
        identifyCompany: identifyCompany_1.default
    }
};
exports.default = shim_1.browserDestination(exports.destination);
//# sourceMappingURL=index.js.map