-- Keymaps are automatically loaded on the VeryLazy event
-- Default keymaps that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/keymaps.lua

-- Remove LazyVim's C-h/j/k/l window navigation (using <leader>h/j/k/l instead)
pcall(vim.keymap.del, "n", "<C-h>")
pcall(vim.keymap.del, "n", "<C-j>")
pcall(vim.keymap.del, "n", "<C-k>")
pcall(vim.keymap.del, "n", "<C-l>")
pcall(vim.keymap.del, "t", "<C-h>")
pcall(vim.keymap.del, "t", "<C-j>")
pcall(vim.keymap.del, "t", "<C-k>")
pcall(vim.keymap.del, "t", "<C-l>")
-- Remove LazyVim's :Lazy shortcut — <leader>l is used for window navigation
pcall(vim.keymap.del, "n", "<leader>l")

-- Window navigation
vim.keymap.set(
  { "n", "v", "x" },
  "<leader>h",
  "<C-w>h",
  { desc = "Go to left window", silent = true }
)
vim.keymap.set(
  { "n", "v", "x" },
  "<leader>j",
  "<C-w>j",
  { desc = "Go to lower window", silent = true }
)
vim.keymap.set(
  { "n", "v", "x" },
  "<leader>k",
  "<C-w>k",
  { desc = "Go to upper window", silent = true }
)
vim.keymap.set(
  { "n", "v", "x" },
  "<leader>l",
  "<C-w>l",
  { desc = "Go to right window", silent = true }
)

-- Buffer tab navigation
vim.keymap.set(
  "n",
  "<C-h>",
  "<cmd>BufferLineCyclePrev<cr>",
  { desc = "Previous buffer", silent = true }
)
vim.keymap.set(
  "n",
  "<C-l>",
  "<cmd>BufferLineCycleNext<cr>",
  { desc = "Next buffer", silent = true }
)
vim.keymap.set(
  "n",
  "<C-j>",
  "<cmd>BufferLineMovePrev<cr>",
  { desc = "Move buffer left", silent = true }
)
vim.keymap.set(
  "n",
  "<C-k>",
  "<cmd>BufferLineMoveNext<cr>",
  { desc = "Move buffer right", silent = true }
)
vim.keymap.set("n", "<C-q>", function()
  Snacks.bufdelete()
end, { desc = "Close buffer", silent = true })
vim.keymap.set("n", "<C-a>", function()
  Snacks.bufdelete.other()
end, { desc = "Close other buffers", silent = true })

-- Disable ex mode
vim.keymap.set("n", "Q", "<nop>")

-- Move selected lines up/down
vim.keymap.set(
  "v",
  "J",
  ":m '>+1<CR>gv=gv",
  { desc = "Move selection down", silent = true }
)
vim.keymap.set(
  "v",
  "K",
  ":m '<-2<CR>gv=gv",
  { desc = "Move selection up", silent = true }
)

-- Join line without moving cursor
vim.keymap.set("n", "J", "mzJ`z", { desc = "Join line, keep cursor" })

-- Scroll and center
vim.keymap.set("n", "<C-d>", "<C-d>zz", { desc = "Scroll down and center" })
vim.keymap.set("n", "<C-u>", "<C-u>zz", { desc = "Scroll up and center" })

-- Search and center
vim.keymap.set("n", "n", "nzzzv", { desc = "Next search result" })
vim.keymap.set("n", "N", "Nzzzv", { desc = "Prev search result" })

-- Yank to system clipboard
vim.keymap.set({ "n", "v" }, "<leader>y", '"+y', { desc = "Yank to clipboard" })
vim.keymap.set("n", "<leader>Y", '"+Y', { desc = "Yank line to clipboard" })
vim.keymap.set(
  "n",
  "<leader>YY",
  ":%y+<CR>",
  { desc = "Yank buffer to clipboard" }
)

-- Delete to black hole register (no yank side effect)
vim.keymap.set(
  { "n", "v" },
  "<leader>d",
  '"_d',
  { desc = "Delete to black hole" }
)
vim.keymap.set(
  "n",
  "<leader>dd",
  '"_dd',
  { desc = "Delete line to black hole" }
)

-- Paste from system clipboard
vim.keymap.set(
  { "n", "v", "x" },
  "<leader>p",
  '"+p',
  { desc = "Paste from clipboard (after)" }
)
vim.keymap.set(
  { "n", "v", "x" },
  "<leader>P",
  '"+P',
  { desc = "Paste from clipboard (before)" }
)

-- Paste over selection without overwriting the yank register
vim.keymap.set("x", "<leader>v", '"_dP', { desc = "Paste over selection" })

-- Utility
vim.keymap.set(
  "n",
  "<leader>so",
  ":update<CR>:source<CR>",
  { desc = "Save and source file" }
)
vim.keymap.set(
  "n",
  "<leader><leader>x",
  ":!chmod +x %<CR>",
  { desc = "Make file executable" }
)
