"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DevConsoleController = void 0;
const tslib_1 = require("tslib");
function __assignType(fn, args) {
    fn.__type = args;
    return fn;
}
const http_1 = require("@deepkit/http");
const path_1 = require("path");
const send_1 = tslib_1.__importDefault(require("send"));
const dev_1 = require("../app/dev");
const state_1 = require("../app/state");
const devconsole_middleware_1 = require("./devconsole.middleware");
const STATIC_DIR = (0, path_1.resolve)(__dirname, '../../devconsole');
function serveStatic(req, res, path) {
    return (Promise.Ω = [['$']], new Promise(__assignType((resolve, reject) => {
        (0, send_1.default)(req, path, { root: STATIC_DIR })
            .on('error', __assignType((err) => {
            if (err.status === 404) {
                reject(new http_1.HttpNotFoundError());
            }
            else {
                reject(err);
            }
        }, ['status', 'err', '', 'PP\'4!M2""/#']))
            .on('end', resolve)
            .pipe(res);
    }, ['resolve', 'reject', '', 'P"2!"2""/#'])));
}
serveStatic.__type = [() => http_1.HttpRequest, 'req', () => http_1.HttpResponse, 'res', 'path', 'serveStatic', 'PP7!2"P7#2$&2%$`/&'];
let DevConsoleController = class DevConsoleController {
    serveIndex(req, res) {
        return serveStatic(req, res, 'index.html');
    }
    serveAsset(path, req, res) {
        return serveStatic(req, res, `assets/${path}`);
    }
    serveOpenApiSchema(res) {
        const app = state_1.globalState.currentApp;
        if (!app) {
            res.status(503);
            res.setHeader('content-type', 'application/json');
            res.end(JSON.stringify({ error: 'App not ready' }));
            return;
        }
        try {
            const schema = (0, dev_1.serializeOpenApiSchema)(app);
            res.setHeader('content-type', 'application/json; charset=utf-8');
            res.end(JSON.stringify(schema, undefined, 2));
        }
        catch (err) {
            res.status(500);
            res.setHeader('content-type', 'application/json');
            res.end(JSON.stringify({ error: String(err) }));
        }
    }
};
exports.DevConsoleController = DevConsoleController;
DevConsoleController.__type = [() => http_1.HttpRequest, 'req', () => http_1.HttpResponse, 'res', 'serveIndex', 'path', () => http_1.HttpRequest, () => http_1.HttpResponse, 'serveAsset', () => http_1.HttpResponse, 'serveOpenApiSchema', 'DevConsoleController', 'PP7!2"P7#2$$`0%P&2&P7\'2"P7(2$$`0)PP7*2$$0+5w,'];
tslib_1.__decorate([
    http_1.http.GET(''),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [http_1.HttpRequest, http_1.HttpResponse]),
    tslib_1.__metadata("design:returntype", Promise)
], DevConsoleController.prototype, "serveIndex", null);
tslib_1.__decorate([
    http_1.http.GET('assets/:path'),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, http_1.HttpRequest, http_1.HttpResponse]),
    tslib_1.__metadata("design:returntype", Promise)
], DevConsoleController.prototype, "serveAsset", null);
tslib_1.__decorate([
    http_1.http.GET('openapi.json'),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [http_1.HttpResponse]),
    tslib_1.__metadata("design:returntype", void 0)
], DevConsoleController.prototype, "serveOpenApiSchema", null);
exports.DevConsoleController = DevConsoleController = tslib_1.__decorate([
    (http_1.http.controller('/_devconsole').middleware(devconsole_middleware_1.DevConsoleLocalhostMiddleware))
], DevConsoleController);
//# sourceMappingURL=devconsole.controller.js.map