{ lib, config, pkgs, ... }:
let
  openOnCurrentScreen = pkgs.writeShellScriptBin "eww-open-on-current-screen" ''
    window="$1"
    shift

    screen="$(hyprctl monitors -j | ${lib.getExe pkgs.jq} -r '.[] | select(.focused == true) | .name' | head -n1)"

    if [ -n "$screen" ]; then
      exec ${lib.getExe pkgs.eww} open "$window" --screen "$screen" "$@"
    fi

    exec ${lib.getExe pkgs.eww} open "$window" "$@"
  '';

  # Wraps a static script file with a bash launcher that prepends Nix store
  # bin dirs to PATH — keeps the source files unchanged.
  mkScript = name: src: inputs: pkgs.writeShellScriptBin name ''
    export PATH="${lib.makeBinPath inputs}:$PATH"
    ${builtins.readFile src}
  '';

  scripts = {
    "scripts/brightness"   = mkScript "brightness"   ./bar/scripts/brightness   [ pkgs.brightnessctl ];
    "scripts/nightlight"   = mkScript "nightlight"   ./bar/scripts/nightlight   [ pkgs.wlsunset ];
    "scripts/panel-toggle" = mkScript "panel-toggle" ./bar/scripts/panel-toggle [ pkgs.eww pkgs.jq ];
    "scripts/power-save"   = mkScript "power-save"   ./bar/scripts/power-save   [ pkgs.eww ];
    "scripts/radio"        = mkScript "radio"        ./bar/scripts/radio        [ pkgs.eww pkgs.jq pkgs.curl pkgs.mpv pkgs.playerctl ];
    "scripts/color-pick"   = mkScript "color-pick"   ./bar/scripts/color-pick   [ pkgs.eww pkgs.hyprpicker ];
    "scripts/media"        = mkScript "media"        ./bar/scripts/media        [ pkgs.playerctl ];
    "scripts/lock"         = mkScript "lock"         ./bar/scripts/lock         [ pkgs.eww pkgs.swaylock-effects ];
    "scripts/screenshot"   = mkScript "screenshot"   ./bar/scripts/screenshot   [ pkgs.eww pkgs.hyprshot pkgs.satty pkgs.wl-clipboard ];
    "scripts/sys/gpu"      = mkScript "gpu"          ./bar/scripts/sys/gpu      [ pkgs.custom.amdgpu_top pkgs.jq ];
    "scripts/sys/memory"   = mkScript "memory"       ./bar/scripts/sys/memory   [ pkgs.ripgrep ];
    "scripts/volume"       = mkScript "volume"       ./bar/scripts/volume       [ pkgs.ripgrep ];
    "scripts/weather"      = mkScript "weather"      ./bar/scripts/weather      [ pkgs.jq pkgs.curl ];
    "scripts/workspaces"   = mkScript "workspaces"   ./bar/scripts/workspaces   [ pkgs.jaq pkgs.socat pkgs.ripgrep ];
  };
in {

  config = lib.mkIf (config.usercfg.wm == "Wayland") {
    home.packages = [ pkgs.eww openOnCurrentScreen ];

    xdg.configFile = lib.mkMerge [
      {
        "eww" = {
          source = lib.cleanSourceWith {
            filter = name: _type:
              let
                baseName = baseNameOf (toString name);
                relPath = lib.removePrefix (toString ./bar + "/") (toString name);
              in !(lib.hasSuffix ".nix" baseName)
              && (baseName != "_colors.scss")
              && !(builtins.elem relPath (builtins.attrNames scripts));
            src = lib.cleanSource ./bar/.;
          };
          recursive = true;
        };

        "eww/css/_colors.scss".text = ''
          $base00: #${config.colorScheme.palette.base00};
          $base01: #${config.colorScheme.palette.base01};
          $base02: #${config.colorScheme.palette.base02};
          $base03: #${config.colorScheme.palette.base03};
          $base04: #${config.colorScheme.palette.base04};
          $base05: #${config.colorScheme.palette.base05};
          $base06: #${config.colorScheme.palette.base06};
          $base07: #${config.colorScheme.palette.base07};
          $base08: #${config.colorScheme.palette.base08};
          $base09: #${config.colorScheme.palette.base09};
          $base0A: #${config.colorScheme.palette.base0A};
          $base0B: #${config.colorScheme.palette.base0B};
          $base0C: #${config.colorScheme.palette.base0C};
          $base0D: #${config.colorScheme.palette.base0D};
          $base0E: #${config.colorScheme.palette.base0E};
          $base0F: #${config.colorScheme.palette.base0F};


          $fg: $base07;
          $bg0: $base00;
          $bg1: $base01;

          $border-color: $base03;
          $border-color-focus: $base04;
          $border-radius: ${config.colorScheme.palette.border-radius}px;
          $border-width: ${config.colorScheme.palette.border-width}px;

          $gaps-screen: ${config.colorScheme.palette.gaps-screen}px;
          $gaps-window: ${config.colorScheme.palette.gaps-window}px;

          $panel-font-size: 10pt;
          $popup-scale: 1.25;
        '';
      }

      (lib.mapAttrs' (rel: drv: {
        name = "eww/${rel}";
        value.source = "${drv}/bin/${drv.name}";
      }) scripts)
    ];
  };
}
