"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.externalServer = 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 fields = {
    extension: {
        type: extensions_core_1.FieldType.STRING,
        id: 'extension',
        label: 'Extension Key',
        description: 'The key of the extension you want to use',
        required: true,
    },
    action: {
        type: extensions_core_1.FieldType.STRING,
        id: 'action',
        label: 'Action Key',
        description: 'The key of the action you want to use',
        required: true,
    },
    input: {
        type: extensions_core_1.FieldType.JSON,
        id: 'input',
        label: 'Input',
        description: "The input (in JSON format: {'fields': {key: val}, 'settings': {key: val}})",
    },
};
const dataPoints = {
    data_points: {
        key: 'data_points',
        valueType: 'string',
    },
};
const PayloadSchema = zod_1.z.object({
    fields: zod_1.z.object({
        extension: zod_1.z.string(),
        action: zod_1.z.string(),
        input: zod_1.z.any(),
    }),
    settings: zod_1.z.object({
        url: zod_1.z.string(),
    }),
});
exports.externalServer = {
    key: 'externalServer',
    title: 'External Server',
    description: 'An extension action used to prototype extensions on your local machine.',
    category: extensions_core_2.Category.DATA,
    fields,
    dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a;
        const { fields, settings } = PayloadSchema.parse(payload);
        const clientPayload = (_a = fields.input) !== null && _a !== void 0 ? _a : { fields: {}, settings: {} };
        const { data, status } = await axios_1.default.post(`${settings.url}/${fields.extension}/${fields.action}`, { data: clientPayload }, { headers: { 'Content-Type': 'application/json' } });
        // eslint-disable-next-line @typescript-eslint/naming-convention
        const { data_points, events, response } = data;
        if (status === 200 && response === 'success') {
            await onComplete({
                data_points: { data_points: JSON.stringify(data_points) },
            });
        }
        else {
            await onError({ events });
        }
    },
};
//# sourceMappingURL=external-extension.js.map