// 自定义事件订阅
import { createRandomValue } from "../renderers/Lion/utils/utils";
import { uuidv4 } from "./helper";


export enum EventEnum {
  ClearVistiMap = 'ClearVistiMap', // tablerow清除可见列表的事件
  ShowVistiMap = 'ShowVistiMap', // tablerow展示快速编辑的功能
  ClearMappingAcahe = 'ClearMappingAcahe', // tablerow展示快速编辑的功能
  DomPrint = 'DetailPrint',
  ClearSelectCache = 'ClearSelectCache', // tablerow展示快速编辑的功能
}

interface Obj {
  [key: string]: {
    [key: string]: (...args: any[]) => void;
  };
}

class Sub {
  subs: Obj;

  constructor() {
    this.subs = {};
  }

  on(eventName: string, cb: (...res: any) => any) {
    const id = 'SUB-' + uuidv4();

    if (this.subs[eventName]) {
      this.subs[eventName][id] = cb;
    } else {
      this.subs[eventName] = { [id]: cb };
    }

    return id;
  }

  off(eventName: string, id: string = '') {
    if (id && this.subs?.[eventName]?.[id]) {
      delete this.subs?.[eventName]?.[id];
    }
    else if (!id)
      delete this.subs[eventName]
  }

  emit(key: string, ...args: any) {
    if (this.subs[key]) {
      for (const item of Object.values(this.subs[key])) {
        item(...args);
      }
    }
  }
}

export const EventSub = new Sub();
