/**
 * 动作基类
 */
class BaseAction {
  constructor(context, config) {
    this.context = context;
    this.config = config;
    this.page = context.page;
    this.logger = context.logger;
  }
  
  /**
   * 执行动作（子类必须实现）
   */
  async execute() {
    throw new Error('子类必须实现 execute 方法');
  }
  
  /**
   * 替换配置中的变量
   * @param {string} value - 包含变量的字符串 (如 "{{account.email}}")
   * @returns {string} - 替换后的值
   */
  replaceVariables(value) {
    if (typeof value !== 'string') return value;
    
    return value.replace(/\{\{(.+?)\}\}/g, (match, path) => {
      const keys = path.trim().split('.');
      let result = this.context.data;
      
      for (const key of keys) {
        if (result && typeof result === 'object') {
          result = result[key];
        } else {
          return match; // 无法解析，返回原始值
        }
      }
      
      return result !== undefined ? result : match;
    });
  }
  
  /**
   * 记录日志
   */
  log(level, message) {
    if (this.logger && this.logger[level]) {
      this.logger[level](this.context.siteName || 'Automation', message);
    } else {
      console.log(`[${level.toUpperCase()}] ${message}`);
    }
  }
}

module.exports = BaseAction;
