local t=require("\099\111\114\101\046\117\116\105\108\115\046\102\117\110\099\116\105\111\110\115")M={}VIRTUAL_TEXT_ACTIVE=true M.toggle_virtual_text=function()VIRTUAL_TEXT_ACTIVE=not VIRTUAL_TEXT_ACTIVE t.notify(string.format("\086\105\114\116\117\097\108\116\101\120\116 \037\115",VIRTUAL_TEXT_ACTIVE and"\111\110"or"\111\102\102"),vim.log.levels.INFO,"\108\115\112\047\117\116\105\108\115\046\108\117\097")vim.diagnostic.show(nil,0,nil,{virtual_text=VIRTUAL_TEXT_ACTIVE})end AUTOFORMAT_ACTIVE=true M.toggle_autoformat=function()AUTOFORMAT_ACTIVE=not AUTOFORMAT_ACTIVE t.notify(string.format("\065\117\116\111\102\111\114\109\097\116\116\105\110\103 \037\115",AUTOFORMAT_ACTIVE and"\111\110"or"\111\102\102"),vim.log.levels.INFO,"\108\115\112\046\117\116\105\108\115")end M.get_python_path=function(t)local z=require("\108\115\112\099\111\110\102\105\103\047\117\116\105\108")local R=z.path if vim.env.VIRTUAL_ENV then return R.join(vim.env.VIRTUAL_ENV,"\098\105\110","\112\121\116\104\111\110")end for z,A in ipairs({"\042";"\046\042"})do local V=vim.fn.glob(R.join(t,A,"\112\121\118\101\110\118\046\099\102\103"))if V~=""then return R.join(R.dirname(V),"\098\105\110","\112\121\116\104\111\110")end end return vim.fn.exepath("\112\121\116\104\111\110\051")or vim.fn.exepath("\112\121\116\104\111\110")or"\112\121\116\104\111\110"end return M
local utils = require 'core.utils.functions'
M = {}

-- TODO: refactor
-- must be global or the initial state is not working
VIRTUAL_TEXT_ACTIVE = true
-- toggle displaying virtual text
M.toggle_virtual_text = function()
  VIRTUAL_TEXT_ACTIVE = not VIRTUAL_TEXT_ACTIVE
  utils.notify(string.format('Virtualtext %s', VIRTUAL_TEXT_ACTIVE and 'on' or 'off'), vim.log.levels.INFO, 'lsp/utils.lua')
  vim.diagnostic.show(nil, 0, nil, { virtual_text = VIRTUAL_TEXT_ACTIVE })
end

-- TODO: refactor
-- must be global or the initial state is not working
AUTOFORMAT_ACTIVE = true
-- toggle null-ls's autoformatting
M.toggle_autoformat = function()
  AUTOFORMAT_ACTIVE = not AUTOFORMAT_ACTIVE
  utils.notify(string.format('Autoformatting %s', AUTOFORMAT_ACTIVE and 'on' or 'off'), vim.log.levels.INFO, 'lsp.utils')
end

-- detect python venv
-- https://github.com/neovim/nvim-lspconfig/issues/500#issuecomment-851247107
M.get_python_path = function(workspace)
  local lsp_util = require 'lspconfig/util'
  local path = lsp_util.path
  -- Use activated virtualenv.
  if vim.env.VIRTUAL_ENV then
    return path.join(vim.env.VIRTUAL_ENV, 'bin', 'python')
  end
  -- Find and use virtualenv in workspace directory.
  for _, pattern in ipairs { '*', '.*' } do
    local match = vim.fn.glob(path.join(workspace, pattern, 'pyvenv.cfg'))
    if match ~= '' then
      return path.join(path.dirname(match), 'bin', 'python')
    end
  end
  -- Fallback to system Python.
  return vim.fn.exepath 'python3' or vim.fn.exepath 'python' or 'python'
end

return M
