import { normalizeMimeTypeForCKEditor, SupportedMimeTypes } from "@triliumnext/commons";
import type { LanguageFn } from "highlight.js";

type MimeRecord = Record<SupportedMimeTypes, (() => Promise<{ default: LanguageFn}>) | null>;

export const byMimeType: MimeRecord = {
    "text/plain": () => import("highlight.js/lib/languages/plaintext"),
    "application/dart": () => import("highlight.js/lib/languages/dart"),
    "application/edn": () => import("highlight.js/lib/languages/clojure"),
    "application/javascript;env=backend": () => import("highlight.js/lib/languages/javascript"),
    "application/javascript;env=frontend": () => import("highlight.js/lib/languages/javascript"),
    "application/json": () => import("highlight.js/lib/languages/json"),
    "application/ld+json": () => import("highlight.js/lib/languages/json"),
    "application/mbox": null,
    "application/n-triples": null,
    "application/pgp": null,
    "application/sieve": null,
    "application/sparql-query": null,
    "application/typescript": () => import("highlight.js/lib/languages/typescript"),
    "application/vnd.kdl": () => import("./languages/kdl.js"),
    "application/x-aspx": null,
    "application/x-bat": () => import("highlight.js/lib/languages/dos"),
    "application/x-cypher-query": () => import("highlightjs-cypher"),
    "application/x-ejs": null,
    "application/x-erb": () => import("highlight.js/lib/languages/erb"),
    "application/x-jsp": () => import("highlight.js/lib/languages/java"),
    "application/x-powershell": () => import("highlight.js/lib/languages/powershell"),
    "application/xml-dtd": () => import("highlight.js/lib/languages/xml"),
    "application/xquery": () => import("highlight.js/lib/languages/xquery"),
    "message/http": () => import("highlight.js/lib/languages/http"),
    "text/apl": null,
    "text/coffeescript": () => import("highlight.js/lib/languages/coffeescript"),
    "text/css": () => import("highlight.js/lib/languages/css"),
    "text/html": () => import("highlight.js/lib/languages/xml"),
    "text/jinja2": () => import("highlight.js/lib/languages/django"),
    "text/jsx": () => import("highlight.js/lib/languages/javascript"),
    "text/mirc": () => import("./languages/mirc.js"),
    "text/tiki": null,
    "text/troff": null,
    "text/turtle": null,
    "text/typescript-jsx": () => import("highlight.js/lib/languages/typescript"),
    "text/vbscript": () => import("highlight.js/lib/languages/vbscript"),
    "text/velocity": null,
    "text/vnd.mermaid": null,
    "text/mermaid": null,
    "text/x-abap": () => import("./languages/abap.js"),
    "text/x-asm-mips": () => import("highlight.js/lib/languages/mipsasm"),
    "text/x-asterisk": null,
    "text/x-brainfuck": () => import("highlight.js/lib/languages/brainfuck"),
    "text/x-c++src": () => import("highlight.js/lib/languages/cpp"),
    "text/x-cassandra": null,
    "text/x-clojure": () => import("highlight.js/lib/languages/clojure"),
    "text/x-clojurescript": () => import("highlight.js/lib/languages/clojure"),
    "text/x-cmake": () => import("highlight.js/lib/languages/cmake"),
    "text/x-cobol": () => import("highlightjs-cobol"),
    "text/x-common-lisp": () => import("highlight.js/lib/languages/lisp"),
    "text/x-crystal": () => import("highlight.js/lib/languages/crystal"),
    "text/x-csharp": () => import("highlight.js/lib/languages/csharp"),
    "text/x-csrc": () => import("highlight.js/lib/languages/c"),
    "text/x-cython": null,
    "text/x-d": () => import("highlight.js/lib/languages/d"),
    "text/x-diff": () => import("highlight.js/lib/languages/diff"),
    "text/x-django": () => import("highlight.js/lib/languages/django"),
    "text/x-dockerfile": () => import("highlight.js/lib/languages/dockerfile"),
    "text/x-dylan": () => import("./languages/dylan.js"),
    "text/x-ebnf": () => import("highlight.js/lib/languages/ebnf"),
    "text/x-ecl": null,
    "text/x-eiffel": null,
    "text/x-elixir": () => import("highlight.js/lib/languages/elixir"),
    "text/x-elm": () => import("highlight.js/lib/languages/elm"),
    "text/x-erlang": () => import("highlight.js/lib/languages/erlang"),
    "text/x-esper": null,
    "text/x-factor": null,
    "text/x-fcl": null,
    "text/x-feature": () => import("highlight.js/lib/languages/gherkin"),
    "text/x-forth": null,
    "text/x-fortran": () => import("highlight.js/lib/languages/fortran"),
    "text/x-fsharp": () => import("highlight.js/lib/languages/fsharp"),
    "text/x-gas": null,
    "text/x-gdscript": () => import("@exercism/highlightjs-gdscript"),
    "text/x-gfm": () => import("highlight.js/lib/languages/markdown"),
    "text/x-go": () => import("highlight.js/lib/languages/go"),
    "text/x-groovy": () => import("highlight.js/lib/languages/groovy"),
    "text/x-gss": null,
    "text/x-haml": () => import("highlight.js/lib/languages/haml"),
    "text/x-haskell": () => import("highlight.js/lib/languages/haskell"),
    "text/x-haxe": () => import("highlight.js/lib/languages/haxe"),
    "text/x-hcl": () => import("./languages/terraform.js"),
    "text/x-hxml": null,
    "text/x-idl": null,
    "text/x-java": () => import("highlight.js/lib/languages/java"),
    "text/x-julia": () => import("highlight.js/lib/languages/julia"),
    "text/x-pegjs": null,
    "text/x-kotlin": () => import("highlight.js/lib/languages/kotlin"),
    "text/x-latex": () => import("highlight.js/lib/languages/latex"),
    "text/x-less": () => import("highlight.js/lib/languages/less"),
    "text/x-literate-haskell": null,
    "text/x-livescript": () => import("highlight.js/lib/languages/livescript"),
    "text/x-lua": () => import("highlight.js/lib/languages/lua"),
    "text/x-mariadb": () => import("highlight.js/lib/languages/sql"),
    "text/x-markdown": () => import("highlight.js/lib/languages/markdown"),
    "text/x-mathematica": () => import("highlight.js/lib/languages/mathematica"),
    "text/x-modelica": null,
    "text/x-mscgen": null,
    "text/x-msgenny": null,
    "text/x-mssql": () => import("highlight.js/lib/languages/sql"),
    "text/x-mumps": null,
    "text/x-mysql": () => import("highlight.js/lib/languages/sql"),
    "text/x-nix": () => import("highlight.js/lib/languages/nix"),
    "text/x-nginx-conf": () => import("highlight.js/lib/languages/nginx"),
    "text/x-nsis": () => import("highlight.js/lib/languages/nsis"),
    "text/x-objectivec": () => import("highlight.js/lib/languages/objectivec"),
    "text/x-ocaml": () => import("highlight.js/lib/languages/ocaml"),
    "text/x-octave": null,
    "text/x-oz": null,
    "text/x-pascal": () => import("highlight.js/lib/languages/delphi"),
    "text/x-perl": () => import("highlight.js/lib/languages/perl"),
    "text/x-pgsql": () => import("highlight.js/lib/languages/sql"),
    "text/x-php": () => import("highlight.js/lib/languages/php"),
    "text/x-pig": null,
    "text/x-plsql": () => import("highlight.js/lib/languages/sql"),
    "text/x-properties": () => import("highlight.js/lib/languages/properties"),
    "text/x-protobuf": () => import("highlight.js/lib/languages/protobuf"),
    "text/x-pug": null,
    "text/x-puppet": () => import("highlight.js/lib/languages/puppet"),
    "text/x-python": () => import("highlight.js/lib/languages/python"),
    "text/x-q": () => import("highlight.js/lib/languages/q"),
    "text/x-rpm-changes": null,
    "text/x-rpm-spec": () => import("./languages/rpm-specfile.js"),
    "text/x-rsrc": () => import("highlight.js/lib/languages/r"),
    "text/x-rst": null,
    "text/x-ruby": () => import("highlight.js/lib/languages/ruby"),
    "text/x-rustsrc": () => import("highlight.js/lib/languages/rust"),
    "text/x-sas": () => import("highlight.js/lib/languages/sas"),
    "text/x-sass": () => import("highlight.js/lib/languages/scss"),
    "text/x-scala": () => import("highlight.js/lib/languages/scala"),
    "text/x-scheme": () => import("highlight.js/lib/languages/scheme"),
    "text/x-scss": () => import("highlight.js/lib/languages/scss"),
    "text/x-sh": () => import("highlight.js/lib/languages/bash"),
    "text/x-slim": null,
    "text/x-smarty": null,
    "text/x-sml": () => import("highlight.js/lib/languages/sml"),
    "text/x-solr": null,
    "text/x-soy": null,
    "text/x-spreadsheet": null,
    "text/x-sql": () => import("highlight.js/lib/languages/sql"),
    "text/x-sqlite;schema=trilium": () => import("highlight.js/lib/languages/sql"),
    "text/x-sqlite": () => import("highlight.js/lib/languages/sql"),
    "text/x-squirrel": null,
    "text/x-stex": null,
    "text/x-stsrc": () => import("highlight.js/lib/languages/smalltalk"),
    "text/x-styl": () => import("highlight.js/lib/languages/stylus"),
    "text/x-swift": () => import("highlight.js/lib/languages/swift"),
    "text/x-systemverilog": () => import("highlight.js/lib/languages/verilog"),
    "text/x-tcl": () => import("highlight.js/lib/languages/tcl"),
    "text/x-textile": null,
    "text/x-tiddlywiki": null,
    "text/x-toml": () => import("highlight.js/lib/languages/ini"),
    "text/x-tornado": null,
    "text/x-ttcn-asn": null,
    "text/x-ttcn-cfg": null,
    "text/x-ttcn": () => import("./languages/ttcn3.js"),
    "text/x-twig": () => import("highlight.js/lib/languages/twig"),
    "text/x-vb": () => import("highlight.js/lib/languages/vbnet"),
    "text/x-verilog": () => import("highlight.js/lib/languages/verilog"),
    "text/x-vhdl": () => import("highlight.js/lib/languages/vhdl"),
    "text/x-vue": null,
    "text/x-webidl": null,
    "text/x-xu": null,
    "text/x-yacas": null,
    "text/x-yaml": () => import("highlight.js/lib/languages/yaml"),
    "text/x-z80": null,
    "text/xml": () => import("highlight.js/lib/languages/xml"),
}

const normalizedByMimeType: Partial<MimeRecord> = {};
for (const [mimeType, loader] of Object.entries(byMimeType)) {
    const normalizedMimeType = normalizeMimeTypeForCKEditor(mimeType);
    normalizedByMimeType[normalizedMimeType] = loader;
}

export default normalizedByMimeType as MimeRecord;
