---@module 'lazy'
---@type LazySpec
return { {
	'akinsho/toggleterm.nvim', -- allows to toggle the terminal rather than open one in a separate window
	event = 'VeryLazy',
	---@type ToggleTermConfig
	---@diagnostic disable-next-line: missing-fields
	opts = { open_mapping = [[<C-\>]] },
	config = function(_, opts)
		local tt = require('toggleterm')
		local term = require('toggleterm.terminal')
		tt.setup(opts)

		local term_lazygit = term.Terminal:new({ cmd = "lazygit", hidden = true, direction = 'float' })

		-- Set mappings to toggle the above
		Map({ 'n' }, '<leader>gg', function() term_lazygit:toggle() end)

		-- Add a keymap for executing makefiles, or launch scripts
		Map({ 'n', 't', 'v' }, '<c-b>', function()
			local cwd = vim.fn.getcwd()
			local cmd = nil
			local launchcfg = cwd .. '/.launch.sh'

			-- If the path is readable, the attached command is executed and no more is checked.
			for _, obj in ipairs({
				{ launchcfg,          launchcfg },
				{ cwd .. '/Makefile', 'make -C ' .. cwd },
				{ cwd .. '/makefile', 'make -C ' .. cwd },
			}) do
				local pat  = obj[1]
				local exec = obj[2]
				if (vim.fn.filereadable(pat) == 1) then
					cmd = '\r' .. exec -- use \r to override text if it's present
					break
				end
			end

			-- If cmd still hasn't been set; edit the file
			if (not cmd) then
				local buf = vim.api.nvim_create_buf(true, false) -- listed, not scratch
				vim.api.nvim_set_current_buf(buf)
				vim.api.nvim_buf_set_name(buf, launchcfg)
				vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, { '#!/usr/bin/env sh', 'exec ' })
				vim.bo.ft = 'sh'
				return
			end

			-- Execute the command in terminal 1
			tt.exec(cmd, 1, nil, nil, nil, nil, false, true)
		end, { desc = 'compile the program using .buildcofig placed in PWD' })
	end
} }
