

class EventInfo{
    public constructor(public callback:Function,public target?:any){}
}


export default class EventMgr{

    private static _infoMap:Map<string,Array<EventInfo>>=new Map()

    public static on(key:string,cb:Function,target?:any){
        let arr=this._infoMap.get(key)
        if(!arr){
            arr=new Array()
            this._infoMap.set(key,arr)
        }
        arr.push(new EventInfo(cb,target))
    }

    public static off(key:string,cb:Function,target?:any){
        let arr=this._infoMap.get(key)
        if(!arr) return
        for(let i=arr.length-1;i>=0;i--){
            let info=arr[i]
            if(target){
                if(info.callback==cb&&info.target==target){
                    arr.splice(i)
                }
            }else{
                if(info.callback==cb){
                    arr.splice(i)
                }
            }
        }
    }

    public static emit(key:string,arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any){
        let arr=this._infoMap.get(key)
        if(!arr||arr.length<=0) return
        for(let info of arr){
            info?.callback?.call(info?.target,arg1,arg2,arg3,arg4,arg5)
        }
    }
}