-----------------------------------------------------------------------------//
-- Language servers
-----------------------------------------------------------------------------//
local enhance_attach = require("plugins.lsp.lsp.config").enhance_attach
local servers = {
    yamlls = {
        settings = {
            yaml = {
                customTags = {
                    "!reference sequence", -- necessary for gitlab-ci.yaml files
                },
            },
        },
    },
    sqls = {
        root_dir = require("lspconfig").util.root_pattern(".git"),
        single_file_support = false,
        on_new_config = function(new_config, new_rootdir)
            table.insert(new_config.cmd, "-config")
            table.insert(new_config.cmd, new_rootdir .. "/.config.yaml")
        end,
    },
    lua_ls = {
        settings = {
            Lua = {
                IntelliSense = {
                    traceLocalSet = true,
                },
                codeLens = { enable = false },
                hint = {
                    enable = false,
                },
                format = { enable = false },
                diagnostics = {
                    globals = {
                        "vim",
                        "P",
                        "describe",
                        "it",
                        "before_each",
                        "after_each",
                        "packer_plugins",
                        "pending",
                    },
                },
                completion = { keywordSnippet = "Replace", callSnippet = "Replace" },
                workspace = { checkThirdParty = false },
                telemetry = { enable = false },
            },
        },
    },
    ruff_ls = {},
    semgrep = {},
}

local con = lambda.config.lsp.python.lsp
for _, server in ipairs(con) do
    if server == "pylance" then
        print("NEed to add this")
    else
        if
            vim.fn.filereadable(
                vim.fn.expand("~/.config/nvim/lua/plugins/lsp/lsp/providers/python/" .. server .. ".lua")
            ) == 0
        then
            if server == "basedpyright" then
                servers[server] = {
                    settings = {
                        basedpyright = {
                            typeCheckingMode = "standard",
                        },
                    },
                }
            elseif server == "sourcery" then
                if lambda.config.lsp.python.use_sourcery then
                    servers[server] = require("plugins.lsp.lsp.providers.python." .. server)
                end
            else
                servers[server] = require("plugins.lsp.lsp.providers.python." .. server)
            end
        else
            servers[server] = require("plugins.lsp.lsp.providers.python." .. server)
        end
    end
end
if lambda.config.lsp.latex == "texlab" then
    servers["texlab"] = require("plugins.lsp.lsp.providers.latex.texlab")
else
    require("lspconfig").ltex.setup({})
end

return function(name)
    local config = servers[name]
    if not config then
        return
    end
    local t = type(config)
    if t == "boolean" then
        config = enhance_attach({})
    end
    if t == "function" then
        config = config()
    end
    return enhance_attach(config)
end
