/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Logger, RuntimeService } from "@matter/general";
const logger = Logger.get("ProcessManager");
class ProcessManager {
  constructor(env) {
    this.env = env;
    this.runtime = env.get(RuntimeService);
    this.runtime.started.on(this.startListener);
    this.runtime.stopped.on(this.stopListener);
    this.runtime.crashed.on(this.crashListener);
    if (this.hasUnhandledErrorSupport) {
      process.addListener("uncaughtExceptionMonitor", (event) => {
        Logger.reportUnhandledError(event);
      });
    }
  }
  runtime;
  #signalHandlersInstalled = false;
  close() {
    this.runtime.started.off(this.startListener);
    this.runtime.stopped.off(this.stopListener);
    this.runtime.crashed.off(this.crashListener);
    this.#ignoreSignals();
  }
  [Symbol.dispose]() {
    this.close();
  }
  get hasSignalSupport() {
    return this.env.vars.get("runtime.signals", true);
  }
  get hasExitCodeSupport() {
    return this.env.vars.get("runtime.exitcode", true);
  }
  get hasUnhandledErrorSupport() {
    return this.env.vars.get("runtime.unhandlederrors", true);
  }
  startListener = () => {
    this.env.vars.use(() => {
      if (this.hasSignalSupport) {
        if (this.#signalHandlersInstalled) {
          return;
        }
        this.installInterruptHandlers();
        process.on("SIGUSR2", this.diagnosticHandler);
        process.on("exit", this.exitHandler);
        this.#signalHandlersInstalled = true;
      } else {
        this.#ignoreSignals();
      }
    });
  };
  stopListener = () => {
    this.#ignoreSignals();
    if (this.hasExitCodeSupport && process.exitCode === void 0) {
      process.exitCode = 0;
    }
  };
  crashListener = () => {
    if (this.hasExitCodeSupport) {
      process.exitCode = 1;
    }
  };
  interruptHandler = () => {
    this.uninstallInterruptHandlers();
    this.installInterruptHandlers();
    this.runtime.interrupt();
  };
  abortHandler = () => {
    this.uninstallInterruptHandlers();
    logger.fatal("Terminating due to SIGABRT of JS runtime, diagnostics follow");
    logger.info(JSON.stringify(process.report.getReport(), void 0, 2));
    process.exit(-66);
  };
  exitHandler = () => {
    if (process.exitCode === 13) {
      logger.error("Internal error: Premature process exit because ongoing work has stalled");
    }
  };
  diagnosticHandler = () => {
    this.env.diagnose();
  };
  installInterruptHandlers = () => {
    process.on("SIGINT", this.interruptHandler);
    process.on("SIGTERM", this.interruptHandler);
    process.on("SIGABRT", this.abortHandler);
  };
  uninstallInterruptHandlers = () => {
    process.off("SIGINT", this.interruptHandler);
    process.off("SIGTERM", this.interruptHandler);
    process.off("SIGABRT", this.abortHandler);
  };
  #ignoreSignals() {
    if (this.#signalHandlersInstalled) {
      this.uninstallInterruptHandlers();
      process.off("SIGUSR2", this.diagnosticHandler);
      process.off("exit", this.exitHandler);
      this.#signalHandlersInstalled = false;
    }
  }
}
export {
  ProcessManager
};
//# sourceMappingURL=ProcessManager.js.map
