"use strict";
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const kit_internal_functions = require("../internal/functions.cjs");
const kit_internal_Logger = require("../internal/Logger.cjs");
const isError = (error) => {
  return error instanceof Error;
};
const DEFAULT_ERROR_MESSAGE = "Unknown error encountered.";
const ErrorLevel = {
  Error: "error",
  Fatal: "fatal",
  Warn: "warning"
};
const ErrorType = {
  // unexpected response structure.
  Api: "api",
  // the issue is caused by unexpected/invalid user input.
  ApiBadResponse: "apiBadResponse",
  // third-party api
  Assert: "assert",
  // internal apis and server errors.
  Auth: "auth",
  Input: "input",
  Server: "server",
  Ui: "ui",
  Unknown: "unknown"
  // assertion failure.
};
const ERROR_NAMESPACE = "EH";
const DEFAULT_LOGGER = kit_internal_Logger.default.extend(ERROR_NAMESPACE);
class DetError extends Error {
  /* eslint-disable-next-line @typescript-eslint/no-explicit-any */
  constructor(e, options = {}) {
    const defaultMessage = isError(e) ? e.message : kit_internal_functions.isString(e) ? e : DEFAULT_ERROR_MESSAGE;
    const message = options.publicSubject || options.publicMessage || defaultMessage;
    super(message);
    __publicField(this, "id");
    __publicField(this, "isUserTriggered", false);
    __publicField(this, "level", ErrorLevel.Error);
    __publicField(this, "logger", DEFAULT_LOGGER);
    // CHECK: do we want this attached to DetError?
    __publicField(this, "payload");
    __publicField(this, "publicMessage");
    __publicField(this, "publicSubject");
    __publicField(this, "silent", false);
    __publicField(this, "type", ErrorType.Unknown);
    __publicField(this, "isHandled");
    /** the wrapped error if one was provided. */
    __publicField(this, "sourceErr");
    const eOpts = {};
    if (kit_internal_functions.isObject(e)) {
      if ("id" in e && e.id != null)
        eOpts.id = e.id;
      if ("isUserTriggered" in e && e.isUserTriggered != null)
        eOpts.isUserTriggered = e.isUserTriggered;
      if ("level" in e && e.level != null)
        eOpts.level = e.level;
      if ("logger" in e && e.logger != null)
        eOpts.logger = e.logger;
      if ("payload" in e && e.payload != null)
        eOpts.payload = e.payload;
      if ("publicMessage" in e && e.publicMessage != null)
        eOpts.publicMessage = e.publicMessage;
      if ("silent" in e && e.silent != null)
        eOpts.silent = e.silent;
      if ("type" in e && e.type != null)
        eOpts.type = e.type;
    }
    this.loadOptions({ ...eOpts, ...options });
    this.isHandled = false;
    this.sourceErr = e;
  }
  loadOptions(options) {
    Object.assign(this, options);
  }
}
exports.DEFAULT_ERROR_MESSAGE = DEFAULT_ERROR_MESSAGE;
exports.DetError = DetError;
exports.ERROR_NAMESPACE = ERROR_NAMESPACE;
exports.ErrorLevel = ErrorLevel;
exports.ErrorType = ErrorType;
exports.isError = isError;
