local M = {
  'jose-elias-alvarez/null-ls.nvim',
  enabled = false,
  dependencies = { 'nvim-lua/plenary.nvim' },
}

local ensure = {
  'hadolint',
}

function M.config()
  local nls = require 'null-ls'

  for _, pkg in pairs(ensure) do
    local p = require('mason-registry').get_package(pkg)
    if not p:is_installed() then
      p:install()
    end
  end

  nls.setup({
    debug = false,
    temp_dir = vim.env.XDG_RUNTIME_DIR or '/tmp',
    sources = {
      nls.builtins.formatting.xmllint,
      nls.builtins.formatting.jq,
      -- nls.builtins.formatting.clang_format,
      -- nls.builtins.formatting.sqlfluff.with({
      --   extra_args = { '--dialect', 'mysql' },
      -- }),
      nls.builtins.diagnostics.phpstan.with({
        cwd = function(params)
          -- falls back to root if return value is nil
          return params.root:match('jopixel') and not params.root:match('repo') and 'www';
        end,
      }),
      nls.builtins.formatting.phpcsfixer.with({ ---{{{
        extra_args = {
          '--rules',
          [[{
            "@Symfony": true,
            "fully_qualified_strict_types": false,
            "array_syntax": {
              "syntax": "short"
            },
            "nullable_type_declaration_for_default_null_value": true,
            "binary_operator_spaces": {
              "default": "single_space",
              "operators": {
                ">>=": "align_single_space_minimal",
                "<<=": "align_single_space_minimal",
                "/=": "align_single_space_minimal",
                "*=": "align_single_space_minimal",
                "^=": "align_single_space_minimal",
                "%=": "align_single_space_minimal",
                "=>": "align_single_space_minimal",
                "|=": "align_single_space_minimal",
                ".=": "align_single_space_minimal",
                "<=": "align_single_space_minimal",
                ">=": "align_single_space_minimal",
                "+=": "align_single_space_minimal",
                "**=": "align_single_space_minimal",
                "&=": "align_single_space_minimal",
                "=": "align_single_space_minimal",
                "-=": "align_single_space_minimal",
                "??=": "align_single_space_minimal"
              }
            }
          }]],
        },
      }), -- }}}
      nls.builtins.diagnostics.shellcheck,
      nls.builtins.diagnostics.hadolint,
      nls.builtins.formatting.eslint,
      -- nls.builtins.diagnostics.eslint,
      nls.builtins.code_actions.eslint,
    },
  })
end

return M
