"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const ejs = require("ejs");
let metadataKey = 'params';
let methodMetadataKey = 'methodus';
const fs = require("fs");
const path = require("path");
const server_1 = require("@methodus/server");
//import { Config } from '@tmla-contracts/config';
const clientDir = path.resolve(path.join(__dirname, '../client'));
var urlBuilder = require('url');
function fullUrl(req) {
    if (req.headers['referer']) {
        const urlArr = req.headers['referer'].split('/describe')[0].split('://');
        const originalUrl = req.originalUrl.split('/describe')[0];
        return urlBuilder.format({
            protocol: '//',
            host: urlArr[1],
            pathname: originalUrl + '/describe/'
        });
    }
}
let ConfigView = class ConfigView {
    constructor(expressInstance) {
        this.expressInstance = expressInstance;
        // this._app.use(describe.init());
    }
    static maybeMethodus(object) {
        let proto = object.prototype;
        if (proto && proto.constructor && proto.constructor.methodus)
            return proto.constructor.methodus;
        if (!proto && object.__proto__ && object.__proto__.methodus)
            return object.__proto__.methodus;
        if (object.methodus)
            return object.methodus;
        return proto.methodus;
    }
    configManager(req) {
        return __awaiter(this, void 0, void 0, function* () {
            let str = fs.readFileSync(path.join(clientDir, 'configManager.ejs'), 'utf-8');
            var template = ejs.compile(str, { filename: path.join(clientDir, 'configManager.ejs') });
            let appsAndEnvs = { result: { applications: [], envs: [] } }; // await Config.getApplications();
            // appsAndEnvs.result.applications = appsAndEnvs.result.applications.map(item => item.id);
            // appsAndEnvs.result.envs = appsAndEnvs.result.envs.map(item => item.id);
            let result = template(Object.assign({}, global.METHODUS_BRIDGE, { base: fullUrl(req) }, appsAndEnvs));
            return new server_1.MethodResult(result);
        });
    }
    config(req) {
        return __awaiter(this, void 0, void 0, function* () {
            let str = fs.readFileSync(path.join(clientDir, 'config.ejs'), 'utf-8');
            var template = ejs.compile(str, { filename: path.join(clientDir, 'config.ejs') });
            const packageJson = require(path.join(process.cwd(), 'package.json'));
            let appsAndEnvsResult = {}; //await Config.getApplications();
            let appsAndEnvs = {
                apps: [],
                envs: []
                // apps: appsAndEnvsResult.result.applications.map(item => item.id),
                // envs: appsAndEnvsResult.result.envs.map(item => item.id)
            };
            let result = template(Object.assign({}, global.METHODUS_BRIDGE, { app: packageJson }, { appsAndEnvs: appsAndEnvs }, { config: global.tmla.config }, { base: fullUrl(req) }));
            return new server_1.MethodResult(result);
        });
    }
};
__decorate([
    server_1.Method("GET" /* Get */, '/config_manager'),
    __param(0, server_1.Request()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], ConfigView.prototype, "configManager", null);
__decorate([
    server_1.Method("GET" /* Get */, '/describe/config'),
    __param(0, server_1.Request()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], ConfigView.prototype, "config", null);
ConfigView = __decorate([
    server_1.MethodConfig('ConfigView'),
    __metadata("design:paramtypes", [Object])
], ConfigView);
exports.ConfigView = ConfigView;
//# sourceMappingURL=configView.js.map