"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 presets_1 = require("./presets");
const routes_1 = require("./routes");
const saveBaseEvent_1 = __importDefault(require("./saveBaseEvent"));
const saveCartEvent_1 = __importDefault(require("./saveCartEvent"));
const saveCheckoutEvent_1 = __importDefault(require("./saveCheckoutEvent"));
const saveCollectionEvent_1 = __importDefault(require("./saveCollectionEvent"));
const saveCustomEvent_1 = __importDefault(require("./saveCustomEvent"));
const saveFormEvent_1 = __importDefault(require("./saveFormEvent"));
const saveOrder_1 = __importDefault(require("./saveOrder"));
const saveProductEvent_1 = __importDefault(require("./saveProductEvent"));
const saveSearchEvent_1 = __importDefault(require("./saveSearchEvent"));
const saveUser_1 = __importDefault(require("./saveUser"));
const destination = {
    name: 'Angler AI',
    slug: 'actions-angler-ai',
    description: 'Send analytics events to Angler AI.',
    mode: 'cloud',
    authentication: {
        scheme: 'custom',
        fields: {
            accessToken: {
                label: 'Authentication Token',
                description: 'Your Angler AI API Authentication Token',
                type: 'password',
                required: true
            },
            workspaceId: {
                label: 'Workspace ID',
                description: 'Your Angler AI Workspace ID',
                type: 'string',
                required: true
            }
        },
        testAuthentication: async (request, options) => {
            const me = await request(routes_1.baseURL + (0, routes_1.testEndpoint)(), {
                method: 'get'
            });
            if (me.data.sub !== options.settings.workspaceId) {
                throw new actions_core_1.IntegrationError('Authentication Invalid. Please Check Workspace Id & Token.', actions_core_1.ErrorCodes.INVALID_AUTHENTICATION, 400);
            }
            if (!me.data.scopes.split(',').includes('DATA_ADMIN')) {
                throw new actions_core_1.IntegrationError('The token provided must have admin privileges.', actions_core_1.ErrorCodes.INVALID_AUTHENTICATION, 400);
            }
        }
    },
    extendRequest({ settings }) {
        return {
            headers: { Authorization: `Bearer ${settings.accessToken}` }
        };
    },
    onDelete: async (request, { settings, payload }) => {
        return request((0, routes_1.privacyEndpoint)(settings.workspaceId), {
            method: 'post',
            json: {
                customer: {
                    id: payload.userId
                },
                source: 'segment'
            }
        });
    },
    presets: presets_1.presets,
    actions: {
        saveOrder: saveOrder_1.default,
        saveUser: saveUser_1.default,
        saveBaseEvent: saveBaseEvent_1.default,
        saveCartEvent: saveCartEvent_1.default,
        saveCheckoutEvent: saveCheckoutEvent_1.default,
        saveCollectionEvent: saveCollectionEvent_1.default,
        saveCustomEvent: saveCustomEvent_1.default,
        saveFormEvent: saveFormEvent_1.default,
        saveProductEvent: saveProductEvent_1.default,
        saveSearchEvent: saveSearchEvent_1.default
    }
};
exports.default = destination;
//# sourceMappingURL=index.js.map