'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

/*!
 * @techmely/utils
 * Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com>
 * MIT Licensed
 */


// src/emitter.ts
function emitter(all) {
  const events = all || /* @__PURE__ */ new Map();
  const getHandlers = (type) => events.get(type);
  function on(type, handler) {
    const handlers = getHandlers(type);
    if (handlers) {
      handlers.push(handler);
    } else {
      events.set(type, [handler]);
    }
  }
  function off(type, handler) {
    const handlers = getHandlers(type);
    if (handlers) {
      if (handler) {
        const indexHandler = handlers.indexOf(handler) >>> 0;
        handlers.splice(indexHandler, 1);
      } else {
        events.set(type, []);
      }
    }
  }
  function emit(type, event) {
    let handlers = getHandlers(type);
    if (handlers) {
      for (const handler of handlers) {
        if (event)
          handler(event);
      }
    }
    handlers = events.get("*");
    if (handlers) {
      for (const handler of handlers) {
        if (event)
          handler(type, event);
      }
    }
  }
  return {
    events,
    on,
    off,
    emit
  };
}
var emitter_default = emitter;

exports.default = emitter_default;
exports.emitter = emitter;
