"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MagicProvideContainer = void 0;
const core_1 = require("@midwayjs/core");
const core_2 = require("./core");
const magic_api_1 = require("./magic.api");
const magic_async_task_1 = require("./magic.async.task");
const magic_datasource_1 = require("./magic.datasource");
const magic_function_1 = require("./magic.function");
const magic_interceptor_1 = require("./magic.interceptor");
const magic_log_1 = require("./magic.log");
const magic_schedule_task_1 = require("./magic.schedule.task");
const path = require("path");
/**
 * Magic处理器
 */
class MagicProvideContainer {
    /**
     * 增加d.ts文件目录
     * @param path
     */
    static addTypeFilePath(path) {
        MagicProvideContainer._MAGIC_TYPE_FILE_PATH.add(path);
    }
    /**
     * 获取d.ts文件目录
     */
    static getTypeFilePaths() {
        return MagicProvideContainer._MAGIC_TYPE_FILE_PATH.values();
    }
    /**
     * Magic处理器初始化
     */
    static async init() {
        for (const provide of [
            core_2.MagicResourceService,
            magic_log_1.MagicLog,
            ...MagicProvideContainer._MAGIC_PROVIDE_ARRAY,
        ]) {
            const definition = await (0, core_1.getCurrentMainApp)()
                .getApplicationContext()
                .getAsync(provide);
            await definition.init();
        }
    }
    /**
     * 重新初始化
     */
    static async reInit() {
        for (const provide of MagicProvideContainer._MAGIC_PROVIDE_ARRAY) {
            const definition = await (0, core_1.getCurrentMainApp)()
                .getApplicationContext()
                .getAsync(provide);
            await definition.init(true);
        }
    }
    /**
     * 根据类型重新初始化
     */
    static async reInitByType(type) {
        for (const provide of MagicProvideContainer._MAGIC_PROVIDE_ARRAY) {
            const definition = await (0, core_1.getCurrentMainApp)()
                .getApplicationContext()
                .getAsync(provide);
            const magicDefinition = definition;
            if (magicDefinition.type === type) {
                await magicDefinition.init(true);
                return;
            }
        }
    }
    /**
     * 注册Magic处理器
     * @param provide 处理器
     */
    static async registerMagicProvide(provide) {
        this._MAGIC_PROVIDE_ARRAY.push(provide);
    }
    /**
     * 根据类型获取magic处理器
     * @param type 资源类型
     */
    static async getMagicProvide(type) {
        for (const provide of MagicProvideContainer._MAGIC_PROVIDE_ARRAY) {
            const definition = await (0, core_1.getCurrentMainApp)()
                .getApplicationContext()
                .getAsync(provide);
            if (definition.type === type) {
                return definition;
            }
        }
        throw new core_1.MidwayCommonError('未找到对应的处理器');
    }
    static async getAllResource() {
        const magicResourceService = await (0, core_1.getCurrentMainApp)()
            .getApplicationContext()
            .getAsync(core_2.MagicResourceService);
        const result = {};
        for (const provide of MagicProvideContainer._MAGIC_PROVIDE_ARRAY) {
            const definition = await (0, core_1.getCurrentMainApp)()
                .getApplicationContext()
                .getAsync(provide);
            const type = definition.type;
            if (type) {
                result[type] = await magicResourceService.getTreeByType(type);
            }
        }
        return result;
    }
}
exports.MagicProvideContainer = MagicProvideContainer;
MagicProvideContainer._MAGIC_TYPE_FILE_PATH = new Set([
    path.join(__dirname, '../..', 'types'),
]);
MagicProvideContainer._MAGIC_PROVIDE_ARRAY = [
    magic_datasource_1.MagicDataSource,
    magic_api_1.MagicApi,
    magic_function_1.MagicFunction,
    magic_interceptor_1.MagicInterceptor,
    magic_schedule_task_1.MagicScheduleTask,
    magic_async_task_1.MagicAsyncTask,
];
