"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.MagicConfiguration = void 0;
const axios = require("@midwayjs/axios");
const bull = require("@midwayjs/bull");
const cache = require("@midwayjs/cache");
const core_1 = require("@midwayjs/core");
const orm = require("@midwayjs/typeorm");
const upload = require("@midwayjs/upload");
const validate = require("@midwayjs/validate");
const DefaultConfig = require("./config/config.default");
const crossDomain = require("@midwayjs/cross-domain");
const staticFile = require("@midwayjs/static-file");
const filter_1 = require("./filter");
const magic_1 = require("./magic");
const middleware_1 = require("./middleware");
let MagicConfiguration = class MagicConfiguration {
    async onReady() {
        const apps = this.applicationManager.getApplications(['koa']);
        if (apps.length) {
            apps.forEach(app => {
                // add middleware
                app.useMiddleware([
                    middleware_1.ExecuteTimeMiddleware,
                    middleware_1.SwaggerMiddleware,
                    middleware_1.FormatMiddleware,
                    middleware_1.SecurityMiddleware,
                    middleware_1.MagicMiddleware,
                ]);
                // add filter
                app.useFilter([
                    filter_1.HttpExitErrorFilter,
                    filter_1.ValidateErrorFilter,
                    filter_1.AssertionErrorFilter,
                    filter_1.DefaultErrorFilter,
                ]);
            });
        }
        //添加提示文件路径
        const typesPath = this.configService.getConfiguration('magic.typesPath');
        magic_1.MagicProvideContainer.addTypeFilePath(typesPath);
    }
    async onServerReady() {
        await magic_1.MagicProvideContainer.init();
        const koaConfig = this.configService.getConfiguration('koa');
        const magicConfig = this.configService.getConfiguration('magic');
        const isHttps = koaConfig.key && koaConfig.cert;
        console.log('[ Magic-Node ] Start Magic-Editor at \x1B[34m%s\x1B[0m', `${isHttps ? 'https' : 'http'}://127.0.0.1:${process.env.MIDWAY_HTTP_PORT}/magic-editor`);
        if (magicConfig.openapi.enabled) {
            console.log('[ Magic-Node ] Start OpenApi at \x1B[34m%s\x1B[0m', `${isHttps ? 'https' : 'http'}://127.0.0.1:${process.env.MIDWAY_HTTP_PORT}/docs`);
        }
    }
};
__decorate([
    (0, core_1.Inject)(),
    __metadata("design:type", core_1.MidwayApplicationManager)
], MagicConfiguration.prototype, "applicationManager", void 0);
__decorate([
    (0, core_1.Inject)(),
    __metadata("design:type", core_1.MidwayConfigService)
], MagicConfiguration.prototype, "configService", void 0);
MagicConfiguration = __decorate([
    (0, core_1.Configuration)({
        namespace: 'magic',
        imports: [crossDomain, validate, staticFile, upload, axios, orm, cache, bull],
        importConfigs: [
            {
                default: DefaultConfig,
            },
        ],
    })
], MagicConfiguration);
exports.MagicConfiguration = MagicConfiguration;
