function createIframeEvent(targetWinow, targetOrigin) {
  const EVENTS = {};
  function receiveMessage(event) {
    const { data } = event;
    const eventHandlers = EVENTS[data.event] || [];
    if (eventHandlers.length) {
      eventHandlers.forEach((handler) => {
        const args = data.args || [];
        handler(...args);
      });
    }
  }
  window.addEventListener("message", receiveMessage, false);
  return {
    $emit(event, ...args) {
      targetWinow.postMessage({ event, args }, targetOrigin || "*");
    },
    $on(event, fn) {
      if (!EVENTS[event]) {
        EVENTS[event] = [];
      }
      EVENTS[event].push(fn);
    },
    $off(event, fn) {
      if (!fn) {
        EVENTS[event] = [];
        return;
      }
      const handlers = EVENTS[event] || [];
      EVENTS[event] = handlers.filter((handler) => handler !== fn);
    }
  };
}
export { createIframeEvent as c };
