{
  config,
  pkgs,
  lib,
  ...
}: let
  inherit
    (lib)
    rp
    mkIf
    mkMerge
    ;
  cfg = config.gtk.rose-pine;
  enable = cfg.enable && config.gtk.enable;

  themeName =
    if (cfg.flavor == "main")
    then "rose-pine"
    else "rose-pine-${cfg.flavor}";
in {
  options.gtk.rose-pine =
    rp.mkRosePineOpt {
      name = "gtk";
      enableDefault = false;
    }
    // {
      icon = rp.mkRosePineOpt {
        name = "GTK icon theme";
        # NOTE: we exclude this from the global `catppuccin.enable` as there
        # is no `enable` option in the upstream module to guard it.
        enableDefault = false;
      };
    };

  config = mkMerge [
    (mkIf enable {
      gtk.theme = {
        name = "${themeName}-gtk";
        package = pkgs.rose-pine-gtk-theme;
      };

      xdg.configFile = let
        gtk4Dir = "${config.gtk.theme.package}/share/themes/${themeName}/gtk-4.0";
      in {
        "gtk-4.0/gtk.css".source = "${gtk4Dir}/gtk.css";
      };
    })

    (mkIf cfg.icon.enable {
      gtk.iconTheme = {
        name = "${themeName}-icons";
        package = pkgs.rose-pine-icon-theme;
      };
    })
  ];
}
