const extensionToLanguage: Record<string, string> = {
  // JavaScript / TypeScript
  ts: 'typescript',
  tsx: 'typescript',
  js: 'javascript',
  jsx: 'javascript',
  mjs: 'javascript',
  cjs: 'javascript',

  // Web
  html: 'html',
  htm: 'html',
  css: 'css',
  scss: 'scss',
  less: 'less',
  vue: 'html',
  svelte: 'html',

  // Data / Config
  json: 'json',
  jsonc: 'json',
  json5: 'json',
  geojson: 'json',
  yaml: 'yaml',
  yml: 'yaml',
  toml: 'ini',
  ini: 'ini',
  cfg: 'ini',
  conf: 'ini',
  properties: 'ini',
  env: 'ini',
  xml: 'xml',
  xsl: 'xml',
  xslt: 'xml',
  xsd: 'xml',
  svg: 'xml',
  plist: 'xml',
  csproj: 'xml',
  fsproj: 'xml',
  vcxproj: 'xml',
  sln: 'xml',

  // Markdown / Text
  md: 'markdown',
  mdx: 'markdown',
  markdown: 'markdown',
  txt: 'plaintext',
  log: 'plaintext',

  // Shell
  sh: 'shell',
  bash: 'shell',
  zsh: 'shell',
  fish: 'shell',
  ksh: 'shell',
  csh: 'shell',
  ps1: 'powershell',
  psm1: 'powershell',
  psd1: 'powershell',
  bat: 'bat',
  cmd: 'bat',

  // Python
  py: 'python',
  pyw: 'python',
  pyi: 'python',
  pyx: 'python',
  ipynb: 'json',

  // Ruby
  rb: 'ruby',
  erb: 'ruby',
  gemspec: 'ruby',
  rake: 'ruby',

  // Rust
  rs: 'rust',

  // Go
  go: 'go',
  mod: 'go',

  // Java / JVM
  java: 'java',
  kt: 'kotlin',
  kts: 'kotlin',
  scala: 'scala',
  sc: 'scala',
  groovy: 'groovy',
  gradle: 'groovy',

  // C / C++ / Objective-C
  c: 'c',
  h: 'c',
  cpp: 'cpp',
  cc: 'cpp',
  cxx: 'cpp',
  hpp: 'cpp',
  hxx: 'cpp',
  hh: 'cpp',
  m: 'objective-c',
  mm: 'objective-c',

  // C# / F#
  cs: 'csharp',
  csx: 'csharp',
  fs: 'fsharp',
  fsx: 'fsharp',
  fsi: 'fsharp',

  // Swift
  swift: 'swift',

  // Dart
  dart: 'dart',

  // PHP
  php: 'php',
  phtml: 'php',

  // SQL
  sql: 'sql',
  mysql: 'sql',
  pgsql: 'pgsql',

  // Lua
  lua: 'lua',

  // R
  r: 'r',
  rmd: 'markdown',

  // Perl
  pl: 'perl',
  pm: 'perl',
  perl: 'perl',

  // GraphQL
  graphql: 'graphql',
  gql: 'graphql',

  // Docker
  dockerfile: 'dockerfile',

  // Elixir / Erlang
  ex: 'elixir',
  exs: 'elixir',
  erl: 'erlang',
  hrl: 'erlang',

  // Haskell
  hs: 'haskell',
  lhs: 'haskell',

  // Clojure
  clj: 'clojure',
  cljs: 'clojure',
  cljc: 'clojure',
  edn: 'clojure',

  // Handlebars
  hbs: 'handlebars',
  handlebars: 'handlebars',

  // Twig
  twig: 'twig',

  // Pug
  pug: 'pug',
  jade: 'pug',

  // Coffee
  coffee: 'coffeescript',

  // Diff / Patch
  diff: 'diff',
  patch: 'diff',

  // Protocol Buffers
  proto: 'protobuf',

  // Terraform
  tf: 'hcl',
  tfvars: 'hcl',
  hcl: 'hcl',

  // ABAP
  abap: 'abap',

  // Apex
  apex: 'apex',
  cls: 'apex',
  trigger: 'apex',

  // Pascal
  pas: 'pascal',
  pp: 'pascal',

  // Tcl
  tcl: 'tcl',

  // Scheme / Lisp
  scm: 'scheme',
  ss: 'scheme',
  rkt: 'scheme',
  lisp: 'scheme',
  lsp: 'scheme',
  el: 'scheme',

  // Misc
  sol: 'sol',
  bicep: 'bicep',
  azcli: 'azcli',
  redis: 'redis',
  sb: 'sb',
  st: 'st',
  lex: 'lexon',
};

export const getLanguage = (filename: string): string => {
  const ext = filename.split('.').pop()?.toLowerCase() ?? '';
  const base = filename.toLowerCase();
  if (base === 'dockerfile' || base.startsWith('dockerfile.')) return 'dockerfile';
  if (base === 'makefile' || base === 'gnumakefile') return 'shell';
  if (base === 'gemfile' || base === 'rakefile' || base === 'vagrantfile') return 'ruby';
  if (base === 'justfile') return 'shell';
  if (base === '.gitignore' || base === '.dockerignore' || base === '.editorconfig') return 'ini';
  if (base === '.prettierrc' || base === '.eslintrc' || base === 'tsconfig.json' || base === 'package.json')
    return 'json';
  // Dotfiles that are shell scripts
  if (/^\.(bash|zsh|sh|ksh|csh)rc$/.test(base)) return 'shell';
  if (/^\.(bash_|zsh_|sh_)/.test(base)) return 'shell'; // .bash_profile, .zsh_history, etc.
  if (base === '.profile' || base === '.bash_logout' || base === '.bash_login') return 'shell';
  return extensionToLanguage[ext] ?? 'plaintext';
};
