{ lib, theme, ... }:
let
  batTheme = ".config/yazi/catppuccin-${theme.flavor}.tmTheme";
in
{
  home.file.${batTheme}.source = ./catppuccin-mocha.tmTheme;

  programs.yazi = {
    enable = true;
    settings = {
      manager = {
        ratio = [
          2
          4
          2
        ];
        sort_by = "natural";
        sort_sensitive = true;
        sort_reverse = false;
        sort_dir_first = true;
        linemode = "none";
        show_hidden = true;
        show_symlink = true;
      };

      preview = {
        image_filter = "lanczos3";
        image_quality = 90;
        tab_size = 1;
        max_width = 600;
        max_height = 900;
        cache_dir = "";
        ueberzug_scale = 1;
        ueberzug_offset = [
          0
          0
          0
          0
        ];
      };

      tasks = {
        micro_workers = 5;
        macro_workers = 10;
        bizarre_retry = 5;
      };
    };
    theme = lib.mkForce {
      manager = {
        cwd = {
          fg = "#${theme.tealAlpha}";
        };

        hovered = {
          fg = "#${theme.baseAlpha}";
          bg = "#${theme.blueAlpha}";
        };
        preview_hovered = {
          fg = "#${theme.baseAlpha}";
          bg = "#${theme.textAlpha}";
        };

        find_keyword = {
          fg = "#${theme.yellowAlpha}";
          italic = true;
        };
        find_position = {
          fg = "#${theme.pinkAlpha}";
          bg = "reset";
          italic = true;
        };

        marker_copied = {
          fg = "#${theme.greenAlpha}";
          bg = "#${theme.greenAlpha}";
        };
        marker_cut = {
          fg = "#${theme.redAlpha}";
          bg = "#${theme.redAlpha}";
        };
        marker_marked = {
          fg = "#${theme.tealAlpha}";
          bg = "#${theme.tealAlpha}";
        };
        marker_selected = {
          fg = "#${theme.blueAlpha}";
          bg = "#${theme.blueAlpha}";
        };

        tab_active = {
          fg = "#${theme.baseAlpha}";
          bg = "#${theme.textAlpha}";
        };
        tab_inactive = {
          fg = "#${theme.textAlpha}";
          bg = "#${theme.surface1Alpha}";
        };
        tab_width = 1;

        count_copied = {
          fg = "#${theme.baseAlpha}";
          bg = "#${theme.greenAlpha}";
        };
        count_cut = {
          fg = "#${theme.baseAlpha}";
          bg = "#${theme.redAlpha}";
        };
        count_selected = {
          fg = "#${theme.baseAlpha}";
          bg = "#${theme.blueAlpha}";
        };

        border_symbol = "│";
        border_style.fg = "#${theme.overlay1Alpha}";

        syntect_theme = "~/${batTheme}";
      };

      mode = {
        normal_main = {
          fg = "#${theme.baseAlpha}";
          bg = "#${theme.blueAlpha}";
          bold = true;
        };
        normal_alt = {
          fg = "#${theme.blueAlpha}";
          bg = "#${theme.surface0Alpha}";
        };

        select_main = {
          fg = "#${theme.baseAlpha}";
          bg = "#${theme.greenAlpha}";
          bold = true;
        };
        select_alt = {
          fg = "#${theme.greenAlpha}";
          bg = "#${theme.surface0Alpha}";
        };

        unset_main = {
          fg = "#${theme.baseAlpha}";
          bg = "#${theme.flamingoAlpha}";
          bold = true;
        };
        unset_alt = {
          fg = "#${theme.flamingoAlpha}";
          bg = "#${theme.surface0Alpha}";
        };
      };

      status = {
        separator_open = "";
        separator_close = "";

        progress_label = {
          fg = "#ffffff";
          bold = true;
        };
        progress_normal = {
          fg = "#${theme.blueAlpha}";
          bg = "#${theme.surface1Alpha}";
        };
        progress_error = {
          fg = "#${theme.redAlpha}";
          bg = "#${theme.surface1Alpha}";
        };

        perm_type = {
          fg = "#${theme.blueAlpha}";
        };
        perm_read = {
          fg = "#${theme.yellowAlpha}";
        };
        perm_write = {
          fg = "#${theme.redAlpha}";
        };
        perm_exec = {
          fg = "#${theme.greenAlpha}";
        };
        perm_sep = {
          fg = "#${theme.overlay1Alpha}";
        };
      };

      input = {
        border = {
          fg = "#${theme.blueAlpha}";
        };
        title = { };
        value = { };
        selected.reversed = true;
      };

      pick = {
        border = {
          fg = "#${theme.blueAlpha}";
        };
        active = {
          fg = "#${theme.pinkAlpha}";
        };
        inactive = { };
      };

      confirm = {
        border = {
          fg = "#${theme.blueAlpha}";
        };
        title = {
          fg = "#${theme.blueAlpha}";
        };
        content = { };
        list = { };
        btn_yes = {
          reversed = true;
        };
        btn_no = { };
      };

      completion = {
        border = {
          fg = "#${theme.blueAlpha}";
        };
      };
      tasks = {
        border = {
          fg = "#${theme.blueAlpha}";
        };
        title = { };
        hovered = {
          underline = true;
        };
      };

      which = {
        mask = {
          bg = "#${theme.surface0Alpha}";
        };
        cand = {
          fg = "#${theme.tealAlpha}";
        };
        rest = {
          fg = "#9399b2";
        };
        desc = {
          fg = "#${theme.pinkAlpha}";
        };
        separator = "  ";
        separator_style = {
          fg = "#${theme.surface2Alpha}";
        };
      };
      help = {
        on = {
          fg = "#${theme.tealAlpha}";
        };
        run = {
          fg = "#${theme.pinkAlpha}";
        };
        desc = {
          fg = "#9399b2";
        };
        hovered = {
          bg = "#${theme.surface2Alpha}";
          bold = true;
        };
        footer = {
          fg = "#${theme.textAlpha}";
          bg = "#${theme.surface1Alpha}";
        };
      };

      notify = {
        title_info = {
          fg = "#${theme.tealAlpha}";
        };
        title_warn = {
          fg = "#${theme.yellowAlpha}";
        };
        title_error = {
          fg = "#${theme.redAlpha}";
        };
      };
      filetype = {
        rules = [
          # Media
          {
            mime = "image/*";
            fg = "#${theme.tealAlpha}";
          }
          {
            mime = "{audio;video}/*";
            fg = "#${theme.yellowAlpha}";
          }

          # Archives
          {
            mime = "application/*zip";
            fg = "#${theme.pinkAlpha}";
          }
          {
            mime = "application/x-{tar;bzip*;7z-compressed;xz;rar}";
            fg = "#${theme.pinkAlpha}";
          }

          # Documents
          {
            mime = "application/{pdf;doc;rtf}";
            fg = "#${theme.greenAlpha}";
          }

          # Fallback
          {
            name = "*";
            fg = "#${theme.textAlpha}";
          }
          {
            name = "*/";
            fg = "#${theme.blueAlpha}";
          }
        ];
      };

      icon = import ./icons.nix;
    };
  };
}
