import cluster from 'cluster';
function emitMessage(message) {
  var _process$send, _process;
  if (cluster.isWorker && !process.connected) return false;
  return ((_process$send = (_process = process).send) === null || _process$send === void 0 ? void 0 : _process$send.call(_process, message)) ??
  // @ts-expect-error: wrong typings `process.emit` return boolean
  process.emit('message', message);
}
export function emitWorkerMessage(message) {
  return emitMessage({
    scope: 'worker',
    ...message
  });
}
export function emitStoriesMessage(message) {
  return emitMessage({
    scope: 'stories',
    ...message
  });
}
export function emitTestMessage(message) {
  return emitMessage({
    scope: 'test',
    ...message
  });
}
export function emitWebpackMessage(message) {
  return emitMessage({
    scope: 'webpack',
    ...message
  });
}
export function emitDockerMessage(message) {
  return emitMessage({
    scope: 'docker',
    ...message
  });
}
export function emitShutdownMessage() {
  return emitMessage({
    scope: 'shutdown'
  });
}
function createHandlers() {
  return Object.assign(Object.create(null), {
    worker: new Set(),
    stories: new Set(),
    test: new Set(),
    webpack: new Set(),
    docker: new Set(),
    shutdown: new Set()
  });
}
const handlers = createHandlers();
const handler = message => {
  switch (message.scope) {
    case 'worker':
      return handlers.worker.forEach(h => h(message));
    case 'stories':
      return handlers.stories.forEach(h => h(message));
    case 'test':
      return handlers.test.forEach(h => h(message));
    case 'webpack':
      return handlers.webpack.forEach(h => h(message));
    case 'docker':
      return handlers.docker.forEach(h => h(message));
    case 'shutdown':
      return handlers.shutdown.forEach(h => h(message));
  }
};
process.on('message', handler);
export function sendStoriesMessage(target, message) {
  var _target$send;
  (_target$send = target.send) === null || _target$send === void 0 || _target$send.call(target, {
    scope: 'stories',
    ...message
  });
}
export function sendTestMessage(target, message) {
  var _target$send2;
  (_target$send2 = target.send) === null || _target$send2 === void 0 || _target$send2.call(target, {
    scope: 'test',
    ...message
  });
}
export function sendDockerMessage(target, message) {
  var _target$send3;
  (_target$send3 = target.send) === null || _target$send3 === void 0 || _target$send3.call(target, {
    scope: 'docker',
    ...message
  });
}
export function sendShutdownMessage(target) {
  var _target$send4;
  (_target$send4 = target.send) === null || _target$send4 === void 0 || _target$send4.call(target, {
    scope: 'shutdown'
  });
}
export function subscribeOn(scope, handler) {
  switch (scope) {
    case 'worker':
      {
        const workerHandler = handler;
        handlers.worker.add(workerHandler);
        return () => handlers.worker.delete(workerHandler);
      }
    case 'stories':
      {
        const storiesHandler = handler;
        handlers.stories.add(storiesHandler);
        return () => handlers.stories.delete(storiesHandler);
      }
    case 'test':
      {
        const testHandler = handler;
        handlers.test.add(testHandler);
        return () => handlers.test.delete(testHandler);
      }
    case 'webpack':
      {
        const webpackHandler = handler;
        handlers.webpack.add(webpackHandler);
        return () => handlers.webpack.delete(webpackHandler);
      }
    case 'docker':
      {
        const dockerHandler = handler;
        handlers.docker.add(dockerHandler);
        return () => handlers.docker.delete(dockerHandler);
      }
    case 'shutdown':
      {
        const shutdownHandler = handler;
        handlers.shutdown.add(shutdownHandler);
        return () => handlers.shutdown.delete(shutdownHandler);
      }
  }
}
const workers = new Map();
export function subscribeOnWorker(worker, scope, handler) {
  const workerHandlers = workers.get(worker) ?? createHandlers();
  if (!workers.has(worker)) {
    workers.set(worker, workerHandlers);
    worker.once('exit', () => workers.delete(worker));
    worker.on('message', message => {
      switch (message.scope) {
        case 'worker':
          return workerHandlers.worker.forEach(h => h(message));
        case 'stories':
          return workerHandlers.stories.forEach(h => h(message));
        case 'test':
          return workerHandlers.test.forEach(h => h(message));
        case 'webpack':
          return workerHandlers.webpack.forEach(h => h(message));
        case 'docker':
          return workerHandlers.docker.forEach(h => h(message));
        case 'shutdown':
          return workerHandlers.shutdown.forEach(h => h(message));
      }
    });
  }
  switch (scope) {
    case 'worker':
      {
        const workerHandler = handler;
        workerHandlers.worker.add(workerHandler);
        return () => workerHandlers.worker.delete(workerHandler);
      }
    case 'stories':
      {
        const storiesHandler = handler;
        workerHandlers.stories.add(storiesHandler);
        return () => workerHandlers.stories.delete(storiesHandler);
      }
    case 'test':
      {
        const testHandler = handler;
        workerHandlers.test.add(testHandler);
        return () => workerHandlers.test.delete(testHandler);
      }
    case 'webpack':
      {
        const webpackHandler = handler;
        workerHandlers.webpack.add(webpackHandler);
        return () => workerHandlers.webpack.delete(webpackHandler);
      }
    case 'docker':
      {
        const dockerHandler = handler;
        workerHandlers.docker.add(dockerHandler);
        return () => workerHandlers.docker.delete(dockerHandler);
      }
    case 'shutdown':
      {
        const shutdownHandler = handler;
        workerHandlers.shutdown.add(shutdownHandler);
        return () => workerHandlers.shutdown.delete(shutdownHandler);
      }
  }
}