/**
 * CodeMirror 语言包加载器（动态导入，按需加载）
 */

import { getCmLanguage } from './languageMap'

const languageCache = new Map()

/**
 * 获取语言扩展（异步，首次调用会动态加载对应语言包）
 * @param {string} language - 语言名称
 * @returns {Promise<Extension|null>} CodeMirror 语言扩展
 */
export async function loadLanguageExtension(language) {
  if (languageCache.has(language)) {
    return languageCache.get(language)
  }

  let mod, extension = null

  switch (language) {
    case 'javascript':
      mod = await import('@codemirror/lang-javascript')
      extension = mod.javascript({ jsx: true })
      break
    case 'typescript':
      mod = await import('@codemirror/lang-javascript')
      extension = mod.javascript({ typescript: true, jsx: true })
      break
    case 'json':
      ;({ json: extension } = await import('@codemirror/lang-json'))
      extension = extension()
      break
    case 'yaml':
      ;({ yaml: extension } = await import('@codemirror/lang-yaml'))
      extension = extension()
      break
    case 'html':
      ;({ html: extension } = await import('@codemirror/lang-html'))
      extension = extension()
      break
    case 'css':
      ;({ css: extension } = await import('@codemirror/lang-css'))
      extension = extension()
      break
    case 'cpp':
    case 'c':
      ;({ cpp: extension } = await import('@codemirror/lang-cpp'))
      extension = extension()
      break
    case 'rust':
      ;({ rust: extension } = await import('@codemirror/lang-rust'))
      extension = extension()
      break
    case 'go':
      ;({ go: extension } = await import('@codemirror/lang-go'))
      extension = extension()
      break
    case 'python':
      ;({ python: extension } = await import('@codemirror/lang-python'))
      extension = extension()
      break
    case 'php':
      ;({ php: extension } = await import('@codemirror/lang-php'))
      extension = extension()
      break
    case 'sql':
      ;({ sql: extension } = await import('@codemirror/lang-sql'))
      extension = extension()
      break
    case 'markdown':
      ;({ markdown: extension } = await import('@codemirror/lang-markdown'))
      extension = extension()
      break
    case 'java':
      ;({ java: extension } = await import('@codemirror/lang-java'))
      extension = extension()
      break
    case 'shell':
    case 'bash':
    case 'sh':
    case 'dockerfile': {
      const { StreamLanguage, shell } = await import('@codemirror/legacy-modes/mode/shell')
      extension = StreamLanguage.define(shell)
      break
    }
    case 'powershell': {
      const { StreamLanguage, powerShell } = await import('@codemirror/legacy-modes/mode/powershell')
      extension = StreamLanguage.define(powerShell)
      break
    }
    case 'dart': {
      const { StreamLanguage, clike: dart } = await import('@codemirror/legacy-modes/mode/clike')
      extension = StreamLanguage.define(dart)
      break
    }
    default:
      return null
  }

  if (extension) {
    languageCache.set(language, extension)
  }
  return extension
}

/**
 * 根据文件扩展名获取语言名称
 * @param {string} extension - 文件扩展名
 * @returns {string} 语言名称
 */
export function getLanguageFromExtension(extension) {
  return getCmLanguage(extension)
}
