"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const core = require("../types/core.cjs");
const i18nPatterns = {
  [core.ContentType.Thinking]: {
    zh: {
      patterns: [
        /^(让我|我来|我需要|我将|我要|让我们)/,
        /^(首先|接下来|然后|最后)/,
        /^(分析|思考|考虑|检查|查看)/,
        /(因为|所以|因此|由于)/
      ]
    },
    en: {
      patterns: [
        /^(let me|i'll|i need to|i will|let's)/i,
        /^(first|next|then|finally)/i,
        /^(analyze|think|consider|check|review)/i,
        /(because|so|therefore|due to)/i
      ]
    },
    ja: {
      patterns: [
        /^(まず|次に|それから|最後に)/,
        /^(分析|考える|検討|確認|チェック)/,
        /(なので|だから|したがって|により)/
      ]
    }
  },
  [core.ContentType.Planning]: {
    zh: {
      patterns: [
        /^(计划|任务|步骤)[:：]/,
        /^\d+[.)\s]+/m,
        /^[-*]\s+\[[ x]\]/m,
        /(第[一二三四五六七八九十]步|步骤\d+)/
      ]
    },
    en: {
      patterns: [
        /^(plan|task|step)s?:/i,
        /^\d+[.)\s]+/m,
        /^[-*]\s+\[[ x]\]/m,
        /(step\s+\d+|phase\s+\d+)/i
      ]
    }
  },
  [core.ContentType.Code]: {
    all: {
      patterns: [/```(\w+)?\n([\s\S]*?)```/g]
    }
  },
  [core.ContentType.Error]: {
    zh: {
      patterns: [/^(错误|失败|异常)[:：]/, /\[错误\]/]
    },
    en: {
      patterns: [/^(error|failure|exception):/i, /\[ERROR\]/i]
    },
    ja: {
      patterns: [/^(エラー|失敗|例外)[:：]/, /\[エラー\]/]
    }
  },
  [core.ContentType.Warning]: {
    zh: {
      patterns: [/^(警告|注意|提醒)[:：]/, /\[警告\]/]
    },
    en: {
      patterns: [/^(warning|caution|notice):/i, /\[WARNING\]/i]
    },
    ja: {
      patterns: [/^(警告|注意|通知)[:：]/, /\[警告\]/]
    }
  },
  [core.ContentType.Confirmation]: {
    zh: {
      patterns: [/^(请确认|是否|您要|您想|需要我)/, /\?$/, /^(可以吗|对吗|是吗|好吗)/]
    },
    en: {
      patterns: [
        /^(please confirm|would you|do you want|shall i)/i,
        /\?$/,
        /^(is this correct|right|okay)/i
      ]
    }
  },
  [core.ContentType.Summary]: {
    zh: {
      patterns: [/^(总结|总的来说|综上所述|简而言之)/, /^(结论|小结)[:：]/]
    },
    en: {
      patterns: [/^(summary|in summary|to sum up|in brief)/i, /^(conclusion|recap):/i]
    }
  },
  [core.ContentType.Progress]: {
    zh: {
      patterns: [/^(进度|状态)[:：]/, /\d+%/, /(\d+\/\d+)/, /^(已完成|进行中|待处理)/]
    },
    en: {
      patterns: [/^(progress|status):/i, /\d+%/, /(\d+\/\d+)/, /^(completed|in progress|pending)/i]
    }
  },
  [core.ContentType.ToolCall]: {
    zh: {
      patterns: [/^(执行|调用|运行|使用).*工具/, /^(正在|开始).*执行/, /\[工具调用\]/]
    },
    en: {
      patterns: [
        /^(execute|call|run|use).*tool/i,
        /^(running|starting).*execute/i,
        /\[Tool Call\]/i
      ]
    }
  },
  [core.ContentType.CodeExplanation]: {
    zh: {
      patterns: [],
      keywords: ["代码", "函数", "方法", "类", "变量", "参数", "返回值"]
    },
    en: {
      patterns: [],
      keywords: ["code", "function", "method", "class", "variable", "parameter", "return"]
    }
  },
  [core.ContentType.Explanation]: {
    zh: {
      patterns: [],
      keywords: ["解释", "说明", "意思是", "定义", "概念"]
    },
    en: {
      patterns: [],
      keywords: ["explain", "describe", "means", "definition", "concept"]
    }
  },
  [core.ContentType.Text]: {
    all: {
      patterns: [],
      keywords: []
    }
  },
  // 添加缺失的类型，暂时使用空模式
  [core.ContentType.Cancelled]: {
    all: {
      patterns: [],
      keywords: []
    }
  },
  [core.ContentType.Data]: {
    zh: {
      patterns: [/^(数据|表格|统计)[:：]/],
      keywords: ["数据", "表格", "统计", "图表"]
    },
    en: {
      patterns: [/^(data|table|statistics):/i],
      keywords: ["data", "table", "statistics", "chart"]
    }
  },
  [core.ContentType.Debug]: {
    all: {
      patterns: [/^\[DEBUG\]/i, /^Debug:/i],
      keywords: ["debug", "trace", "log"]
    }
  },
  [core.ContentType.Visualization]: {
    zh: {
      patterns: [/^(图表|可视化|图形)[:：]/],
      keywords: ["图表", "可视化", "图形", "绘图"]
    },
    en: {
      patterns: [/^(chart|visualization|graph):/i],
      keywords: ["chart", "visualization", "graph", "plot"]
    }
  }
};
const backendToFrontendMapping = {
  // 标准映射
  planning: core.ContentType.Planning,
  plan: core.ContentType.Planning,
  plan_step: core.ContentType.Planning,
  thinking: core.ContentType.Thinking,
  code: core.ContentType.Code,
  error: core.ContentType.Error,
  warning: core.ContentType.Warning,
  progress: core.ContentType.Progress,
  summary: core.ContentType.Summary,
  confirmation: core.ContentType.Confirmation,
  tool_call: core.ContentType.ToolCall,
  text: core.ContentType.Text,
  // 步骤相关映射
  step_start: core.ContentType.Progress,
  step_complete: core.ContentType.Progress,
  step_result: core.ContentType.Text,
  complete: core.ContentType.Summary,
  // 额外的别名映射
  thought: core.ContentType.Thinking,
  task: core.ContentType.Planning,
  err: core.ContentType.Error,
  warn: core.ContentType.Warning,
  info: core.ContentType.Text,
  debug: core.ContentType.Text
};
class MessageTypeDetectorService {
  detectors = [];
  language = "zh";
  cache = /* @__PURE__ */ new Map();
  maxCacheSize = 1e3;
  constructor(language = "zh") {
    this.language = language;
    this.initializeDetectors();
  }
  // 设置语言
  setLanguage(language) {
    if (this.language !== language) {
      this.language = language;
      this.cache.clear();
      this.initializeDetectors();
    }
  }
  // 初始化检测器
  initializeDetectors() {
    this.detectors = [];
    const priorityMap = {
      [core.ContentType.Code]: 1,
      [core.ContentType.Error]: 2,
      [core.ContentType.Warning]: 3,
      [core.ContentType.ToolCall]: 4,
      [core.ContentType.Planning]: 5,
      [core.ContentType.Progress]: 6,
      [core.ContentType.Confirmation]: 7,
      [core.ContentType.Summary]: 8,
      [core.ContentType.Thinking]: 9,
      [core.ContentType.CodeExplanation]: 10,
      [core.ContentType.Explanation]: 11,
      [core.ContentType.Text]: 12,
      [core.ContentType.Cancelled]: 13,
      [core.ContentType.Data]: 14,
      [core.ContentType.Debug]: 15,
      [core.ContentType.Visualization]: 16
    };
    Object.entries(i18nPatterns).forEach(([type, langPatterns]) => {
      const messageType = type;
      const patterns = langPatterns[this.language] || langPatterns.all || langPatterns.en;
      if (patterns) {
        this.detectors.push({
          type: messageType,
          priority: priorityMap[messageType] || 99,
          patterns: patterns.patterns || [],
          keywords: patterns.keywords
        });
      }
    });
    this.detectors.sort((a, b) => a.priority - b.priority);
  }
  // 映射后端类型到前端类型
  mapBackendType(backendType) {
    if (!backendType) return void 0;
    const normalizedType = backendType.toLowerCase().trim();
    return backendToFrontendMapping[normalizedType] || core.ContentType.Text;
  }
  // 检测消息类型
  detectMessageType(content) {
    if (!content || content.trim().length === 0) {
      return core.ContentType.Text;
    }
    const cacheKey = `${this.language}:${content.substring(0, 100)}`;
    if (this.cache.has(cacheKey)) {
      return this.cache.get(cacheKey);
    }
    const trimmedContent = content.trim();
    for (const detector of this.detectors) {
      if (detector.patterns.length > 0) {
        const matches = detector.patterns.some((pattern) => pattern.test(trimmedContent));
        if (matches) {
          this.addToCache(cacheKey, detector.type);
          return detector.type;
        }
      }
      if (detector.keywords && detector.keywords.length > 0) {
        const hasKeyword = detector.keywords.some(
          (keyword) => content.toLowerCase().includes(keyword.toLowerCase())
        );
        if (hasKeyword) {
          this.addToCache(cacheKey, detector.type);
          return detector.type;
        }
      }
    }
    this.addToCache(cacheKey, core.ContentType.Text);
    return core.ContentType.Text;
  }
  // 添加到缓存
  addToCache(key, value) {
    if (this.cache.size >= this.maxCacheSize) {
      const firstKey = this.cache.keys().next().value;
      if (firstKey) this.cache.delete(firstKey);
    }
    this.cache.set(key, value);
  }
  // 清除缓存
  clearCache() {
    this.cache.clear();
  }
  // 添加自定义检测规则
  addCustomDetector(detector) {
    this.detectors.push(detector);
    this.detectors.sort((a, b) => a.priority - b.priority);
    this.clearCache();
  }
  // 移除自定义检测规则
  removeCustomDetector(type) {
    this.detectors = this.detectors.filter((d) => d.type !== type);
    this.clearCache();
  }
}
const messageTypeDetector = new MessageTypeDetectorService();
exports.MessageTypeDetectorService = MessageTypeDetectorService;
exports.backendToFrontendMapping = backendToFrontendMapping;
exports.messageTypeDetector = messageTypeDetector;
//# sourceMappingURL=message-type-detector.cjs.map
