/** Monaco 语言 ID 与文件名/路径映射（运维场景优先） */

const EXT_TO_LANG: Record<string, string> = {
  js: 'javascript',
  mjs: 'javascript',
  cjs: 'javascript',
  jsx: 'javascript',
  ts: 'typescript',
  mts: 'typescript',
  cts: 'typescript',
  tsx: 'typescript',
  vue: 'html',
  py: 'python',
  pyw: 'python',
  rb: 'ruby',
  sh: 'shell',
  bash: 'shell',
  zsh: 'shell',
  fish: 'shell',
  php: 'php',
  phtml: 'php',
  java: 'java',
  go: 'go',
  rs: 'rust',
  c: 'c',
  cpp: 'cpp',
  cc: 'cpp',
  cxx: 'cpp',
  h: 'c',
  hpp: 'cpp',
  cs: 'csharp',
  swift: 'swift',
  kt: 'kotlin',
  kts: 'kotlin',
  html: 'html',
  htm: 'html',
  xhtml: 'html',
  css: 'css',
  scss: 'scss',
  sass: 'scss',
  less: 'less',
  json: 'json',
  jsonc: 'json',
  xml: 'xml',
  xsl: 'xml',
  xsd: 'xml',
  svg: 'xml',
  yaml: 'yaml',
  yml: 'yaml',
  toml: 'toml',
  md: 'markdown',
  mdx: 'mdx',
  sql: 'sql',
  graphql: 'graphql',
  gql: 'graphql',
  dockerfile: 'dockerfile',
  conf: 'ini',
  cfg: 'ini',
  cnf: 'ini',
  ini: 'ini',
  properties: 'ini',
  env: 'ini',
  tpl: 'html',
  twig: 'twig',
  blade: 'php',
  lua: 'lua',
  pl: 'perl',
  pm: 'perl',
  ps1: 'powershell',
  psm1: 'powershell',
  bat: 'bat',
  cmd: 'bat',
  r: 'r',
  dart: 'dart',
  scala: 'scala',
  clj: 'clojure',
  ex: 'elixir',
  exs: 'elixir',
  erl: 'elixir',
  hs: 'haskell',
  ml: 'fsharp',
  fs: 'fsharp',
  fsx: 'fsharp',
  vb: 'vb',
  pas: 'pascal',
  dockerignore: 'plaintext',
  gitignore: 'plaintext',
  gitattributes: 'plaintext',
  editorconfig: 'ini',
  htaccess: 'shell',
  service: 'ini',
  socket: 'ini',
  timer: 'ini',
  mount: 'ini',
  fstab: 'ini',
  crt: 'plaintext',
  pem: 'plaintext',
  key: 'plaintext',
  pub: 'plaintext',
  log: 'plaintext',
  lock: 'json',
}

const BASENAME_TO_LANG: Record<string, string> = {
  dockerfile: 'dockerfile',
  makefile: 'makefile',
  gnumakefile: 'makefile',
  'cmakelists.txt': 'plaintext',
  gemfile: 'ruby',
  rakefile: 'ruby',
  vagrantfile: 'ruby',
  nginx: 'nginx',
  'nginx.conf': 'nginx',
  '.env': 'ini',
  '.env.example': 'ini',
  '.env.local': 'ini',
  '.gitignore': 'plaintext',
  '.dockerignore': 'plaintext',
  '.htaccess': 'shell',
  hosts: 'plaintext',
  crontab: 'shell',
  'supervisord.conf': 'ini',
  'php.ini': 'ini',
  'my.cnf': 'ini',
}

export interface EditorLanguageOption {
  title: string
  value: string
}

/** 工具栏语言下拉（Monaco 已注册 ID） */
export const EDITOR_LANGUAGE_OPTIONS: EditorLanguageOption[] = [
  { title: '自动', value: 'auto' },
  { title: 'Plain Text', value: 'plaintext' },
  { title: 'PHP', value: 'php' },
  { title: 'JavaScript', value: 'javascript' },
  { title: 'TypeScript', value: 'typescript' },
  { title: 'JSON', value: 'json' },
  { title: 'HTML', value: 'html' },
  { title: 'CSS', value: 'css' },
  { title: 'SCSS', value: 'scss' },
  { title: 'Shell', value: 'shell' },
  { title: 'Python', value: 'python' },
  { title: 'Go', value: 'go' },
  { title: 'Rust', value: 'rust' },
  { title: 'Java', value: 'java' },
  { title: 'C', value: 'c' },
  { title: 'C++', value: 'cpp' },
  { title: 'C#', value: 'csharp' },
  { title: 'SQL', value: 'sql' },
  { title: 'YAML', value: 'yaml' },
  { title: 'TOML', value: 'toml' },
  { title: 'INI / Config', value: 'ini' },
  { title: 'Markdown', value: 'markdown' },
  { title: 'Dockerfile', value: 'dockerfile' },
  { title: 'Nginx', value: 'nginx' },
  { title: 'XML', value: 'xml' },
  { title: 'Vue (HTML)', value: 'html' },
  { title: 'Ruby', value: 'ruby' },
  { title: 'Perl', value: 'perl' },
  { title: 'PowerShell', value: 'powershell' },
]

export function detectEditorLanguage(fileName: string, filePath?: string): string {
  const base = fileName.toLowerCase()
  const pathBase = (filePath?.split('/').pop() ?? fileName).toLowerCase()

  if (BASENAME_TO_LANG[pathBase]) return BASENAME_TO_LANG[pathBase]
  if (BASENAME_TO_LANG[base]) return BASENAME_TO_LANG[base]

  if (base.endsWith('.blade.php')) return 'php'
  const dotParts = base.split('.')
  if (dotParts.length > 2 && dotParts[dotParts.length - 1] === 'php') {
    const compound = dotParts.slice(-2).join('.')
    if (compound === 'blade.php') return 'php'
  }

  const ext = base.includes('.') ? base.split('.').pop() ?? '' : ''
  if (ext && EXT_TO_LANG[ext]) return EXT_TO_LANG[ext]

  if (pathBase.includes('nginx') && (pathBase.endsWith('.conf') || pathBase === 'nginx.conf')) {
    return 'nginx'
  }

  return 'plaintext'
}

export function resolveTabLanguage(
  fileName: string,
  filePath: string,
  manualLanguageId: string | null | undefined,
): string {
  if (manualLanguageId && manualLanguageId !== 'auto') return manualLanguageId
  return detectEditorLanguage(fileName, filePath)
}
