# This module is an interface preventing circular import dependencies.
{
  config,
  lib,
  pkgs,
  ...
}: {
  # To avoid circular import dependency edge cases, this module should not
  # declare any dependencies.
  imports = [];

  options.dotfiles.inputs.home-manager.home.sessionVariables = {
    BROWSER.enable = lib.dotfiles.mkEnableOption;
    EDITOR.enable = lib.dotfiles.mkEnableOption;
    MANPAGER.enable = lib.dotfiles.mkEnableOption;
    SHELL.enable = lib.dotfiles.mkEnableOption;
    TERMINAL.enable = lib.dotfiles.mkEnableOption;
    TMPDIR.enable = lib.dotfiles.mkEnableOption;
    enable = lib.dotfiles.mkEnableOption;
    full = lib.dotfiles.mkEnableOption;
  };

  config = let
    cfg = config.dotfiles.inputs.home-manager.home.sessionVariables;
  in
    lib.mkIf cfg.enable {
      dotfiles.inputs.home-manager.home.sessionVariables = lib.mkIf cfg.full {
        BROWSER.enable = lib.mkDefault true;
        EDITOR.enable = lib.mkDefault true;
        MANPAGER.enable = lib.mkDefault true;
        SHELL.enable = lib.mkDefault true;
        TERMINAL.enable = lib.mkDefault true;
        TMPDIR.enable = lib.mkDefault true;
      };

      # Due to the intentional lack of imports, the pkgs.<PACKAGE> packages are
      # used as a fallback when the internal module packages are unavailable.
      home.sessionVariables = let
        neovim = lib.getExe (
          config.programs.nixvim.finalPackage or pkgs.neovim
        );
      in
        # The
        #
        #     lib.mkMerge [(lib.mkIf <CONDITION> {<KEY> = <VALUE>;})];
        #
        # pattern is used instead of the simpler
        #
        #     {<VARIABLE> = lib.mkIf <CONDITION> <VALUE>;};
        #
        # expression to avoid the following upstream error:
        #
        #     error: The option `home.sessionVariables.<VARIABLE>' is used but
        #     not defined.
        #
        # TODO: Patch an upstream fix.
        lib.mkMerge [
          (
            lib.mkIf cfg.BROWSER.enable {
              BROWSER = lib.getExe (
                config.programs.qutebrowser.package or pkgs.qutebrowser
              );
            }
          )

          (
            lib.mkIf cfg.EDITOR.enable {
              EDITOR = neovim;
            }
          )

          (
            lib.mkIf cfg.MANPAGER.enable {
              MANPAGER = "${neovim} +Man!";
            }
          )

          (
            lib.mkIf cfg.SHELL.enable {
              SHELL = lib.getExe (config.programs.fish.package or pkgs.fish);
            }
          )

          (
            lib.mkIf cfg.TERMINAL.enable {
              TERMINAL = lib.getExe (
                config.programs.kitty.package or pkgs.kitty
              );
            }
          )

          (
            lib.mkIf cfg.TMPDIR.enable {
              TMPDIR = "${config.home.homeDirectory}/tmp";
            }
          )
        ];
    };
}
