{
  pkgs,
  myConfig,
  ...
}: let
  # Yazi wrapper for xdg-desktop-portal-termfilechooser
  # TODO: rewrite in the future
  yaziWrapper = pkgs.writeShellScript "yazi-wrapper.sh" ''
    if [[ "$6" == "1" ]]; then
      set -x
    fi

    multiple="$1"
    directory="$2"
    save="$3"
    path="$4"
    out="$5"

    termcmd="${pkgs.util-linux}/bin/setsid uwsm-app -- xdg-terminal-exec --app-id=org.hakase.popup.yazi -e"

    cleanup() {
      if [ -f "$tmpfile" ]; then
        rm "$tmpfile" || :
      fi
      if [ "$save" = "1" ] && [ ! -s "$out" ]; then
        rm "$path" || :
      fi
    }

    trap cleanup EXIT HUP INT QUIT ABRT TERM

    if [ "$save" = "1" ]; then
      tmpfile=$(mktemp)

      printf '%s' 'xdg-desktop-portal-termfilechooser saving files tutorial

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!                 === WARNING! ===                 !!!
    !!! The contents of *whatever* file you open last in !!!
    !!! yazi will be *overwritten*!                      !!!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Instructions:
    1) Move this file wherever you want.
    2) Rename the file if needed.
    3) Confirm your selection by opening the file, for
       example by pressing <Enter>.

    Notes:
    1) This file is provided for your convenience. You can
       only choose this placeholder file otherwise the save operation aborted.
    2) If you quit yazi without opening a file, this file
       will be removed and the save operation aborted.
    ' >"$path"

      $termcmd ${pkgs.yazi}/bin/yazi --chooser-file="$tmpfile" "$path"
    elif [ "$directory" = "1" ]; then
      $termcmd ${pkgs.yazi}/bin/yazi --cwd-file="$out" "$path"
    elif [ "$multiple" = "1" ]; then
      $termcmd ${pkgs.yazi}/bin/yazi --chooser-file="$out" "$path"
    else
      $termcmd ${pkgs.yazi}/bin/yazi --chooser-file="$out" "$path"
    fi

    # Handle save file case
    if [ "$save" = "1" ] && [ -s "$tmpfile" ]; then
      selected_file=$(head -n 1 "$tmpfile")
      if [ -f "$selected_file" ] && grep -qi "^xdg-desktop-portal-termfilechooser saving files tutorial" "$selected_file"; then
        echo "$selected_file" >"$out"
      fi
    fi
  '';
in {
  wayland.windowManager.hyprland.settings = {
    bindd = [
      "SUPER, X, Open File Manager, exec, uwsm app -- yazi"
    ];
  };

  programs.yazi = {
    enable = true;
    initLua = ''
      require("session"):setup {
        sync_yanked = true,
      }
    '';
    enableFishIntegration = true;
    settings = {
      mgr = {
        show_hidden = false;
        sort_by = "mtime";
        sort_reverse = true;
        sort_dir_first = false;
        linemode = "size";
        mouse_events = ["click" "scroll" "touch" "move" "drag"];
      };
      tasks = {
        image_bound = [10000 10000];
      };
      opener = {
        anki = [
          {
            run = "anki \"$@\"";
            desc = "Open with Anki";
          }
        ];
        pdf = [
          {
            run = "zathura \"$@\"";
            desc = "Open with Zathura";
          }
          {
            run = "firefox \"$@\"";
            desc = "Open with Firefox";
          }
        ];
      };
      open = {
        prepend_rules = [
          {
            name = "*.{apkg,colpkg}";
            use = "anki";
          }
          {
            mime = "application/pdf";
            use = "pdf";
          }
        ];
      };
    };
  };

  # Portal packages need to be in user profile for proper path resolution
  home.packages = [
    pkgs.xdg-desktop-portal-hyprland
    pkgs.xdg-desktop-portal-gtk
    pkgs.xdg-desktop-portal-termfilechooser
    pkgs.p7zip
  ];

  # xdg-desktop-portal-termfilechooser configuration
  xdg.configFile."xdg-desktop-portal-termfilechooser/config".text = ''
    [filechooser]
    cmd=${yaziWrapper}
    default_dir=$HOME/Downloads
  '';

  # Portal routing configuration - use termfilechooser for file dialogs
  xdg.configFile."xdg-desktop-portal/portals.conf".text = ''
    [preferred]
    default=hyprland;gtk
    org.freedesktop.impl.portal.FileChooser=termfilechooser
  '';

  xdg.mimeApps = {
    enable = true;
    defaultApplications = {
      "inode/directory" = "yazi.desktop";
    };
  };

  wayland.windowManager.hyprland.settings.windowrule = [
    # File picker yazi - floating, centered, smaller size
    "match:class ^(org\\.hakase\\.popup\\.yazi)$, float on"
    "match:class ^(org\\.hakase\\.popup\\.yazi)$, size 60% 70%"
    "match:class ^(org\\.hakase\\.popup\\.yazi)$, center on"
  ];
}
