const listeners = new Map();

let sendMessage = () => {};

export function setMessageSender(sender) {
  sendMessage = sender;
}

export function sendIPC(channel, data) {
  sendMessage(channel, data);
}

export function onIPCData(channel, callback) {
  if (!listeners.has(channel)) {
    listeners.set(channel, new Set());
  }
  listeners.get(channel).add(callback);
}

export function removeAllListeners(channel) {
  listeners.delete(channel);
}

export function dispatchIPC(channel, data) {
  const channelListeners = listeners.get(channel);
  if (!channelListeners) {
    return;
  }

  for (const callback of channelListeners) {
    callback(data);
  }
}
