/**
 * 实体数据服务注册中心
 *
 * @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('hroperationunit', () => import('@/service/hroperation-unit/hroperation-unit-service'));
        this.allEntityService.set('hromhierarchypurposeref', () => import('@/service/hromhierarchy-purpose-ref/hromhierarchy-purpose-ref-service'));
        this.allEntityService.set('hromhierarchycat', () => import('@/service/hromhierarchy-cat/hromhierarchy-cat-service'));
        this.allEntityService.set('hrorgcontact', () => import('@/service/hrorg-contact/hrorg-contact-service'));
        this.allEntityService.set('hremployee', () => import('@/service/hremployee/hremployee-service'));
        this.allEntityService.set('hrlegal', () => import('@/service/hrlegal/hrlegal-service'));
        this.allEntityService.set('hromhierarchypurpose', () => import('@/service/hromhierarchypurpose/hromhierarchypurpose-service'));
        this.allEntityService.set('hromhierarchy', () => import('@/service/hromhierarchy/hromhierarchy-service'));
        this.allEntityService.set('hrdistribution', () => import('@/service/hrdistribution/hrdistribution-service'));
        this.allEntityService.set('hrorgaddress', () => import('@/service/hrorg-address/hrorg-address-service'));
        this.allEntityService.set('hrpost', () => import('@/service/hrpost/hrpost-service'));
        this.allEntityService.set('hrorganization', () => import('@/service/hrorganization/hrorganization-service'));
        this.allEntityService.set('hrduty', () => import('@/service/hrduty/hrduty-service'));
        this.allEntityService.set('hrpostrel', () => import('@/service/hrpost-rel/hrpost-rel-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();