{ inputs, writeShellScript, system, stdenv, cage, swww, esbuild, dart-sass, fd
, fzf, brightnessctl, accountsservice, slurp, wf-recorder, wl-clipboard
, grimblast, satty, hyprpicker, pavucontrol, networkmanager, gtk3, which
, killall, }:
let
  name = "asztal";

  ags = inputs.ags.packages.${system}.default.override {
    extraPackages = [ accountsservice ];
  };

  dependencies = [
    which
    dart-sass
    fd
    fzf
    brightnessctl
    swww
    # inputs.matugen.packages.${system}.default
    slurp
    grimblast
    satty
    wf-recorder
    wl-clipboard
    # wayshot
    # swappy
    hyprpicker
    pavucontrol
    networkmanager
    gtk3
    killall
  ];

  addBins = list:
    builtins.concatStringsSep ":" (builtins.map (p: "${p}/bin") list);

  greeter = writeShellScript "greeter" ''
    export PATH=$PATH:${addBins dependencies}
    ${cage}/bin/cage -ds -m last ${ags}/bin/ags -- -c ${config}/greeter.js
  '';

  desktop = writeShellScript name ''
    export PATH=$PATH:${addBins dependencies}
    ${ags}/bin/ags -b ${name} -c ${config}/config.js $@
  '';

  config = stdenv.mkDerivation {
    inherit name;
    src = ./.;

    buildPhase = ''
      ${esbuild}/bin/esbuild \
        --bundle ./main.ts \
        --outfile=main.js \
        --format=esm \
        --external:resource://\* \
        --external:gi://\* \

      ${esbuild}/bin/esbuild \
        --bundle ./greeter/greeter.ts \
        --outfile=greeter.js \
        --format=esm \
        --external:resource://\* \
        --external:gi://\* \
    '';

    installPhase = ''
      mkdir -p $out
      cp -r assets $out
      cp -r style $out
      cp -r greeter $out
      cp -r widget $out
      cp -f main.js $out/config.js
      cp -f greeter.js $out/greeter.js
    '';
  };
in stdenv.mkDerivation {
  inherit name;
  src = config;

  installPhase = ''
    mkdir -p $out/bin
    cp -r . $out
    cp ${desktop} $out/bin/${name}
    cp ${greeter} $out/bin/greeter
  '';
}
