import type { Extension } from '@codemirror/state';

import type { CodeLanguage } from './types';

// 语言扩展懒加载映射
const languageLoaders: Record<CodeLanguage, () => Promise<Extension>> = {
  python: async () => {
    const { python } = await import('@codemirror/lang-python');
    return python();
  },
  sql: async () => {
    const { sql } = await import('@codemirror/lang-sql');
    return sql();
  },
  json: async () => {
    const { json } = await import('@codemirror/lang-json');
    return json();
  },
};

/**
 * 获取语言扩展
 * @param language 语言类型
 * @returns 语言扩展
 */
export async function getLanguageExtension(
  language: CodeLanguage | string,
): Promise<Extension | null> {
  const loader = languageLoaders[language as CodeLanguage];
  if (loader) {
    return await loader();
  }
  return null;
}

/**
 * 支持的语言列表
 */
export const supportedLanguages: CodeLanguage[] = [
  'python',
  'sql',
  'json',
];
