local M = {}

---@param opts conform.setupOpts
function M.setup(_, opts)
  for _, key in ipairs({ "format_on_save", "format_after_save" }) do
    if opts[key] then
      local msg = "Don't set `opts.%s` for `conform.nvim`.\n**LazyVim** will use the conform formatter automatically"
      LazyVim.warn(msg:format(key))
      ---@diagnostic disable-next-line: no-unknown
      opts[key] = nil
    end
  end
  ---@diagnostic disable-next-line: undefined-field
  if opts.format then
    LazyVim.warn("**conform.nvim** `opts.format` is deprecated. Please use `opts.default_format_opts` instead.")
  end
  require("conform").setup(opts)
end

return {
  {
    "stevearc/conform.nvim",
    dependencies = { "mason.nvim" },
    lazy = true,
    cmd = { "ConformInfo" },
    keys = {
      {
        "<leader>cF",
        function()
          require("conform").format({ formatters = { "injected" }, timeout_ms = 3000 })
        end,
        mode = { "n", "v" },
        desc = "Format Injected Langs",
      },
    },
    init = function()
      -- Install the conform formatter on VeryLazy
      LazyVim.on_very_lazy(function()
        LazyVim.format.register({
          name = "conform.nvim",
          priority = 100,
          primary = true,
          format = function(buf)
            require("conform").format({ bufnr = buf })
            --local opts = LazyVim.opts("conform.nvim")
            --require("conform").format(LazyVim.merge({}, opts.format, { bufnr = buf }))
          end,
          sources = function(buf)
            local ret = require("conform").list_formatters(buf)
            ---@param v conform.FormatterInfo
            return vim.tbl_map(function(v)
              return v.name
            end, ret)
          end,
        })
      end)
    end,
    opts = function()
      local plugin = require("lazy.core.config").plugins["conform.nvim"]
      if plugin.config ~= M.setup then
        LazyVim.error({
          "Don't set `plugin.config` for `conform.nvim`.\n",
          "This will break **LazyVim** formatting.\n",
          "Please refer to the docs at https://www.lazyvim.org/plugins/formatting",
        }, { title = "LazyVim" })
      end
      local prettier = { "prettierd", "prettier", stop_after_first = true }
      ---@type conform.setupOpts
      local opts = {
        default_format_opts = {
          timeout_ms = 3000,
          async = false, -- not recommended to change
          quiet = false, -- not recommended to change
          lsp_format = "fallback", -- not recommended to change
        },
        formatters_by_ft = {
          -- c = { "clang-format" },
          -- cmake = { "cmake-format" },
          -- cpp = { "clang-format" },
          -- css = prettier,
          -- handlebars = prettier,
          -- html = prettier,
          -- javascript = { "eslint" },
          -- jsonc = prettier,
          -- json = prettier,
          -- less = prettier,
          lua = { "stylua" },
          -- php = { "php-cs-fixer" },
          -- scss = prettier,
          sh = { "shfmt" },
          -- typescript = prettier,
          -- vue = prettier,
          -- yaml = prettier,
        },
        -- The options you set here will be merged with the builtin formatters.
        -- You can also define any custom formatters here.
        ---@type table<string, conform.FormatterConfigOverride|fun(bufnr: integer): nil|conform.FormatterConfigOverride>
        formatters = {
          injected = { options = { ignore_errors = true } },
          -- added
          -- astyle = {
          --   command = "astyle",
          --   prepend_args = { "-s2", "-c", "-J", "-n", "-q", "-z2", "-xC80" },
          -- },
          -- ["clang-format"] = {
          --   command = "clang-format",
          --   prepend_args = { "--style=file", "-i" },
          -- },
          -- ["cmake-format"] = {
          --   command = "cmake-format",
          --   prepend_args = { "-i" },
          -- },
          -- nix = {
          --   command = "nixpkgs-fmt",
          -- },
          -- ["php-cs-fixer"] = {
          --   command = "php-cs-fixer",
          --   prepend_args = { "fix", "--rules=@PSR12" },
          -- },
          -- prettier = {
          --   command = "prettier",
          --   prepend_args = { "-w" },
          -- },
          -- prettierd = {
          --   command = "prettierd",
          --   prepend_args = { "-w" },
          -- },
          shfmt = {
            command = "shfmt",
            prepend_args = { "-i", "0", "-sr", "-kp" },
          },
        },
      }
      return opts
    end,
    config = M.setup,
  },
}
