"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DevConsoleController = void 0;
exports.getDevConsoleAssetsRoot = getDevConsoleAssetsRoot;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-server-foundation/type-metadata-runtime"));
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const http_1 = require("../http");
const security_1 = require("./security");
const __tsf_metadata_type = _a.createCompactMetadataRegistryV1("[1,[{\"kind\":13,\"typeName\":\"HttpPath\",\"types\":[{\"kind\":6},{\"kind\":2,\"typeName\":\"HttpPath\",\"annotations\":{\"httpPath\":{\"kind\":18,\"types\":[{\"kind\":20,\"name\":\"type\",\"type\":{\"kind\":6},\"optional\":false}]}}}]}]]", []);
let _DevConsoleControllerMetadata_1;
let DevConsoleController = class DevConsoleController {
    app;
    constructor(app) {
        this.app = app;
    }
    index() {
        return this.serveFile('index.html');
    }
    asset(path) {
        return this.serveFile((0, node_path_1.join)('assets', (0, node_path_1.basename)(path)));
    }
    serveFile(relativePath) {
        const root = getDevConsoleAssetsRoot();
        const normalized = (0, node_path_1.normalize)(relativePath);
        const file = (0, node_path_1.join)(root, normalized);
        if (!file.startsWith(root) || !(0, node_fs_1.existsSync)(file))
            return (0, http_1.rawResponse)('Not Found', {
                statusCode: 404,
                contentType: 'text/plain; charset=utf-8'
            });
        return (0, http_1.rawResponse)((0, node_fs_1.readFileSync)(file), { contentType: getContentType(file) });
    }
    static __tsfType = _DevConsoleControllerMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"DevConsoleController\",\"typeName\":\"DevConsoleController\",\"classType\":{\"$tsf\":0},\"properties\":[],\"methods\":[{\"name\":\"index\",\"parameters\":[],\"returnType\":{\"kind\":16,\"typeName\":\"RawResponseResult\",\"classType\":{\"$tsfImport\":[1,\"../http/index\",\"RawResponseResult\"]}}},{\"name\":\"asset\",\"parameters\":[{\"name\":\"path\",\"type\":{\"$tsfType\":0},\"optional\":false,\"default\":false}],\"returnType\":{\"kind\":16,\"typeName\":\"RawResponseResult\",\"classType\":{\"$tsfImport\":[1,\"../http/index\",\"RawResponseResult\"]}}}],\"hasConstructor\":true,\"constructorParameters\":[{\"name\":\"app\",\"type\":{\"kind\":16,\"typeName\":\"App\",\"classType\":{\"$tsfImport\":[1,\"../app/index\",\"App\"]},\"typeArguments\":[{\"kind\":1}]},\"optional\":false,\"default\":false}]}]", [() => DevConsoleController, require], __tsf_metadata_type);
};
exports.DevConsoleController = DevConsoleController;
tslib_1.__decorate([
    http_1.http.GET(),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", []),
    tslib_1.__metadata("design:returntype", Function)
], DevConsoleController.prototype, "index", null);
tslib_1.__decorate([
    http_1.http.GET('/assets/:path'),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [Object]),
    tslib_1.__metadata("design:returntype", Function)
], DevConsoleController.prototype, "asset", null);
exports.DevConsoleController = DevConsoleController = tslib_1.__decorate([
    http_1.http.controller('/_devconsole'),
    http_1.http.middleware(security_1.DevConsoleLocalhostMiddleware),
    tslib_1.__metadata("design:paramtypes", [Function])
], DevConsoleController);
_DevConsoleControllerMetadata_1.classType = DevConsoleController;
DevConsoleController.__tsfType = _DevConsoleControllerMetadata_1;
function getDevConsoleAssetsRoot() {
    return (0, node_path_1.join)(__dirname, '..', '..', 'devconsole');
}
function getContentType(file) {
    switch ((0, node_path_1.extname)(file)) {
        case '.html':
            return 'text/html; charset=utf-8';
        case '.js':
            return 'text/javascript; charset=utf-8';
        case '.css':
            return 'text/css; charset=utf-8';
        case '.json':
            return 'application/json; charset=utf-8';
        case '.svg':
            return 'image/svg+xml';
        case '.png':
            return 'image/png';
        case '.ico':
            return 'image/x-icon';
        default:
            return 'application/octet-stream';
    }
}
//# sourceMappingURL=controller.js.map