local utils = require("user.utils")

require("gitsigns").setup({
	signs = {
		add = { text = "│" },
		change = { text = "│" },
		delete = { text = "_" },
		topdelete = { text = "‾" },
		changedelete = { text = "~" },
		untracked = { text = "┆" },
	},
	on_attach = function(bufnr)
		local gs = package.loaded.gitsigns

		local function map(mode, l, r, opts)
			opts = opts or {}
			opts.buffer = bufnr
			vim.keymap.set(mode, l, r, opts)
		end

		-- Navigation
		map("n", "]c", function()
			if vim.wo.diff then
				return "]c"
			end
			vim.schedule(function()
				gs.next_hunk()
			end)
			return "<Ignore>"
		end, { expr = true })

		map("n", "[c", function()
			if vim.wo.diff then
				return "[c"
			end
			vim.schedule(function()
				gs.prev_hunk()
			end)
			return "<Ignore>"
		end, { expr = true })

		-- Actions
		map("n", "<leader>gb", ":Gitsigns blame_line<CR>")
	end,
})

function runLazyGit()
	local Terminal = require("toggleterm.terminal").Terminal
	local run = Terminal:new({
		cmd = "lazygit",
		hidden = true,
		direction = "float",
		close_on_exit = true,
	})

	run:toggle()
end

utils.keymap("n", "<leader>gl", "<cmd>lua runLazyGit()<CR>")
