local ensure = {
  'hadolint',
  'php-cs-fixer',
  'ansible-lint',
  'shfmt'
}

return {
  'stevearc/conform.nvim',
  cmd = { 'ConformInfo' },
  keys = {
    {
      -- Customize or remove this keymap to your liking
      '<leader>F',
      function()
        require('conform').format({ async = true, lsp_fallback = true })
      end,
      mode = '',
      desc = 'Format buffer',
    },
  },
  -- Everything in opts will be passed to setup()
  opts = {
    -- Define your formatters
    formatters_by_ft = {
      -- lua = { "stylua" },
      -- javascript = { { "prettierd", "prettier" } },
      dockerfile = { 'hadolint' },
      php = { 'php_cs_fixer' },
      json = { 'jq' },
      just = { 'just' },
      sh = {'shfmt' }
    },

    -- Set up format-on-save
    -- format_on_save = { timeout_ms = 500, lsp_fallback = true },
    -- Customize formatters
    formatters = {
      php_cs_fixer = { ---{{{
        args = {
          'fix',
          '--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"
              }
            }
          }]],
          '$FILENAME',
        },
      }, -- }}}
    },
  },
  config = function(_, opts)
    for _, pkg in pairs(ensure) do
      local p = require('mason-registry').get_package(pkg)
      if not p:is_installed() then
        p:install()
      end
    end
    require('conform').setup(opts)
  end,
  init = function()
    -- If you want the formatexpr, here is the place to set it
    vim.o.formatexpr = "v:lua.require'conform'.formatexpr()"
  end,
}
