-- Customize Mason plugins

---@type LazySpec
return {
	-- use mason-lspconfig to configure LSP installations
	{
		"williamboman/mason-lspconfig.nvim",
		-- overrides `require("mason-lspconfig").setup(...)`
		opts = {
			----------------------------------------------------------
			-- Note that these also need to be listed in my_lsp.lua! --
			----------------------------------------------------------
			ensure_installed = {
				"angularls",
				"ast_grep",
				"bashls",
				"clangd",
				"cssls",
				"docker_compose_language_service",
				"dockerls",
				"dotls",
				"eslint",
				"gopls",
				"groovyls",
				"helm_ls",
				"html",
				"intelephense",
				"jdtls",
				"jsonls",
				"lua_ls",
				"marksman",
				"nginx_language_server",
				"pbls",
				"puppet",
				"ruff",
				"rust_analyzer",
				"sqlls",
				"terraformls",
				"ts_ls",
				"vimls",
				"yamlls",
				"zls",
			},
		},
	},
	-- use mason-null-ls to configure Formatters/Linter installation for null-ls sources
	{
		"jay-babu/mason-null-ls.nvim",
		-- overrides `require("mason-null-ls").setup(...)`
		opts = {
			ensure_installed = {
				"stylua",
				-- add more arguments for adding more null-ls sources
			},
		},
	},
	{
		"jay-babu/mason-nvim-dap.nvim",
		-- overrides `require("mason-nvim-dap").setup(...)`
		-- See https://github.com/jay-babu/mason-nvim-dap.nvim/blob/main/lua/mason-nvim-dap/mappings/source.lua
		opts = {
			ensure_installed = {
				"python",
				"cppdbg",
				-- add more arguments for adding more debuggers
			},
		},
	},
	{
		"joe-re/sql-language-server",
		config = function()
			require("lspconfig").sqlls.setup({
				-- capabilities = capabilities,
				filetypes = { "sql" },
				root_dir = function(_)
					return vim.loop.cwd()
				end,
			})
		end,
	},
	{
		"mfussenegger/nvim-dap-python", -- debug configuration for py
		-- build = "TSInstall python",
		config = function()
			require("dap-python").setup("/usr/bin/python3") -- system python, requires `pip install debugpy`
		end,
	},
	{
		"rcarriga/nvim-dap-ui",
		dependencies = {
			"nvim-neotest/nvim-nio",
		},
	},
	{
		"jay-babu/mason-nvim-dap.nvim",
		opts = {
			handlers = {
				cppdbg = function(source_name)
					local dap = require("dap")
					dap.adapters.gdb = {
						id = "gdb",
						type = "executable",
						command = "gdb",
						args = { "--quiet", "--interpreter=dap" },
					}

					debug_with_gdb = {
						{
							type = "gdb",
							request = "launch",
							name = "Run executable (GDB)",
							-- This requires special handling of 'run_last', see
							-- https://github.com/mfussenegger/nvim-dap/issues/1025#issuecomment-1695852355
							program = function()
								local path = vim.fn.input({
									prompt = "Path to executable: ",
									default = vim.fn.getcwd() .. "/",
									completion = "file",
								})

								return (path and path ~= "") and path or dap.ABORT
							end,
						},
						{
							type = "gdb",
							request = "launch",
							name = "Run executable with arguments (GDB)",
							-- This requires special handling of 'run_last', see
							-- https://github.com/mfussenegger/nvim-dap/issues/1025#issuecomment-1695852355
							program = function()
								local path = vim.fn.input({
									prompt = "Path to executable: ",
									default = vim.fn.getcwd() .. "/",
									completion = "file",
								})

								return (path and path ~= "") and path or dap.ABORT
							end,
							args = function()
								local args_str = vim.fn.input({
									prompt = "Arguments: ",
								})
								return vim.split(args_str, " +")
							end,
						},
						{
							type = "gdb",
							request = "attach",
							name = "Attach to process (GDB)",
							processId = require("dap.utils").pick_process,
						},
					}
					dap.configurations.c = debug_with_gdb
					dap.configurations.cpp = debug_with_gdb
					dap.configurations.rust = debug_with_gdb
				end,
			},
		},
	},
}