import { Injectable } from '@angular/core';
import { AppEnvironment } from '../../environments/AppEnvironment';
import { CodeItem } from '@global/interface/CodeItem';
import { Http } from './Http';

@Injectable({ providedIn: 'root' })
export class CodeList {

    private codelistMap:Map<string,any> =new Map<string,any>();

    constructor(private $http:Http) {
        
    }

    /**
     * 初始化代码表
     */
    public initCodelist():Promise<any>{
        return  new Promise((resolve: any, reject: any) =>{
            if(this.codelistMap.size === 0){
                this.$http.post(`${AppEnvironment.AppName.toLocaleLowerCase()}/app/codelist/getall`).then((response:any) =>{
                    if(response){
                        response.forEach((element:any) => {
                            let target:any ={items:[]};
                            Object.assign(target,element);
                            this.setCodeItem(element.srfkey,target);
                        });
                        resolve(true);
                    }else{
                        reject(false);
                        console.log('代码表加载失败');
                    }
                })
            }else{
                resolve(true);
            }
        })

    }

    public getCodeItemByTag(srfkey:string){
        return  this.codelistMap.get(srfkey);
    }

    public setCodeItem(srfkey:string,codeitem:any){
        this.codelistMap.set(srfkey,codeitem);
    }

}
