local map = vim.api.nvim_set_keymap
local kmap = vim.keymap.set
-- Базовые настройки навигации --
map("n", "<Space>", "<Nop>", { noremap = true, silent = true })

-- Навигация в начало/конец строки
map("n", "<Home>", "^", { noremap = true, silent = true })
map("n", "<End>", "$", { noremap = true, silent = true })
map("v", "<Home>", "^", { noremap = true, silent = true })
map("v", "<End>", "$", { noremap = true, silent = true })
map("i", "<Home>", "<C-o>^", { noremap = true, silent = true })
map("i", "<End>", "<C-o>$", { noremap = true, silent = true })

-- Фикс для визуального режима
map("v", "i", "<S-i>", { noremap = true, silent = true })
map("v", "a", "<S-a>", { noremap = true, silent = true })
map("v", "J", ":m '>+1<CR>gv=gv", { noremap = true, silent = true })
map("v", "K", ":m '<-2<CR>gv=gv", { noremap = true, silent = true })
map("v", "<", "<gv", { noremap = true, silent = true })
map("v", ">", ">gv", { noremap = true, silent = true })

-- {{{ Управление буферами --
kmap("n", "<leader>bn", "<cmd>bnext<CR>", { noremap = true, silent = true, desc = "Next buffer" })
kmap("n", "<leader>bp", "<cmd>bprevious<CR>", { noremap = true, silent = true, desc = "Previous buffer" })
kmap("n", "<leader>bd", "<cmd>bdelete<CR>", { noremap = true, silent = true, desc = "Delete buffer" })
kmap("n", "<C-s>", "<cmd>wa<CR>", { noremap = true, silent = true, desc = "Save all files" })
kmap("n", "<leader>q", "<cmd>q<CR>", { noremap = true, silent = true, desc = "Exit" })
kmap("n", "<leader>vs", "<cmd>vsplit<CR>", { noremap = true, silent = true, desc = "Vertical split" })
-- }}}

-- {{{ Управление вкладками --
kmap("n", "<A-Right>", ":tabnext<CR>", { noremap = true, silent = true, desc = "Next tab" })
kmap("n", "<A-Left>", ":tabprevious<CR>", { noremap = true, silent = true, desc = "Previous tab" })
kmap("n", "<A-t>", ":tabnew<CR>", { noremap = true, silent = true, desc = "New tab" })
kmap("n", "<A-w>", ":tabclose<CR>", { noremap = true, silent = true, desc = "Close tab" })

-- Быстрый переход по вкладкам --
for i = 1, 9 do
	kmap("n", "<A-" .. i .. ">", ":tabn " .. i .. "<CR>", { noremap = true, silent = true, desc = "Go to tab " .. i })
end

-- }}}

-- {{{ LSP функции
kmap("n", "d[", vim.diagnostic.goto_prev, { noremap = true, silent = true, desc = "Previous diagnostic" })
kmap("n", "d]", vim.diagnostic.goto_next, { noremap = true, silent = true, desc = "Next diagnostic" })

kmap("n", "gD", vim.lsp.buf.declaration, { noremap = true, silent = true, desc = "Go to declaration" })
kmap("n", "gd", vim.lsp.buf.definition, { noremap = true, silent = true, desc = "Go to definition" })
kmap("n", "K", vim.lsp.buf.hover, { noremap = true, silent = true, desc = "Show documentation" })

kmap("n", "<C-k>", vim.lsp.buf.signature_help, { noremap = true, silent = true, desc = "Signature help" })

kmap(
	"n",
	"<leader>wa",
	vim.lsp.buf.add_workspace_folder,
	{ noremap = true, silent = true, desc = "Add workspace folder" }
)

kmap(
	"n",
	"<leader>wr",
	vim.lsp.buf.remove_workspace_folder,
	{ noremap = true, silent = true, desc = "Remove workspace folder" }
)

kmap("n", "<leader>wl", function()
	print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, { noremap = true, silent = true, desc = "List workspace folders" })

--kmap("n", "<A-CR>", vim.lsp.buf.code_action, { noremap = true, silent = true, desc = "Code actions" })
kmap("n", "<A-CR>", "<cmd>Lspsaga code_action<cr>", { noremap = true, silent = true, desc = "Code actions" })

kmap("n", "<F2>", vim.lsp.buf.rename, { noremap = true, silent = true, desc = "Rename symbol" })

-- }}}
