{ config, lib, pkgs, ... }:

let
  inherit (lib) any filterAttrs mapAttrsToList mkEnableOption mkIf mkOption;

  cfg = config.my.programs.jetbrains;
  isEnabled = _: value: value.enable;
  anyEnabled = any (x: x) (mapAttrsToList isEnabled cfg);
in
{
  options.my.programs.jetbrains = {
    pycharm = {
      enable = mkEnableOption "pycharm";
      package = mkOption {
        default = pkgs.jetbrains.pycharm-community;
      };
    };
    idea = {
      enable = mkEnableOption "idea";
      package = mkOption {
        default = pkgs.jetbrains.idea-community;
      };
    };
  };

  config = mkIf anyEnabled {
    home-manager.users.moritz = {
      xdg.configFile."ideavim/ideavimrc" = {
        source = ./ideavimrc;
      };
      home.packages =
        let
          enabledPackages = filterAttrs isEnabled cfg;
        in
        mapAttrsToList (_: value: value.package) enabledPackages;
    };
  };
}
