"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mtls = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const extensions_core_2 = require("@awell-health/extensions-core");
const zod_1 = require("zod");
const axios_1 = __importDefault(require("axios"));
const lodash_1 = require("lodash");
const fields = {
    payload: {
        type: extensions_core_1.FieldType.JSON,
        id: 'payload',
        label: 'Payload',
        description: "Payload (in JSON format: {'fields': {key: val}, 'settings': {key: val}})",
    },
};
const dataPoints = {
    response: {
        key: 'response',
        valueType: 'string',
    },
};
const PayloadSchema = zod_1.z.object({
    fields: zod_1.z.object({
        payload: zod_1.z.any(),
    }),
    settings: zod_1.z.object({
        url: zod_1.z.string(),
    }),
});
exports.mtls = {
    key: 'mtls',
    title: 'mTLS Connect',
    description: 'An extension action that communicates using mTLS by sending a POST request.',
    category: extensions_core_2.Category.DATA,
    fields,
    dataPoints,
    previewable: true,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        var _a;
        if ((0, lodash_1.isNil)(helpers)) {
            throw new Error('Helpers are not provided');
        }
        const { fields, settings } = PayloadSchema.parse(payload);
        const clientPayload = (_a = fields.payload) !== null && _a !== void 0 ? _a : {};
        const { data, status } = await axios_1.default.post(`${settings.url}`, { data: clientPayload }, {
            headers: { 'Content-Type': 'application/json' },
            httpsAgent: helpers.httpsAgent(),
        });
        if (status === 200) {
            await onComplete({
                data_points: { response: JSON.stringify(data) },
            });
        }
        else {
            throw new Error(`Error: ${JSON.stringify(data)}`);
        }
    },
};
//# sourceMappingURL=mtls.js.map