// Copyright 2018-2026 the Deno authors. MIT license.
// deno-lint-ignore-file no-process-global
(function () {
const { core, primordials } = __bootstrap;
const { getOptionValue } = core.loadExtScript(
  "ext:deno_node/internal/options.ts",
);

const {
  ErrorPrototype,
  ErrorPrototypeToString,
  ObjectPrototypeIsPrototypeOf,
  SafeSet,
} = primordials;

let disableWarningSet;

function onWarning(
  warning: Error & { code?: string; name?: string; detail?: string },
) {
  if (!disableWarningSet) {
    disableWarningSet = new SafeSet();
    const disableWarningValues = getOptionValue("--disable-warning");
    for (let i = 0; i < disableWarningValues?.length; i++) {
      disableWarningSet.add(disableWarningValues[i]);
    }
  }
  if (
    (warning?.code && disableWarningSet.has(warning.code)) ||
    (warning?.name && disableWarningSet.has(warning.name))
  ) return;

  if (!ObjectPrototypeIsPrototypeOf(ErrorPrototype, warning)) return;

  const isDeprecation = warning.name === "DeprecationWarning";
  if (isDeprecation && process.noDeprecation) return;
  const trace = process.traceProcessWarnings ||
    (isDeprecation && process.traceDeprecation);
  let msg = `(${process.release.name}:${process.pid}) `;
  if (warning.code) {
    msg += `[${warning.code}] `;
  }
  if (trace && warning.stack) {
    msg += `${warning.stack}`;
  } else {
    msg += typeof warning.toString === "function"
      // deno-lint-ignore deno-internal/prefer-primordials
      ? `${warning.toString()}`
      : ErrorPrototypeToString(warning);
  }
  if (typeof warning.detail === "string") {
    msg += `\n${warning.detail}`;
  }
  process.stderr.write(msg + "\n");
}

return {
  onWarning,
};
})();
