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,IAAI,GAAG,CAAC;AACnD,CAAC","sourcesContent":["import type { Category } from './hardware-errors-enums';\nimport { ErrorCode, Severity } from './hardware-errors-enums';\n\n/**\n * Generates a unique error ID using timestamp and random values.\n *\n * @returns A unique error ID string.\n */\nfunction generateErrorId(): string {\n  const timestamp = Date.now().toString(36);\n  // Random string will be formatted as: 0.fa4dmg7flr8, so we skip 0. part.\n  const randomPart = Math.random().toString(36).substring(2, 9);\n  return `err_${timestamp}_${randomPart}`;\n}\n\n/**\n * Gets the human-readable name for an error code using enum reverse mapping.\n *\n * @param code - The error code enum value.\n * @returns The string name of the error code, or 'Unknown' if not found.\n */\nfunction getErrorCodeName(code: ErrorCode): string {\n  // Numeric enums have a reverse mapping at runtime: ErrorCode[1000] => \"AuthFailed\"\n  return ErrorCode[code] ?? ErrorCode[ErrorCode.Unknown];\n}\n\nexport type HardwareWalletErrorOptions = {\n  code: ErrorCode;\n  severity: Severity;\n  category: Category;\n  userMessage: string;\n  cause?: Error;\n  metadata?: Record<string, unknown>;\n};\n\nexport class HardwareWalletError extends Error {\n  public readonly id: string;\n\n  public readonly code: ErrorCode;\n\n  public readonly severity: Severity;\n\n  public readonly category: Category;\n\n  public readonly userMessage: string;\n\n  public readonly timestamp: Date;\n\n  public readonly metadata: Record<string, unknown> | undefined;\n\n  public readonly cause: Error | undefined;\n\n  constructor(message: string, options: HardwareWalletErrorOptions) {\n    super(message);\n    this.name = 'HardwareWalletError';\n    this.id = generateErrorId();\n    this.code = options.code;\n    this.severity = options.severity;\n    this.category = options.category;\n    this.userMessage = options.userMessage;\n    this.timestamp = new Date();\n    this.metadata = options.metadata;\n    this.cause = options.cause;\n  }\n\n  /**\n   * Checks if this error is critical.\n   *\n   * @returns True if the error is critical, false otherwise.\n   */\n  isCritical(): boolean {\n    return this.severity === Severity.Critical;\n  }\n\n  /**\n   * Checks if this error is a warning.\n   *\n   * @returns True if the error is a warning, false otherwise.\n   */\n  isWarning(): boolean {\n    return this.severity === Severity.Warning;\n  }\n\n  /**\n   * Creates a new error instance with additional metadata.\n   *\n   * @param additionalMetadata - Additional metadata to merge with existing metadata.\n   * @returns A new HardwareWalletError instance with merged metadata.\n   */\n  withMetadata(\n    additionalMetadata: Record<string, unknown>,\n  ): HardwareWalletError {\n    const options: HardwareWalletErrorOptions = {\n      code: this.code,\n      severity: this.severity,\n      category: this.category,\n      userMessage: this.userMessage,\n      metadata: { ...(this.metadata ?? {}), ...additionalMetadata },\n    };\n\n    if (this.cause !== undefined) {\n      options.cause = this.cause;\n    }\n\n    return new HardwareWalletError(this.message, options);\n  }\n\n  /**\n   * Serializes the error to a JSON-compatible object.\n   * Note: The cause property is serialized if it exists.\n   *\n   * @returns A JSON-compatible object representation of the error.\n   */\n  toJSON(): Record<string, unknown> {\n    const json: Record<string, unknown> = {\n      id: this.id,\n      name: this.name,\n      message: this.message,\n      code: this.code,\n      severity: this.severity,\n      category: this.category,\n      userMessage: this.userMessage,\n      timestamp: this.timestamp.toISOString(),\n      metadata: this.metadata,\n    };\n\n    if (this.cause !== undefined) {\n      json.cause = {\n        name: this.cause.name,\n        message: this.cause.message,\n      };\n    }\n\n    return json;\n  }\n\n  /**\n   * Returns a user-friendly string representation of the error.\n   *\n   * @returns A user-friendly string representation of the error.\n   */\n  toString(): string {\n    return `${this.#getErrorPrefix()}: ${this.message}`;\n  }\n\n  /**\n   * Returns a detailed string representation for debugging.\n   *\n   * @returns A detailed string representation of the error for debugging.\n   */\n  toDetailedString(): string {\n    const details = [\n      this.toString(),\n      `User Message: ${this.userMessage}`,\n      `Severity: ${this.severity}`,\n      `Category: ${this.category}`,\n      `Timestamp: ${this.timestamp.toISOString()}`,\n    ];\n\n    if (this.metadata && Object.keys(this.metadata).length > 0) {\n      details.push(`Metadata: ${JSON.stringify(this.metadata, null, 2)}`);\n    }\n\n    if (this.cause !== undefined) {\n      details.push(`Caused by: ${this.cause.message}`);\n    }\n\n    return details.join('\\n');\n  }\n\n  #getErrorPrefix(): string {\n    const codeName = getErrorCodeName(this.code);\n    return `${this.name} [${codeName}:${this.code}]`;\n  }\n\n  static isHardwareWalletError(error: unknown): error is HardwareWalletError {\n    return error instanceof HardwareWalletError;\n  }\n}\n"]}