import mitt from 'mitt'
const emitter = mitt()

export const useMittHook = () => {

  const emit = (eventName, params) => {
    emitter.emit(eventName, params)
    emitter.emit(`${eventName}Once`, eventName)
  }

  const on = (eventName, fun) => {
    emitter.on(eventName, fun)
  }

  const once = (eventName, fun) => {
    emitter.on(eventName, fun)
    emitter.on(`${eventName}Once`, (en) => {
      emitter.off(en)
    })
  }

  const off = (eventName, params?) => {
    emitter.off(eventName, params)
  }

  const clearAll = () => {
    emitter.all.clear()
  }

  return {
    emit,
    on,
    once,
    off,
    clearAll
  }
}