{
  config,
  lib,
  pkgs,
  ...
}: {
  imports = [../../home/sessionVariables];

  options.dotfiles.inputs.home-manager.programs.zellij.enable =
    lib.dotfiles.mkEnableOption;

  config = lib.mkIf config.dotfiles.inputs.home-manager.programs.zellij.enable {
    dotfiles.inputs.home-manager.home.sessionVariables = {
      EDITOR.enable = true;
      enable = true;
    };

    home.shellAliases.${pkgs.hyprland.meta.mainProgram} =
      lib.mkIf
      (config.wayland.windowManager.hyprland.enable or false)
      ''ZELLIJ="" ${lib.getExe pkgs.hyprland}'';

    programs = {
      zellij = {
        enable = true;

        settings = {
          scroll_buffer_size = 100000;

          scrollback_editor = lib.getExe (
            pkgs.writeShellApplication {
              name = "scrollback_editor";
              text = ''
                ${config.home.sessionVariables.EDITOR} \
                  -c 'silent $ | set norelativenumber' \
                  "$@"
              '';
            }
          );

          simplified_ui = true;
        };
      };

      fish.interactiveShellInit = lib.mkBefore ''
        if test -z $ZELLIJ
          exec ${lib.getExe config.programs.zellij.package}
        end
      '';
    };

    # Due a lacking upstream KDL generator, unsupported declarations are
    # declared as strings.
    xdg.configFile = {
      "zellij/config.kdl".text = let
        goToTab = lib.concatLines (
          map
          (
            index: let
              indexString = toString index;
            in ''
              bind "${leader} ${indexString}" {
                GoToTab ${indexString}; SwitchToMode "normal"
              }
            ''
          )
          (lib.lists.range 1 9)
        );

        leader = "Alt";
      in
        lib.mkAfter ''
          keybinds clear-defaults=true {
            locked {
              bind "Esc" { SwitchToMode "normal"; }
            }

            normal {
              ${goToTab}
              bind "${leader} C" { Clear; }
              bind "${leader} D" { Detach; }
              bind "${leader} H" { Resize "Decrease"; }
              bind "${leader} J" { MovePane; }
              bind "${leader} K" { MovePaneBackwards; }
              bind "${leader} L" { Resize "Increase"; }
              bind "${leader} Q" { CloseFocus; SwitchToMode "normal"; }
              bind "${leader} W" { ToggleFloatingPanes; SwitchToMode "normal"; }
              bind "${leader} X" { SwitchToMode "locked"; }
              bind "${leader} c" { NewTab; SwitchToMode "normal"; }
              bind "${leader} e" { EditScrollback; SwitchToMode "normal"; }

              bind "${leader} f" {
                ToggleFocusFullscreen; SwitchToMode "normal";
              }

              bind "${leader} h" { GoToPreviousTab; }
              bind "${leader} j" { FocusNextPane; }
              bind "${leader} k" { FocusPreviousPane; }
              bind "${leader} l" { GoToNextTab; }
              bind "${leader} n" { NextSwapLayout; }
              bind "${leader} p" { PreviousSwapLayout; }
              bind "${leader} t" { NewPane; SwitchToMode "normal"; }

              bind "${leader} w" {
                TogglePaneEmbedOrFloating; SwitchToMode "normal"
              }
            }
          }
        '';

      "zellij/layouts/default.kdl".text = ''
        layout {
          swap_tiled_layout name="horizontal" {
            tab max_panes=5 {
              pane
              pane
            }

            tab max_panes=8 {
              pane {
                pane split_direction="vertical" { children; }
                pane split_direction="vertical" { pane; pane; pane; pane; }
              }
            }

            tab max_panes=12 {
              pane {
                pane split_direction="vertical" { children; }
                pane split_direction="vertical" { pane; pane; pane; pane; }
                pane split_direction="vertical" { pane; pane; pane; pane; }
              }
            }
          }

          swap_tiled_layout name="vertical" {
            tab max_panes=5 {
              pane split_direction="vertical" {
                pane
                pane { children; }
              }
            }

            tab max_panes=8 {
              pane split_direction="vertical" {
                pane { children; }
                pane { pane; pane; pane; pane; }
              }
            }

            tab max_panes=12 {
              pane split_direction="vertical" {
                pane { children; }
                pane { pane; pane; pane; pane; }
                pane { pane; pane; pane; pane; }
              }
            }
          }

          swap_tiled_layout name="stacked" {
            tab min_panes=5 {
              pane split_direction="vertical" {
                pane
                pane stacked=true { children; }
              }
            }
          }

          swap_floating_layout name="staggered" {
            floating_panes
          }

          swap_floating_layout name="enlarged" {
            floating_panes max_panes=10 {
              pane { x "5%"; y 1; width "90%"; height "90%"; }
              pane { x "5%"; y 2; width "90%"; height "90%"; }
              pane { x "5%"; y 3; width "90%"; height "90%"; }
              pane { x "5%"; y 4; width "90%"; height "90%"; }
              pane { x "5%"; y 5; width "90%"; height "90%"; }
              pane { x "5%"; y 6; width "90%"; height "90%"; }
              pane { x "5%"; y 7; width "90%"; height "90%"; }
              pane { x "5%"; y 8; width "90%"; height "90%"; }
              pane { x "5%"; y 9; width "90%"; height "90%"; }
              pane focus=true { x 10; y 10; width "90%"; height "90%"; }
            }
          }

          swap_floating_layout name="spread" {
            floating_panes max_panes=1 {
              pane {y "50%"; x "50%"; }
            }

            floating_panes max_panes=2 {
              pane { x "1%"; y "25%"; width "45%"; }
              pane { x "50%"; y "25%"; width "45%"; }
            }

            floating_panes max_panes=3 {
              pane focus=true { y "55%"; width "45%"; height "45%"; }
              pane { x "1%"; y "1%"; width "45%"; }
              pane { x "50%"; y "1%"; width "45%"; }
            }

            floating_panes max_panes=4 {
              pane { x "1%"; y "55%"; width "45%"; height "45%"; }

              pane focus=true {
                x "50%"; y "55%"; width "45%"; height "45%"
              }

              pane { x "1%"; y "1%"; width "45%"; height "45%"; }
              pane { x "50%"; y "1%"; width "45%"; height "45%"; }
            }
          }
        }
      '';
    };
  };
}
