{ config, lib, ... }:

let
  cfg = config.subs.zed;
in
{
  options = {
    subs.zed.enable = lib.mkOption {
      default = false;
      type = lib.types.bool;
    };
  };

  config = lib.mkIf cfg.enable {
    programs.zed-editor = {
      enable = true;
      extensions = [
        "kanagawa-themes" "modus-themes" "material-icon-theme"
        "toml" "php" "dockerfile" "vue" "sql" "svelte" "scss" "lua"
        "astro" "graphql" "ini" "emmet"
      ];

      userKeymaps = [
        {
          context = "Workspace";
          bindings = {
            "alt-`" = "workspace::ToggleLeftDock";
            "alt-~" = "workspace::ToggleRightDock";
            "ctrl-`" = "workspace::ToggleBottomDock";
            "alt-h" = "workspace::ActivatePaneLeft";
            "alt-j" = "workspace::ActivatePaneDown";
            "alt-k" = "workspace::ActivatePaneUp";
            "alt-l" = "workspace::ActivatePaneRight";
          };
        }
        {
          context = "Editor";
          bindings = {
            "ctrl-w" = "pane::CloseActiveItem";
          };
        }
        {
          context = "VimControl && !menu";
          bindings = {
            "ctrl-w" = "pane::CloseActiveItem";
          };
        }
        {
          context = "Editor || Pane";
          bindings = {
            "ctrl-alt-h" = "pane::SplitLeft";
            "ctrl-alt-j" = "pane::SplitDown";
            "ctrl-alt-k" = "pane::SplitUp";
            "ctrl-alt-l" = "pane::SplitRight";
          };
        }
      ];

      userSettings = {
        vim_mode = true;
        base_keymap = "VSCode";
        diagnostics = {
          include_warnings = false;
          inline.enabled = true;
        };
        tab_size = 4;
        hard_tabs = true;
        git.inline_blame.enabled = false;
        ui_font_family = "Inter";
        ui_font_size = 16;
        buffer_font_family = "Iosevka Nerd Font";
        buffer_font_fallbacks = [ "Symbols Nerd Font" "Noto Color Emoji" ];
        buffer_font_weight = 500;
        buffer_font_size = 19;
        buffer_line_height.custom = 1.3;
        terminal = {
          font_family = "Iosevka Nerd Font";
          font_weight = 500;
          font_size = 16;
          font_fallbacks = [ "Symbols Nerd Font" "Noto Color Emoji" ];
          working_directory = "current_project_directory";
          toolbar.breadcrumbs = false;
          env = {
            "ZED" = "1";
          };
        };
        icon_theme = "Material Icon Theme";
        theme = {
          mode = "dark";
          light = "Modus Operandi Deuteranopia";
          dark = "Kanagawa";
        };
        toolbar.quick_actions = false;
        tab_bar.show = false;
        scrollbar = {
          show = "never";
          git_diff = false;
        };
        gutter = {
          code_actions = true;
          runnables = true;
          folds = false;
          line_numbers = true;
        };
        inlay_hints = {
          enabled = true;
          show_type_hints = true;
        };
        telemetry = {
          diagnostics = false;
          metrics = false;
        };
        languages = {
          CSS.tab_size = 2;
          HTML.tab_size = 2;
          Astro.tab_size = 2;
          JavaScript.tab_size = 2;
          TypeScript.tab_size = 2;
          TSX.tab_size = 2;
          JSON.hard_tabs = false;
          JSON.tab_size = 2;
          YAML.hard_tabs = false;
          YAML.tab_size = 2;
          JSONC.hard_tabs = false;
          JSONC.tab_size = 2;
          TOML.tab_size = 2;
          Python.hard_tabs = false;
        };
      };
    };
  };
}
