import { jsonSchema } from "./json-schema.js";
function getErrorMessage(error) {
  return getErrorMessageInternal(error, /* @__PURE__ */ new Set());
}
function getErrorMessageInternal(error, seen) {
  if (typeof error === "string") {
    return error;
  }
  if (typeof error === "object" && error !== null) {
    if (seen.has(error)) {
      return "Circular error reference detected while extracting the error message.";
    }
    seen.add(error);
  }
  if (error instanceof Error) {
    if (error.message) {
      return error.message;
    }
    if ("cause" in error) {
      const { cause } = error;
      if (cause !== void 0) {
        const causeMessage = getErrorMessageInternal(cause, seen);
        if (causeMessage) {
          return causeMessage;
        }
      }
    }
  }
  if (typeof error === "object" && error !== null && "message" in error && typeof error.message === "string") {
    const message = error.message;
    if (message) {
      return message;
    }
  }
  try {
    const json = jsonSchema.parse(error);
    return `Parsed message: ${JSON.stringify(json)}`;
  } catch {
  }
  return `Unknown error of type ${typeof error} was thrown and the message could not be determined. See cause for details.`;
}
function getErrorDetails(error) {
  if (error instanceof Error) {
    if ("details" in error) {
      try {
        return jsonSchema.parse(error?.details);
      } catch {
      }
    }
    if (error.name && error.name !== "Error") {
      return { name: error.name };
    }
    return void 0;
  }
  if (typeof error === "object" && error !== null && "details" in error) {
    try {
      return jsonSchema.parse(error?.details);
    } catch {
    }
  }
  try {
    return jsonSchema.parse(error);
  } catch {
  }
  return void 0;
}
export {
  getErrorDetails,
  getErrorMessage
};
//# sourceMappingURL=error.js.map
