{ config, lib, ... }:

let
  cfg = config.programs.nixvim;
  plugin = cfg.plugins.diffview;

  inherit (lib) mkDefault mkIf;
in
{
  config = {
    programs.nixvim = {
      plugins.diffview = {
        enable = mkDefault true;
      };

      # highlight = mkIf plugin.enable {
      #   DiffAdd = {
      #     bg = "#2d4a2d";
      #     fg = "NONE";
      #   };
      #   DiffDelete = {
      #     bg = "#4a2d2d";
      #     fg = "NONE";
      #   };
      #   DiffChange = {
      #     bg = "#2d3a4a";
      #     fg = "NONE";
      #   };
      #   DiffText = {
      #     bg = "#1a5a1a";
      #     fg = "NONE";
      #   };
      # };

      keymaps = mkIf plugin.enable [
        {
          mode = "n";
          key = "<leader>gd";
          action.__raw = ''
            function()
              local lib = require("diffview.lib")
              local view = lib.get_current_view()
              if view then
                vim.cmd("DiffviewClose")
              else
                vim.cmd("DiffviewOpen")
              end
            end
          '';
          options = {
            noremap = true;
            silent = true;
            desc = "toggle git diff";
          };
        }
      ];
    };
  };
}
