/**
 * 实体数据服务注册中心
 *
 * @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('opportunityproduct', () => import('@/service/opportunity-product/opportunity-product-service'));
        this.allEntityService.set('campaignlist', () => import('@/service/campaign-list/campaign-list-service'));
        this.allEntityService.set('task', () => import('@/service/task/task-service'));
        this.allEntityService.set('letter', () => import('@/service/letter/letter-service'));
        this.allEntityService.set('pricelevel', () => import('@/service/price-level/price-level-service'));
        this.allEntityService.set('email', () => import('@/service/email/email-service'));
        this.allEntityService.set('serviceappointment', () => import('@/service/service-appointment/service-appointment-service'));
        this.allEntityService.set('transactioncurrency', () => import('@/service/transaction-currency/transaction-currency-service'));
        this.allEntityService.set('opportunity', () => import('@/service/opportunity/opportunity-service'));
        this.allEntityService.set('goal', () => import('@/service/goal/goal-service'));
        this.allEntityService.set('invoice', () => import('@/service/invoice/invoice-service'));
        this.allEntityService.set('campaign', () => import('@/service/campaign/campaign-service'));
        this.allEntityService.set('lead', () => import('@/service/lead/lead-service'));
        this.allEntityService.set('incident', () => import('@/service/incident/incident-service'));
        this.allEntityService.set('ibizservice', () => import('@/service/ibiz-service/ibiz-service-service'));
        this.allEntityService.set('ibizlist', () => import('@/service/ibiz-list/ibiz-list-service'));
        this.allEntityService.set('campaignresponse', () => import('@/service/campaign-response/campaign-response-service'));
        this.allEntityService.set('phonecall', () => import('@/service/phone-call/phone-call-service'));
        this.allEntityService.set('listcontact', () => import('@/service/list-contact/list-contact-service'));
        this.allEntityService.set('appointment', () => import('@/service/appointment/appointment-service'));
        this.allEntityService.set('campaigncampaign', () => import('@/service/campaign-campaign/campaign-campaign-service'));
        this.allEntityService.set('salesliterature', () => import('@/service/sales-literature/sales-literature-service'));
        this.allEntityService.set('activitypointer', () => import('@/service/activity-pointer/activity-pointer-service'));
        this.allEntityService.set('listaccount', () => import('@/service/list-account/list-account-service'));
        this.allEntityService.set('fax', () => import('@/service/fax/fax-service'));
        this.allEntityService.set('quote', () => import('@/service/quote/quote-service'));
        this.allEntityService.set('campaignactivity', () => import('@/service/campaign-activity/campaign-activity-service'));
        this.allEntityService.set('listlead', () => import('@/service/list-lead/list-lead-service'));
        this.allEntityService.set('opportunitycompetitor', () => import('@/service/opportunity-competitor/opportunity-competitor-service'));
        this.allEntityService.set('contact', () => import('@/service/contact/contact-service'));
        this.allEntityService.set('product', () => import('@/service/product/product-service'));
        this.allEntityService.set('competitor', () => import('@/service/competitor/competitor-service'));
        this.allEntityService.set('account', () => import('@/service/account/account-service'));
        this.allEntityService.set('salesorder', () => import('@/service/sales-order/sales-order-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();