"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 identify_1 = __importDefault(require("./identify"));
const group_1 = __importDefault(require("./group"));
const track_1 = __importDefault(require("./track"));
const page_1 = __importDefault(require("./page"));
const screen_1 = __importDefault(require("./screen"));
const destination = {
    name: 'Hilo',
    slug: 'actions-hilo',
    mode: 'cloud',
    authentication: {
        scheme: 'oauth-managed',
        fields: {
            integrationId: {
                description: 'Hilo integration ID. This can be found on the integration page.',
                label: 'Integration ID',
                type: 'string',
                required: true
            }
        },
        testAuthentication: (request) => {
            return request('https://api.hilohq.com/oauth/token/info');
        },
        refreshAccessToken: async (request, { auth }) => {
            const res = await request('https://api.hilohq.com/oauth/token', {
                method: 'POST',
                body: new URLSearchParams({
                    refresh_token: auth.refreshToken,
                    client_id: auth.clientId,
                    client_secret: auth.clientSecret,
                    grant_type: 'refresh_token'
                })
            });
            return { accessToken: res.data?.access_token, refreshToken: res.data?.refresh_token };
        }
    },
    extendRequest({ auth, settings }) {
        return {
            headers: {
                authorization: `Bearer ${auth?.accessToken}`
            },
            searchParams: {
                integration_id: settings?.integrationId
            }
        };
    },
    onDelete: async (request, { payload }) => {
        return request(`https://api.hilohq.com/v1/events/delete`, {
            method: 'POST',
            json: {
                event: {
                    contact_id: payload.userId
                }
            }
        });
    },
    actions: {
        identify: identify_1.default,
        group: group_1.default,
        track: track_1.default,
        page: page_1.default,
        screen: screen_1.default
    },
    presets: [
        {
            name: 'Track Calls',
            subscribe: 'type = "track"',
            partnerAction: 'track',
            mapping: (0, actions_core_1.defaultValues)(track_1.default.fields),
            type: 'automatic'
        },
        {
            name: 'Page Calls',
            subscribe: 'type = "page"',
            partnerAction: 'page',
            mapping: (0, actions_core_1.defaultValues)(page_1.default.fields),
            type: 'automatic'
        },
        {
            name: 'Screen Calls',
            subscribe: 'type = "screen"',
            partnerAction: 'screen',
            mapping: (0, actions_core_1.defaultValues)(screen_1.default.fields),
            type: 'automatic'
        },
        {
            name: 'Identify Calls',
            subscribe: 'type = "identify"',
            partnerAction: 'identify',
            mapping: (0, actions_core_1.defaultValues)(identify_1.default.fields),
            type: 'automatic'
        },
        {
            name: 'Group Calls',
            subscribe: 'type = "group"',
            partnerAction: 'group',
            mapping: (0, actions_core_1.defaultValues)(group_1.default.fields),
            type: 'automatic'
        }
    ]
};
exports.default = destination;
//# sourceMappingURL=index.js.map