/**
 * 实体数据服务注册中心
 *
 * @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('userrole', () => import('@/service/user-role/user-role-service'));
        this.allEntityService.set('orguser', () => import('@/service/org-user/org-user-service'));
        this.allEntityService.set('usergroupdetail', () => import('@/service/user-group-detail/user-group-detail-service'));
        this.allEntityService.set('userroledatascope', () => import('@/service/user-role-data-scope/user-role-data-scope-service'));
        this.allEntityService.set('unires', () => import('@/service/uni-res/uni-res-service'));
        this.allEntityService.set('org', () => import('@/service/org/org-service'));
        this.allEntityService.set('dataentity', () => import('@/service/data-entity/data-entity-service'));
        this.allEntityService.set('application', () => import('@/service/application/application-service'));
        this.allEntityService.set('userobject', () => import('@/service/user-object/user-object-service'));
        this.allEntityService.set('usergroup', () => import('@/service/user-group/user-group-service'));
        this.allEntityService.set('userroledataaction', () => import('@/service/user-role-data-action/user-role-data-action-service'));
        this.allEntityService.set('userroledetail', () => import('@/service/user-role-detail/user-role-detail-service'));
        this.allEntityService.set('orgdept', () => import('@/service/org-dept/org-dept-service'));
        this.allEntityService.set('userroletype', () => import('@/service/user-role-type/user-role-type-service'));
        this.allEntityService.set('userroledata', () => import('@/service/user-role-data/user-role-data-service'));
        this.allEntityService.set('userroleres', () => import('@/service/user-role-res/user-role-res-service'));
        this.allEntityService.set('orgdeptuser', () => import('@/service/org-dept-user/org-dept-user-service'));
        this.allEntityService.set('user', () => import('@/service/user/user-service'));
        this.allEntityService.set('system', () => import('@/service/system/system-service'));
        this.allEntityService.set('loginaccountpwd', () => import('@/service/login-account-pwd/login-account-pwd-service'));
        this.allEntityService.set('userroledatas', () => import('@/service/user-role-datas/user-role-datas-service'));
        this.allEntityService.set('loginaccount', () => import('@/service/login-account/login-account-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();