# Bat — cat replacement with nix-colors syntax highlighting theme.
{
  config,
  lib,
  pkgs,
  ...
}:
with lib; let
  cfg = config.base.cliTools.bat;
  palette = config.colorScheme.palette;
in {
  # Enabled by default — base modules are always-on.
  options.base.cliTools.bat.enable = (mkEnableOption "enable bat with nix-colors theme") // {default = true;};

  config = mkIf cfg.enable {
    programs.bat = {
      enable = true;
      config = {theme = "universal";};
      themes = {
        universal = {
          src = pkgs.writeText "universal.tmTheme" ''
            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
            <plist version="1.0">
            <dict>
              <key>name</key>
              <string>Universal (nix-colors)</string>
              <key>settings</key>
              <array>
                <dict>
                  <key>settings</key>
                  <dict>
                    <key>background</key>
                    <string>#${palette.base00}</string>
                    <key>foreground</key>
                    <string>#${palette.base05}</string>
                    <key>caret</key>
                    <string>#${palette.base05}</string>
                    <key>selection</key>
                    <string>#${palette.base02}</string>
                    <key>selectionForeground</key>
                    <string>#${palette.base05}</string>
                    <key>lineHighlight</key>
                    <string>#${palette.base01}</string>
                  </dict>
                </dict>
                <dict>
                  <key>name</key>
                  <string>Comment</string>
                  <key>scope</key>
                  <string>comment</string>
                  <key>settings</key>
                  <dict>
                    <key>foreground</key>
                    <string>#${palette.base03}</string>
                    <key>fontStyle</key>
                    <string>italic</string>
                  </dict>
                </dict>
                <dict>
                  <key>name</key>
                  <string>String</string>
                  <key>scope</key>
                  <string>string</string>
                  <key>settings</key>
                  <dict>
                    <key>foreground</key>
                    <string>#${palette.base0A}</string>
                  </dict>
                </dict>
                <dict>
                  <key>name</key>
                  <string>Number</string>
                  <key>scope</key>
                  <string>constant.numeric</string>
                  <key>settings</key>
                  <dict>
                    <key>foreground</key>
                    <string>#${palette.base0E}</string>
                  </dict>
                </dict>
                <dict>
                  <key>name</key>
                  <string>Keyword</string>
                  <key>scope</key>
                  <string>keyword</string>
                  <key>settings</key>
                  <dict>
                    <key>foreground</key>
                    <string>#${palette.base08}</string>
                  </dict>
                </dict>
                <dict>
                  <key>name</key>
                  <string>Function</string>
                  <key>scope</key>
                  <string>entity.name.function</string>
                  <key>settings</key>
                  <dict>
                    <key>foreground</key>
                    <string>#${palette.base0B}</string>
                  </dict>
                </dict>
                <dict>
                  <key>name</key>
                  <string>Type</string>
                  <key>scope</key>
                  <string>entity.name.type, storage.type</string>
                  <key>settings</key>
                  <dict>
                    <key>foreground</key>
                    <string>#${palette.base0D}</string>
                  </dict>
                </dict>
                <dict>
                  <key>name</key>
                  <string>Variable</string>
                  <key>scope</key>
                  <string>variable</string>
                  <key>settings</key>
                  <dict>
                    <key>foreground</key>
                    <string>#${palette.base05}</string>
                  </dict>
                </dict>
                <dict>
                  <key>name</key>
                  <string>Constant</string>
                  <key>scope</key>
                  <string>constant</string>
                  <key>settings</key>
                  <dict>
                    <key>foreground</key>
                    <string>#${palette.base0E}</string>
                  </dict>
                </dict>
              </array>
            </dict>
            </plist>
          '';
        };
      };
    };
  };
}
