"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
var DescribeView_1;
Object.defineProperty(exports, "__esModule", { value: true });
"use strict";
const path = __importStar(require("path"));
const mock_1 = require("./mock");
const server_1 = require("@methodus/server");
function getBridge() {
    return global.METHODUS_BRIDGE;
}
let DescribeView = DescribeView_1 = class DescribeView {
    static maybeMethodus(object) {
        const proto = object.prototype;
        if (proto && proto.constructor && proto.constructor.methodus) {
            return proto.constructor.methodus[object.name];
        }
        if (!proto && object.__proto__ && object.__proto__.methodus) {
            return object.__proto__.methodus[object.name];
        }
        if (object.methodus) {
            return object.methodus[object.name];
        }
        return proto.methodus[object.name];
    }
    static getMethodusData() {
        return __awaiter(this, void 0, void 0, function* () {
            const data = getBridge();
            const routes = [];
            Object.keys(data.classes).forEach((cls) => {
                const methodus = DescribeView_1.maybeMethodus(data.classes[cls].classType);
                let pj = null;
                try {
                    pj = require(path.join(process.cwd(), 'node_modules', methodus.name, 'package.json'));
                }
                catch (error) {
                    pj = { 'version': 'NA' };
                }
                routes.push({ name: cls, methodus, info: pj });
            });
            return new server_1.MethodResult(routes);
        });
    }
    static getMethodusDataClass(className) {
        return __awaiter(this, void 0, void 0, function* () {
            const data = getBridge();
            const routes = [];
            const methodus = DescribeView_1.maybeMethodus(data.classes[className].classType);
            let pj = null;
            try {
                pj = require(path.join(process.cwd(), 'node_modules', methodus.name, 'package.json'));
            }
            catch (error) {
                pj = { 'version': 'NA' };
            }
            routes.push({ name: className, methodus, info: pj });
            return new server_1.MethodResult(routes);
        });
    }
    static describeproxy(applicationEndpoint, applicationName) {
        return __awaiter(this, void 0, void 0, function* () {
            return new server_1.MethodResult({});
        });
    }
    static dashboard() {
        return __awaiter(this, void 0, void 0, function* () {
            // const str = fs.readFileSync(path.join(clientDir, 'tabs/dashboard_tabs.ejs'), 'utf-8');
            // const template = ejs.compile(str, { filename: path.join(clientDir, 'tabs/dashboard_tabs.ejs') });
            const data = getBridge();
            const packageJson = require(path.join(process.cwd(), 'package.json'));
            const routes = [];
            const ignoreInClasse = ['DescribeView', 'ConfigView'];
            Object.keys(data.classes).filter(cls => ignoreInClasse.indexOf(cls) === -1)
                .forEach((cls) => {
                const methodus = DescribeView_1.maybeMethodus(data.classes[cls].classType);
                const pj = { 'version': getVersionFromPackageFile(methodus.name) };
                routes.push({ info: pj, active: true, methodus, name: cls });
            });
            const remoteRoutes = [];
            Object.keys(data.clients).forEach((cls) => {
                const methodus = DescribeView_1.maybeMethodus(data.clients[cls].classType);
                const pj = { 'version': getVersionFromPackageFile(methodus.name) };
                remoteRoutes.push({ info: pj, active: true, methodus, name: cls });
            });
            const result = Object.assign({}, {
                routes, remoteRoutes
            }, { app: { version: packageJson.version, name: packageJson.name } });
            return new server_1.MethodResult(result);
        });
    }
    static action(className, actionKey) {
        return __awaiter(this, void 0, void 0, function* () {
            const data = getBridge();
            const testedClass = data.classes[className] || data.clients[className];
            const methodus = DescribeView_1.maybeMethodus(testedClass.classType);
            const result = Object.assign({}, { methodus, cls: testedClass.classType, actionKey });
            return new server_1.MethodResult(result);
        });
    }
};
__decorate([
    server_1.Method("GET" /* Get */, '/describe/methodus'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], DescribeView, "getMethodusData", null);
__decorate([
    server_1.Method("GET" /* Get */, '/describe/methodus/:className'),
    __param(0, server_1.Param('className')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], DescribeView, "getMethodusDataClass", null);
__decorate([
    server_1.Method("GET" /* Get */, '/describeproxy/:path'),
    __param(0, server_1.Query('u')), __param(1, server_1.Param('path')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], DescribeView, "describeproxy", null);
__decorate([
    server_1.MethodMock(mock_1.Mock.dashbaord),
    server_1.Method("GET" /* Get */, '/describe/dashboard'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], DescribeView, "dashboard", null);
__decorate([
    server_1.MethodMock(mock_1.Mock.action),
    server_1.Method("GET" /* Get */, '/describe/test/:className/:actionKey'),
    __param(0, server_1.Param('className')), __param(1, server_1.Param('actionKey')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], DescribeView, "action", null);
DescribeView = DescribeView_1 = __decorate([
    server_1.MethodConfig('DescribeView')
], DescribeView);
exports.DescribeView = DescribeView;
function getVersionFromPackageFile(name) {
    try {
        const pj = require(path.join(process.cwd(), 'node_modules', name, 'package.json'));
        return pj.version;
    }
    catch (error) {
    }
}
//# sourceMappingURL=describeView.js.map