/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function FailureDetail(error, id, logs, parentStack, diagnostics) {
  const { message, stack, stackLines, cause, errors, secondary } = parseError(error, parentStack);
  const result = { message };
  if (stack) {
    result.stack = stack;
  }
  if (id) {
    result.id = id;
  }
  if (logs?.length) {
    result.logs = logs.join("\n");
  }
  if (diagnostics?.length) {
    result.diagnostics = diagnostics;
  }
  if (cause) {
    result.cause = cause;
  }
  if (secondary) {
    result.secondary = secondary;
  }
  if (errors) {
    result.errors = errors;
  }
  if (stackLines) {
    result.stackLines = stackLines;
  }
  const { actual, expected } = error;
  if (actual) {
    result.actual = actual;
  }
  if (expected) {
    result.expected = expected;
  }
  return result;
}
function messageAndStackFor(error, parentStack) {
  if (MatterHooks?.messageAndStackFor) {
    return MatterHooks.messageAndStackFor(error, parentStack);
  }
  let message, stack;
  if (error === void 0 || error === null) {
    message = `(error is ${error})`;
  } else {
    message = error.message;
  }
  if (error.stack) {
    let lines = error.stack.trim().split("\n");
    if (!message) {
      message = lines[0];
    }
    lines = lines.filter((line) => line.match(/:\d+:\d+\)?/));
    if (lines.length) {
      stack = lines.map((line) => line.trim()).join("\n");
    }
  }
  if (!message) {
    message = error.toString();
  }
  return { message, stack };
}
function parseError(error, parentStack) {
  let secondary;
  if ("error" in error && "suppressed" in error) {
    secondary = FailureDetail(error.error);
    error = error.suppressed;
  }
  const { message, id, stack, stackLines } = messageAndStackFor(error, parentStack);
  let cause, errors;
  const errorCause = error.cause;
  if (errorCause) {
    cause = FailureDetail(errorCause, id, void 0, stackLines);
  }
  const errorErrors = error.errors;
  if (Array.isArray(errorErrors)) {
    errors = errorErrors.map((e) => FailureDetail(e, id, void 0, stackLines));
  }
  return { message, id, stack, stackLines, cause, errors, secondary };
}
export {
  FailureDetail
};
//# sourceMappingURL=failure-detail.js.map
