/**
 * UI服务注册中心
 *
 * @export
 * @class UIServiceRegister
 */
export class UIServiceRegister {

    /**
     * 所有UI实体服务Map
     *
     * @protected
     * @type {*}
     * @memberof UIServiceRegister
     */
    protected allUIService: Map<string, () => Promise<any>> = new Map();

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

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

    /**
     * 初始化
     *
     * @protected
     * @memberof UIServiceRegister
     */
    protected init(): void {
                this.allUIService.set('task', () => import('@/uiservice/task/task-ui-service'));
        this.allUIService.set('letter', () => import('@/uiservice/letter/letter-ui-service'));
        this.allUIService.set('pricelevel', () => import('@/uiservice/price-level/price-level-ui-service'));
        this.allUIService.set('email', () => import('@/uiservice/email/email-ui-service'));
        this.allUIService.set('serviceappointment', () => import('@/uiservice/service-appointment/service-appointment-ui-service'));
        this.allUIService.set('transactioncurrency', () => import('@/uiservice/transaction-currency/transaction-currency-ui-service'));
        this.allUIService.set('opportunity', () => import('@/uiservice/opportunity/opportunity-ui-service'));
        this.allUIService.set('goal', () => import('@/uiservice/goal/goal-ui-service'));
        this.allUIService.set('invoice', () => import('@/uiservice/invoice/invoice-ui-service'));
        this.allUIService.set('lead', () => import('@/uiservice/lead/lead-ui-service'));
        this.allUIService.set('incident', () => import('@/uiservice/incident/incident-ui-service'));
        this.allUIService.set('ibizlist', () => import('@/uiservice/ibiz-list/ibiz-list-ui-service'));
        this.allUIService.set('campaignresponse', () => import('@/uiservice/campaign-response/campaign-response-ui-service'));
        this.allUIService.set('phonecall', () => import('@/uiservice/phone-call/phone-call-ui-service'));
        this.allUIService.set('appointment', () => import('@/uiservice/appointment/appointment-ui-service'));
        this.allUIService.set('salesliterature', () => import('@/uiservice/sales-literature/sales-literature-ui-service'));
        this.allUIService.set('activitypointer', () => import('@/uiservice/activity-pointer/activity-pointer-ui-service'));
        this.allUIService.set('fax', () => import('@/uiservice/fax/fax-ui-service'));
        this.allUIService.set('quote', () => import('@/uiservice/quote/quote-ui-service'));
        this.allUIService.set('contact', () => import('@/uiservice/contact/contact-ui-service'));
        this.allUIService.set('product', () => import('@/uiservice/product/product-ui-service'));
        this.allUIService.set('competitor', () => import('@/uiservice/competitor/competitor-ui-service'));
        this.allUIService.set('account', () => import('@/uiservice/account/account-ui-service'));
        this.allUIService.set('salesorder', () => import('@/uiservice/sales-order/sales-order-ui-service'));
    }

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

    /**
     * 获取应用实体服务
     *
     * @param {string} name
     * @returns {Promise<any>}
     * @memberof UIServiceRegister
     */
    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 uiServiceRegister: UIServiceRegister = new UIServiceRegister();