vim.g.mapleader = " "
vim.g.maplocalleader = "\\"
-- (mode, key, command)

local function map(mode, lhs, rhs, opts)
	local options = { noremap = true, silent = true }
	if opts then
		if opts.desc then
			opts.desc = "keymaps.lua: " .. opts.desc
		end
		options = vim.tbl_extend("force", options, opts)
	end
	vim.keymap.set(mode, lhs, rhs, options)
end

map("n", "<leader>sn", "<cmd>noautocmd w <CR>", { desc = "Save without formatting" }) -- save without autoformat

map("n", "x", '"_x', { desc = "Remove character under cursor without clipboard" })

map("n", "n", "nzzzv", { desc = "Find next occurence of search" })
map("n", "N", "Nzzzv", { desc = "Find previous occurence of search" })

map("n", "<Tab>", ":bnext<CR>", { desc = "Switch to next buffer" })
map("n", "<S-Tab>", ":bprev<CR>", { desc = "Switch to previous buffer" })
map("n", "<leader>xb", ":bdelete!<CR>", { desc = "Close buffer" })
map("n", "<leader>b", "<cmd> enew <CR>", { desc = "Open new buffer" })

map("n", "<leader>v", "<C-w>v", { desc = "Split vertical" })
map("n", "<leader>h", "<C-w>s", { desc = "Split horizontal" })
map("n", "<leader>se", "<C-w>=", { desc = "Reset split sizes" })
map("n", "<leader>xs", ":close<CR>", { desc = "Close split" })

map("v", "<", "<gv", { desc = "Indent left" })
map("v", ">", ">gv", { desc = "Indent right" })
map("v", "p", '"_dP', { desc = "Paste without yanking underlying text" })

map("n", "gl", "`.", { desc = "Jump to the last change in the file" })

map("n", "<C-d>", "<C-d>zz")
map("n", "<C-u>", "<C-u>zz")
map("n", "<C-f>", "<C-f>zz")
map("n", "<C-b>", "<C-b>zz")

-- map('n', '<C-f>', 'za', { desc = 'Toggle cursor fold' })
-- map('n', '<C-down>', 'zm', { desc = 'Toggle all folds at cursor' })
-- map('n', '<C-S-down>', 'zM', { desc = 'Close all open folds' })
-- map('n', '<C-up>', 'zr', { desc = 'Open all folds' })
-- map('n', '<C-S-up>', 'zR', { desc = 'Open all folds' })

-- map("n", "<S-up>", "<C-w><up>", { desc = "Move to split upwards" })
-- map("n", "<S-down>", "<C-w><down>", { desc = "Move to split upwards" })
-- map("n", "<S-left>", "<C-w><left>", { desc = "Move to split left" })
-- map("n", "<S-right>", "<C-w><right>", { desc = "Move to split right" })

map("n", "<C-up>", "<C-y>", { desc = "Move screen up one line" })
map("n", "<C-down>", "<C-e>", { desc = "Move screen down one line" })
map("v", "<C-up>", "<C-y>", { desc = "Move screen up one line" })
map("v", "<C-down>", "<C-e>", { desc = "Move screen down one line" })
