"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HomeController = void 0;
const decorator_1 = require("@midwayjs/decorator");
const fse = require("fs-extra");
const path = require("path");
const recursive_readdir_async_1 = require("recursive-readdir-async");
const magic_1 = require("../magic");
let HomeController = class HomeController {
    constructor() {
        this.typesPath = path.join(__dirname, '../..', 'typesPath');
    }
    async configJs() {
        this.ctx.ignoreFormat = true;
        if (await fse.pathExists(this.magicConfig.editorConfigPath)) {
            return await fse.readFile(this.magicConfig.editorConfigPath);
        }
        this.ctx.status = 404;
    }
    /**
     * 版本号
     */
    async version() {
        return {
            version: (await fse.readJSON(path.join(__dirname, '../..', 'package.json'))).version,
        };
    }
    /**
     * swagger定义
     */
    async swaggerSpec() {
        if (this.magicConfig.swagger.enabled) {
            return this.magicApi.generateSpec();
        }
        else {
            this.ctx.ignoreFormat = true;
            this.ctx.status = 404;
        }
    }
    /**
     * d.ts类型路径
     */
    async types() {
        const result = [];
        const typeFilePaths = magic_1.MagicProvideContainer.getTypeFilePaths();
        for (const typeFilePath of typeFilePaths) {
            let types = await (0, recursive_readdir_async_1.list)(typeFilePath);
            if ('error' in types) {
                types = [];
            }
            result.push(...types);
        }
        return [
            ...new Set(result
                .filter(f => f.name.endsWith('.d.ts'))
                .map(f => f.fullname
                .replace(this.typesPath.replace(/\\/g, '/'), '')
                .replace(this.magicConfig.typesPath.replace(/\\/g, '/'), ''))),
        ];
    }
    /**
     * 获取d.ts文件
     */
    async typeFile() {
        this.ctx.ignoreFormat = true;
        const typeFilePaths = magic_1.MagicProvideContainer.getTypeFilePaths();
        for (const typeFilePath of typeFilePaths) {
            const filePath = path.join(typeFilePath, this.ctx.path);
            if (await fse.pathExists(filePath)) {
                return await fse.readFile(filePath);
            }
        }
        this.ctx.status = 404;
    }
};
__decorate([
    (0, decorator_1.Inject)(),
    __metadata("design:type", magic_1.MagicApi)
], HomeController.prototype, "magicApi", void 0);
__decorate([
    (0, decorator_1.Inject)(),
    __metadata("design:type", Object)
], HomeController.prototype, "ctx", void 0);
__decorate([
    (0, decorator_1.Config)('magic'),
    __metadata("design:type", Object)
], HomeController.prototype, "magicConfig", void 0);
__decorate([
    (0, decorator_1.Get)('/js/config.js'),
    (0, decorator_1.ContentType)('application/javascript'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], HomeController.prototype, "configJs", null);
__decorate([
    (0, decorator_1.Get)('/version'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], HomeController.prototype, "version", null);
__decorate([
    (0, decorator_1.Get)('/docs/spec'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], HomeController.prototype, "swaggerSpec", null);
__decorate([
    (0, decorator_1.Get)('/types'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], HomeController.prototype, "types", null);
__decorate([
    (0, decorator_1.Get)('/types/*'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], HomeController.prototype, "typeFile", null);
HomeController = __decorate([
    (0, decorator_1.Controller)('/')
], HomeController);
exports.HomeController = HomeController;
//# sourceMappingURL=home.controller.js.map