"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const identifyContact_1 = __importDefault(require("./identifyContact"));
const groupIdentifyContact_1 = __importDefault(require("./groupIdentifyContact"));
const trackEvent_1 = __importDefault(require("./trackEvent"));
const util_1 = require("./util");
const destination = {
    name: 'Intercom Cloud Mode (Actions)',
    slug: 'actions-intercom-cloud',
    mode: 'cloud',
    authentication: {
        scheme: 'oauth2',
        fields: {},
        testAuthentication: async (request) => {
            try {
                return await request('https://api.intercom.io/me');
            }
            catch (error) {
                throw new Error('Test authentication failed');
            }
        }
    },
    extendRequest({ auth }) {
        return {
            headers: {
                authorization: `Bearer ${auth?.accessToken}`
            }
        };
    },
    onDelete: async (request, { payload }) => {
        const external_id = payload.userId;
        const contact = await (0, util_1.getUniqueIntercomContact)(request, { external_id });
        if (contact) {
            return request(`https://api.intercom.io/contacts/${payload.userId}`, {
                method: 'DELETE'
            });
        }
    },
    actions: {
        identifyContact: identifyContact_1.default,
        groupIdentifyContact: groupIdentifyContact_1.default,
        trackEvent: trackEvent_1.default
    }
};
exports.default = destination;
//# sourceMappingURL=index.js.map