"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-misused-promises */
const express_1 = __importDefault(require("express"));
const body_parser_1 = __importDefault(require("body-parser"));
const extensions_core_1 = require("@awell-health/extensions-core");
const extensions_1 = require("../extensions");
const app = (0, express_1.default)();
const port = 3000;
app.use(body_parser_1.default.json());
app.post('/', async (req, res) => {
    console.log(req.body);
    res.send('ok');
});
app.get('/:extension', async (req, res) => {
    const extensionKey = req.params.extension;
    const extension = extensions_1.extensions.find(({ key }) => key === extensionKey);
    if (extension === undefined) {
        res.status(404).send(`Extension ${extensionKey} not found`);
        return;
    }
    res.send(extension);
});
app.post('/:extension/:action', async (req, res) => {
    const extensionKey = req.params.extension;
    const actionKey = req.params.action;
    const extension = extensions_1.extensions.find(({ key }) => key === extensionKey);
    if (extension === undefined) {
        res.status(404).send(`Extension ${extensionKey} not found`);
        return;
    }
    const action = Object.values(extension.actions).find(({ key }) => key === actionKey);
    if (action === undefined) {
        res
            .status(404)
            .send(`Action ${actionKey} not found in extension ${extensionKey}`);
        return;
    }
    const payload = JSON.parse(req.body.data);
    console.log('incoming', payload);
    const onCompleteCb = createOnCompleteCallback(res);
    const onErrorCb = createOnErrorCallback(res);
    await action.onActivityCreated(payload, onCompleteCb, onErrorCb).catch((err) => {
        const error = new extensions_core_1.AwellError({
            error: err,
            action: actionKey,
            extension: extensionKey,
        });
        void onErrorCb({
            events: [
                {
                    text: { en: error.title },
                    date: error.date.toISOString(),
                    error: {
                        category: error.category,
                        message: error.message,
                    },
                },
            ],
        });
    });
    // const result = queue.shift()
    // res.send(result)
});
app.listen(port, () => {
    console.log(`Test server listening at http://localhost:${port}`);
});
const createOnCompleteCallback = (
// payload: NewActivityPayload,
res) => {
    return async (params = {}) => {
        console.log({ ...params, response: 'success' });
        res.send({ ...params, response: 'success' });
        // queue.push({ ...params, response: 'success' })
    };
};
const createOnErrorCallback = (
// payload: NewActivityPayload,
res) => {
    return async (params = {}) => {
        console.error({ ...params, response: 'failure' });
        res.send({ ...params, response: 'failure' });
        // queue.push({ ...params, response: 'failure' })
    };
};
//# sourceMappingURL=test-server.js.map