"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 sendCallCompleted_1 = __importDefault(require("./sendCallCompleted"));
const constants_1 = require("./constants");
function buildHeaders(accessToken) {
    return {
        Authorization: `Bearer ${accessToken}`,
        'Content-Type': 'application/json',
        Accept: 'application/json',
        'User-Agent': constants_1.SEGMENT_USER_AGENT
    };
}
const destination = {
    name: 'Voiceops',
    slug: 'actions-voiceops',
    mode: 'cloud',
    description: 'Send Segment call-completed events to Voiceops for call ingestion, conference bridge splitting, and agent attribution.',
    presets: [
        {
            name: 'Send Call Completed',
            subscribe: 'type = "track" and event = "Call Completed"',
            partnerAction: 'sendCallCompleted',
            mapping: (0, actions_core_1.defaultValues)(sendCallCompleted_1.default.fields),
            type: 'automatic'
        }
    ],
    authentication: {
        scheme: 'custom',
        fields: {
            accessToken: {
                label: 'Access Token',
                description: 'Your Voiceops access token.',
                type: 'password',
                required: true
            },
            baseUrl: {
                label: 'Base URL',
                description: 'Your Voiceops base URL.',
                type: 'string',
                format: 'uri',
                required: true,
                default: constants_1.DEFAULT_VOICEOPS_BASE_URL
            }
        },
        testAuthentication: async (request, { settings }) => {
            try {
                return await request((0, constants_1.getVoiceopsAuthenticationEndpoint)(settings.baseUrl), {
                    method: 'get',
                    headers: buildHeaders(settings.accessToken)
                });
            }
            catch (error) {
                const authError = error;
                const message = authError.response?.data?.message ?? authError.response?.data?.error ?? 'Invalid Voiceops access token';
                throw new actions_core_1.InvalidAuthenticationError(message);
            }
        }
    },
    extendRequest: ({ settings }) => {
        return {
            headers: buildHeaders(settings.accessToken)
        };
    },
    actions: {
        sendCallCompleted: sendCallCompleted_1.default
    }
};
exports.default = destination;
//# sourceMappingURL=index.js.map