/**
 * 实体数据服务注册中心
 *
 * @export
 * @class EntityServiceRegister
 */
export class EntityServiceRegister {

    /**
     * 所有实体数据服务Map
     *
     * @protected
     * @type {*}
     * @memberof EntityServiceRegister
     */
    protected allEntityService: Map<string, () => Promise<any>> = new Map();

    /**
     * 已加载实体数据服务Map缓存
     *
     * @protected
     * @type {Map<string, any>}
     * @memberof EntityServiceRegister
     */
    protected serviceCache: Map<string, any> = new Map();

    /**
     * Creates an instance of EntityServiceRegister.
     * @memberof EntityServiceRegister
     */
    constructor() {
        this.init();
    }

    /**
     * 初始化
     *
     * @protected
     * @memberof EntityServiceRegister
     */
    protected init(): void {
                this.allEntityService.set('dadimension', () => import('@/service/dadimension/dadimension-service'));
        this.allEntityService.set('dstsystem', () => import('@/service/dst-system/dst-system-service'));
        this.allEntityService.set('dstapp', () => import('@/service/dst-app/dst-app-service'));
        this.allEntityService.set('dareport', () => import('@/service/dareport/dareport-service'));
        this.allEntityService.set('metadataset', () => import('@/service/meta-data-set/meta-data-set-service'));
        this.allEntityService.set('ruleitem', () => import('@/service/rule-item/rule-item-service'));
        this.allEntityService.set('dachart', () => import('@/service/dachart/dachart-service'));
        this.allEntityService.set('dabuild', () => import('@/service/dabuild/dabuild-service'));
        this.allEntityService.set('dstdatasource', () => import('@/service/dst-data-source/dst-data-source-service'));
        this.allEntityService.set('metafield', () => import('@/service/meta-field/meta-field-service'));
        this.allEntityService.set('execlog', () => import('@/service/exec-log/exec-log-service'));
        this.allEntityService.set('execresult', () => import('@/service/exec-result/exec-result-service'));
        this.allEntityService.set('metarelationship', () => import('@/service/meta-relationship/meta-relationship-service'));
        this.allEntityService.set('dametric', () => import('@/service/dametric/dametric-service'));
        this.allEntityService.set('ruleengine', () => import('@/service/rule-engine/rule-engine-service'));
        this.allEntityService.set('metaentity', () => import('@/service/meta-entity/meta-entity-service'));
        this.allEntityService.set('metamodule', () => import('@/service/meta-module/meta-module-service'));
        this.allEntityService.set('metamodel', () => import('@/service/meta-model/meta-model-service'));
    }

    /**
     * 加载实体数据服务
     *
     * @protected
     * @param {string} serviceName
     * @returns {Promise<any>}
     * @memberof EntityServiceRegister
     */
    protected async loadService(serviceName: string): Promise<any> {
        const service = this.allEntityService.get(serviceName);
        if (service) {
            return service();
        }
    }

    /**
     * 获取应用实体数据服务
     *
     * @param {string} name
     * @returns {Promise<any>}
     * @memberof EntityServiceRegister
     */
    public async getService(name: string): Promise<any> {
        if (this.serviceCache.has(name)) {
            return this.serviceCache.get(name);
        }
        const entityService: any = await this.loadService(name);
        if (entityService && entityService.default) {
            const instance: any = new entityService.default();
            this.serviceCache.set(name, instance);
            return instance;
        }
    }

}
export const entityServiceRegister: EntityServiceRegister = new EntityServiceRegister();