/**
 * @file 必应词典实现（Mock版）
 * @description 必应词典的 Mock 实现，返回模拟数据
 */

import { DictionaryBase, createResult, createMeaning, createExample } from './base.js';

/**
 * 必应词典 Mock 实现
 */
export class BingDictionary extends DictionaryBase {
  constructor(config = {}) {
    super({
      name: '必应词典',
      icon: 'icons/bing.png',
      languages: ['en', 'zh'],
      ...config
    });
  }

  /**
   * 查询单词（Mock 实现）
   * @param {string} word - 要查询的单词
   * @returns {Promise<DictionaryResult>} 模拟查询结果
   */
  async search(word) {
    if (!word?.trim()) {
      throw new Error('Word is empty');
    }

    const trimmedWord = word.trim();

    // 模拟网络延迟
    await this._delay(100);

    // 返回模拟数据
    return createResult({
      word: trimmedWord,
      phonetic: this._getMockPhonetic(trimmedWord),
      meanings: this._getMockMeanings(trimmedWord),
      examples: this._getMockExamples(trimmedWord),
      url: `https://cn.bing.com/dict/search?q=${encodeURIComponent(trimmedWord)}`
    });
  }

  /**
   * 模拟延迟
   * @private
   * @param {number} ms - 延迟毫秒数
   * @returns {Promise<void>}
   */
  _delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

  /**
   * 获取模拟音标
   * @private
   * @param {string} word - 单词
   * @returns {string} 音标
   */
  _getMockPhonetic(word) {
    const phonetics = {
      'hello': '/həˈləʊ/',
      'world': '/wɜːld/',
      'apple': '/ˈæpl/',
      'computer': '/kəmˈpjuːtə(r)/',
      'dictionary': '/ˈdɪkʃənri/'
    };
    
    return phonetics[word.toLowerCase()] || `/${word.toLowerCase()}/`;
  }

  /**
   * 获取模拟释义
   * @private
   * @param {string} word - 单词
   * @returns {Array<Meaning>} 释义列表
   */
  _getMockMeanings(word) {
    const meaningsMap = {
      'hello': [
        createMeaning('int.', ['你好', '喂', '嘿']),
        createMeaning('n.', ['问候', '招呼'])
      ],
      'world': [
        createMeaning('n.', ['世界', '地球', '天下', '界'])
      ],
      'apple': [
        createMeaning('n.', ['苹果', '苹果公司'])
      ],
      'computer': [
        createMeaning('n.', ['计算机', '电脑'])
      ],
      'dictionary': [
        createMeaning('n.', ['词典', '字典', '辞典'])
      ]
    };

    return meaningsMap[word.toLowerCase()] || [
      createMeaning('n.', [`${word} 的中文释义`]),
      createMeaning('v.', [`${word} 的动词释义`])
    ];
  }

  /**
   * 获取模拟例句
   * @private
   * @param {string} word - 单词
   * @returns {Array<Example>} 例句列表
   */
  _getMockExamples(word) {
    const examplesMap = {
      'hello': [
        createExample('Hello, how are you?', '你好，你好吗？'),
        createExample('She said hello to everyone.', '她向每个人问好。')
      ],
      'world': [
        createExample('The world is getting smaller.', '世界变得越来越小。'),
        createExample('He wants to travel around the world.', '他想环游世界。')
      ],
      'apple': [
        createExample('An apple a day keeps the doctor away.', '一天一苹果，医生远离我。'),
        createExample('She took a bite of the apple.', '她咬了一口苹果。')
      ],
      'computer': [
        createExample('I use a computer for work.', '我用电脑工作。'),
        createExample('The computer is running slowly.', '电脑运行很慢。')
      ],
      'dictionary': [
        createExample('I looked it up in the dictionary.', '我在词典里查了一下。'),
        createExample('This is an English-Chinese dictionary.', '这是一本英汉词典。')
      ]
    };

    return examplesMap[word.toLowerCase()] || [
      createExample(`This is a sentence with "${word}".`, `这是一个包含"${word}"的句子。`),
      createExample(`Can you use "${word}" in a sentence?`, `你能用"${word}"造句吗？`)
    ];
  }
}

// 导出单例实例
export const bingDictionary = new BingDictionary();
