"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.destination = void 0;
const tslib_1 = require("tslib");
const actions_core_1 = require("@segment/actions-core");
const shim_1 = require("../../runtime/shim");
const init_script_1 = require("./init-script");
const trackEvent_1 = tslib_1.__importDefault(require("./trackEvent"));
const identifyVisitor_1 = tslib_1.__importDefault(require("./identifyVisitor"));
exports.destination = {
    name: 'Koala',
    slug: 'actions-koala',
    description: 'Connect Koala in Segment to send visitor events or traits to Koala.',
    mode: 'device',
    settings: {
        project_slug: {
            type: 'string',
            label: 'Public API Key',
            description: 'Please enter your Public API Key found in your Koala workspace settings.',
            required: true
        }
    },
    initialize: async ({ settings, analytics }, deps) => {
        init_script_1.initScript();
        await deps.loadScript(`https://cdn.koala.live/v1/${settings.project_slug}/umd.js`);
        const ko = await window.KoalaSDK.load({
            project: settings.project_slug,
            hookSegment: false
        });
        void analytics.ready(() => ko.ready(() => ko.identify(analytics.user().traits())));
        return ko;
    },
    actions: {
        trackEvent: trackEvent_1.default,
        identifyVisitor: identifyVisitor_1.default
    },
    presets: [
        {
            name: 'Track Event',
            subscribe: 'type = "track"',
            partnerAction: 'trackEvent',
            mapping: actions_core_1.defaultValues(trackEvent_1.default.fields)
        },
        {
            name: 'Identify Visitor',
            subscribe: 'type = "identify"',
            partnerAction: 'identifyVisitor',
            mapping: actions_core_1.defaultValues(identifyVisitor_1.default.fields)
        }
    ]
};
exports.default = shim_1.browserDestination(exports.destination);
//# sourceMappingURL=index.js.map