{ lib, ... }:

# TODO: midi
# TODO: mod-music
# TODO: vgmstream
# TODO: 3d
# TODO: onlyoffice

let
  mime-map = {
    image = {
      apng = "image/apng";
      avif = "image/avif";
      bmp = "image/bmp";
      gif = "image/gif";
      ico = "image/vnd.microsoft.icon";
      icox = "image/x-icon";
      jpg = "image/jpeg";
      jxl = "image/jxl";
      pic = "image/x-pict";
      png = "image/png";
      psd = "image/vnd.adobe.photoshop";
      svg = "image/svg+xml";
      tif = "image/tif";
      tiff = "image/tiff";
      webp = "image/webp";
      xbm = "image/x-xbitmap";
      xpm = "image/x-xpixmap";
    };

    audio = {
      "3g2-audio" = "audio/3gpp";
      "3gp-audio" = "audio/3gpp";
      aac = "audio/aac";
      aiff = "audio/aiff";
      flac = "audio/flac";
      mkv = "audio/x-matroska";
      mpeg = "audio/mpeg"; # NOTE: this is the real mp3, but the other one also exists
      mpeg3 = "audio/mpeg3";
      mp3 = "audio/mp3";
      mp4 = "audio/mp4";
      ogg = "audio/ogg";
      opus = "audio/opus";
      wav = "audio/wav";
      wavx = "audio/x-wav";
      webm-audio = "audio/webm";
    };

    video = {
      "3g2-video" = "video/3gpp";
      "3gp-video" = "video/3gpp";
      avi = "video/x-msvideo";
      flv = "video/x-flv";
      m4v = "video/x-m4v";
      mkv = "video/x-matroska";
      mov = "video/quicktime";
      mp4 = "video/mp4";
      mpeg = "video/mpeg";
      ogv = "video/ogg";
      webm-video = "video/webm";
      wmv = "video/x-ms-wmv";
    };

    fonts = {
      otf = "font/otf";
      ttf = "font/ttf";
      woff = "font/woff";
      woff2 = "font/woff2";
    };

    docs = {
      azv = "application/vnd.amazon.ebook";
      cbr = "application/vnd.comicbook+rar";
      cbrx = "application/x-cbr";
      cbz = "application/vnd.comicbook+zip";
      cbzx = "application/x-cbz";
      djvu = "image/vnd.djvu";
      epub = "application/epub+zip";
      pdf = "application/pdf";
    };

    web = {
      html = "text/html";
      xhtml = "application/xhtml+xml";
      http = "x-scheme-handler/http";
      https = "x-scheme-handler/https";
      url = "application/x-mswinurl";
    };

    code = {
      css = "text/css";
      csv = "text/csv";
      txt = "text/plain";
      xml = "text/xml";
      py = "text/x-script.python";

      wine-ini = "application/x-wine-extension-ini";
      ics = "text/calendar";
    };

    vtty = {
      x-terminal-emulator-1 = "application/x-terminal-emulator";
      x-terminal-emulator-2 = "x-terminal-emulator";
      x-terminal-emulator-3 = "x-scheme-handler/terminal";
    };
  };

  # Applications
  app-map = {
    # ls ~/.nix-profile/share/applications/
    # ls ~/.local/share/applications/
    # ls /run/current-system/sw/share/applications/
    # ls /usr/share/applications/
    image = ["org.gnome.Loupe.desktop" "org.gnome.eog.desktop"];
    audio = ["mpv.desktop" "vlc.desktop" "ZXTune.desktop"];
    video = ["mpv.desktop" "vlc.desktop"];
    fonts = ["org.gnome.font-viewer.desktop"];
    docs = ["org.gnome.Papers.desktop" "org.gnome.Evince.desktop"];
    web = ["firefox.desktop"];
    # code = ["Pulsar.desktop"];
    # code = ["micro.desktop"];
    code = ["dev.zed.Zed.desktop"];
    vtty = ["com.mitchellh.ghostty.desktop" "lxterminal.desktop"];
  };
in {
  xdg.configFile."mimeapps.list".force = true;
  xdg.mimeApps = {
    enable = true;
    # associations.added = {};
    # associations.removed = {};
    defaultApplications = lib.pipe app-map [
      (lib.mapAttrsToList (key: apps:
        lib.forEach (lib.attrValues mime-map.${key}) (mime:
          lib.nameValuePair mime apps
        )
      ))
      lib.flatten
      lib.listToAttrs
    ];
  };
}
