{ inputs, pkgs, lib, ... }:
{
  programs.yazi = {
    enable = true;
    enableFishIntegration = true;
    package = inputs.yazi.packages.x86_64-linux.default;
    initLua = ./yazi/init.lua;

    plugins = let
      officialPluginsNames = [
        "chmod"
        "diff"
        "git"
        "mount"
        # "toggle-pane"
      ];

      officialPluginsSrc = pkgs.fetchgit {
        url = "https://github.com/yazi-rs/plugins.git";
        sparseCheckout = map (p: "${p}.yazi") officialPluginsNames;
        rev = "HEAD";
        hash = "sha256-3F44uFeFBX7PNXo2/maiAzkA/OfweyN4nbDhftna+CI=";
      };

      officialPlugins =
        lib.lists.fold (
          a: b:
          {
            ${a} = "${officialPluginsSrc}/${a}.yazi";
          }
          // b
        )
          {}
          officialPluginsNames;
    in
      officialPlugins // {
        yatline = pkgs.fetchFromGitHub {
          owner = "imsi32";
          repo = "yatline.yazi";
          rev = "HEAD";
          sha256 = "sha256-JiUik4umi+ujsJmHliUbLsIj7ZTKayrWAAM8HmRdjwk=";
        };
        restore = pkgs.fetchFromGitHub {
          owner = "boydaihungst";
          repo = "restore.yazi";
          rev = "HEAD";
          sha256 = "sha256-OJJPgpSaUHYz8a9opVLCds+VZsK1B6T+pSRJyVgYNy8=";
        };
        lazygit = pkgs.fetchFromGitHub {
          owner = "Lil-Dank";
          repo = "lazygit.yazi";
          rev = "HEAD";
          sha256 = "sha256-OJJPgpSaUHYz8a9opVLCds+VZsK1B6T+pSRJyVgYNy8=";
        };
        ouch = pkgs.fetchFromGitHub {
          owner = "ndtoan96";
          repo = "ouch.yazi";
          rev = "HEAD";
          sha256 = "sha256-7X8uAiJ8vBXYBXOgyKhVVikOnTBGrdCcXOJemjQNolI=";
        };
        rich-preview = pkgs.fetchFromGitHub {
          owner = "AnirudhG07";
          repo = "rich-preview.yazi";
          rev = "HEAD";
          sha256 = "sha256-dW2gAAv173MTcQdqMV32urzfrsEX6STR+oCJoRVGGpA=";
        };
        mediainfo= pkgs.fetchFromGitHub {
          owner = "boydaihungst";
          repo = "mediainfo.yazi";
          rev = "HEAD";
          sha256 = "sha256-U6rr3TrFTtnibrwJdJ4rN2Xco4Bt4QbwEVUTNXlWRps=";
        };

        dir-rules = ./yazi/plugins/dir-rules.yazi;
        smart-tab = ./yazi/plugins/smart-tab.yazi;
        smart-paste = ./yazi/plugins/smart-paste.yazi;
      };

    keymap = {
      manager.prepend_keymap = [
        { on   = "!"; run  = "shell '$SHELL' --block"; desc = "Open shell here"; }

        { on = ["c" "d"]; run = "shell 'ripdrag \"$@\" -x 2>/dev/null &' --confirm"; desc = "Drag selection";}
        { on = ["c" "c"]; run = "yank"; desc = "Copy file"; }
        { on = "y"; run = ["shell 'for path in \"$@\"; do echo \"file://$path\"; done | wl-copy -t text/uri-list'\n" "yank"]; }

        { on = ["g" "r"]; run = "shell 'ya emit cd \"$(git rev-parse --show-toplevel)\"'\n"; desc = "Go to top of git repo"; }
        { on = ["g" "p"]; run = "cd ~/Projects"; desc = "Go to ~/Projects"; }

        { on = ["g" "l"]; run = "plugin lazygit"; desc = "lazygit"; }

        # { on = ["t" "p"]; run = "plugin toggle-pane min-parent"; desc = "Toggle parent"; }
        # { on = ["t" "c"]; run = "plugin toggle-pane min-current"; desc = "Toggle current"; }
        # { on = ["t" "r"]; run = "plugin toggle-pane min-preview"; desc = "Toggle preview"; }

        { on = "<C-t>"; run ="tab_create"; }
        { on = "<C-w>"; run ="tab_close"; }
        { on = "<C-A-right>"; run ="tab_switch 1 --relative"; }
        { on = "<C-A-left>"; run ="tab_switch -1 --relative"; }

        { on = "<Tab>"; run = "spot"; desc = "Spot hovered file"; }

        { on = "M"; run = "plugin mount"; }

        { on = "p"; run = "plugin smart-paste"; desc = "Paste into the hovered directory or CWD"; }

        { on = "C"; run = "plugin ouch zip"; }

        { on = "T"; run = "plugin smart-tab"; desc = "Create a tab and enter the hovered directory"; }

        { on = "<Backspace>"; run = "remove"; }
        { on = ["d" "d"]; run = "remove"; desc = "Delete files/folders"; }
        { on = ["d" "u"]; run = "plugin restore"; desc = "Restore last deleted files/folders"; }

        { on = "f"; run = "filter --smart"; desc = "Find file"; }
        { on = "F"; run = "find --smart"; desc = "Find file"; }

        { on = "z"; run = "plugin zoxide"; }
        { on = "Z"; run = "plugin fzf"; }
      ];
    };
    settings = {
      manager = {
        sort_dir_first = false;
        linemode = "mtime";
        show_symlink = true;
      };
      opener = {
        # open = [
        #   { run  = "plugin open-with-cmd block"; desc = "Open with command in the terminal"; }
        #   { run  = "plugin open-with-cmd"; desc = "Open with command"; }
        # ];
        edit = [
          { run = "emacsclient -r -n \"$@\""; desc = "open in current emacsclient"; orphan = true; }
        ];
        extract = [
	        { run = "ouch d -y \"$@\""; desc = "Extract here with ouch"; }
        ];
      };
      plugin = {
        prepend_preloaders = [
          { name = "/mnt/**"; run = "noop"; }
          { name = "/home/emenel/Mounts/**"; run = "noop"; }
        ];
        prepend_previewers = [
          { name = "*.csv"; run = "rich-preview"; }
          { name = "*.md"; run = "rich-preview" ; }
          { name = "*.rst"; run = "rich-preview"; }
          { name = "*.ipynb"; run = "rich-preview"; }
          { name = "*.json"; run = "rich-preview"; }
          { name = "*.lisp"; run = "rich-preview"; }
          { name = "*.el"; run = "rich-preview"; }
          { name = "*.org"; run = "rich-preview"; }
        ];
        append_previewers = [
          { mime = "{image,audio,video}/*"; run = "mediainfo";}
          { mime = "application/x-subrip"; run = "mediainfo";}
          # Archive previewer
          { mime = "application/*zip";            run = "ouch"; }
          { mime = "application/x-tar";           run = "ouch"; }
          { mime = "application/x-bzip2";         run = "ouch"; }
          { mime = "application/x-7z-compressed"; run = "ouch"; }
          { mime = "application/x-rar";           run = "ouch"; }
          { mime = "application/x-xz";            run = "ouch"; }
          { mime = "application/octet-stream";    run = "ouch"; }
        ];
        _spotters = [
          { name = "*"; run = "file-extra-metadata"; }
        ];
        prepend_fetchers = [
          # {
          #   id = "mime";
          #   name = "*";
          #   run = "mime-ext";
          #   prio = "high";
          # }
          # {
          #   id   = "git";
          #   name = "*";
          #   run  = "git";
          # }
          # {
          #   id   = "git";
          #   name = "*/";
          #   run  = "git";
          # }
        ];
      };
    };
  };

  xdg.configFile."yazi/theme.toml".source = ./yazi/theme.toml;
  xdg.configFile."yazi/flavors".source = ./yazi/flavors;
}
