local O=(require("\099\104\097\105\046\111\112\116\105\111\110\115")).makeopt local function V(O,V)local G,L=pcall(vim.lsp.config,O,V)if not G then vim.notify(("\117\110\097\098\108\101 \116\111 \099\111\110\102\105\103\117\114\101 \108\115\112 \115\101\114\118\101\114 \037\115\058\n\t\037\115"):format(O,L))return end vim.lsp.enable(O)end return{options={common=O({{},"\099\111\109\109\111\110 \115\101\114\118\101\114 \099\111\110\102\105\103",function(O)local V,G=pcall(vim.validate,"\099\111\109\109\111\110 \115\101\114\118\101\114 \099\111\110\102\105\103\117\114\097\116\105\111\110",O,{"\116\097\098\108\101","\102\117\110\099\116\105\111\110"})return V end});servers=O({{},"\108\115\112 \115\101\114\118\101\114\115 \116\111 \099\111\110\102\105\103\117\114\101",function(O)local V,G=pcall(vim.validate,"\115\101\114\118\101\114 \099\111\110\102\105\103\117\114\097\116\105\111\110\115",O,{"\116\097\098\108\101","\102\117\110\099\116\105\111\110"})return V end});on_attach=O({function() end,"\099\097\108\108\098\097\099\107 \097\102\116\101\114 \108\115\112 \097\116\116\097\099\104\101\115","\102\117\110\099\116\105\111\110"})};setup=function(O)vim.api.nvim_create_autocmd("\076\115\112\065\116\116\097\099\104",{group=vim.api.nvim_create_augroup("\076\115\112\065\116\116\097\099\104",{clear=true});callback=O.on_attach})local G=O.common if type(G)=="\102\117\110\099\116\105\111\110"then G=G()end vim.lsp.config("\042",G)local L=O.servers if type(L)=="\102\117\110\099\116\105\111\110"then L=L()end;(vim.iter(pairs(L))):each(V)end}
local makeopt = require("chai.options").makeopt

local function setup_ls(name, cfg)
  ---@diagnostic disable-next-line: param-type-mismatch
  local ok, result = pcall(vim.lsp.config, name, cfg)
  if not ok then
    vim.notify(('unable to configure lsp server %s:\n\t%s'):format(name, result))
    return
  end
  vim.lsp.enable(name)
end

return {
  options = {
    common = makeopt({
      {},
      "common server config",
      function(v)
        local ok, _ = pcall(vim.validate, "common server configuration", v, { "table", "function" })
        return ok
      end,
    }),
    servers = makeopt({
      {},
      "lsp servers to configure",
      function(v)
        local ok, _ = pcall(vim.validate, "server configurations", v, { "table", "function" })
        return ok
      end,
    }),
    on_attach = makeopt({
      function() end,
      "callback after lsp attaches",
      "function",
    }),
  },
  setup = function(props)
    vim.api.nvim_create_autocmd("LspAttach", {
      group = vim.api.nvim_create_augroup("LspAttach", { clear = true }),
      callback = props.on_attach,
    })

    local common = props.common
    if type(common) == "function" then
      common = common()
    end

    vim.lsp.config("*", common)

    local servers = props.servers
    if type(servers) == "function" then
      servers = servers()
    end

    ---@diagnostic disable-next-line: param-type-mismatch
    vim.iter(pairs(servers)):each(setup_ls)
  end,
}
