/**
 * 框架自定义错误类
 */

class AutomationError extends Error {
  constructor(message, context = {}) {
    super(message);
    this.name = 'AutomationError';
    this.context = context;
    this.timestamp = new Date();
  }
  
  toJSON() {
    return {
      name: this.name,
      message: this.message,
      context: this.context,
      timestamp: this.timestamp,
      stack: this.stack
    };
  }
}

class ElementNotFoundError extends AutomationError {
  constructor(selector, context = {}) {
    super(`元素未找到: ${JSON.stringify(selector)}`, context);
    this.name = 'ElementNotFoundError';
    this.selector = selector;
  }
}

class TimeoutError extends AutomationError {
  constructor(operation, timeout, context = {}) {
    super(`操作超时: ${operation} (${timeout}ms)`, context);
    this.name = 'TimeoutError';
    this.operation = operation;
    this.timeout = timeout;
  }
}

class ValidationError extends AutomationError {
  constructor(message, expected, actual, context = {}) {
    super(message, context);
    this.name = 'ValidationError';
    this.expected = expected;
    this.actual = actual;
  }
}

class ConfigurationError extends AutomationError {
  constructor(message, configPath, context = {}) {
    super(message, context);
    this.name = 'ConfigurationError';
    this.configPath = configPath;
  }
}

class RetryExhaustedError extends AutomationError {
  constructor(operation, attempts, context = {}) {
    super(`重试次数用尽: ${operation} (${attempts} 次尝试)`, context);
    this.name = 'RetryExhaustedError';
    this.operation = operation;
    this.attempts = attempts;
  }
}

module.exports = {
  AutomationError,
  ElementNotFoundError,
  TimeoutError,
  ValidationError,
  ConfigurationError,
  RetryExhaustedError
};
