-- autopairs.lua: Auto-inserts matching pairs for brackets, quotes, and delimiters during coding.

return {
  {
    "windwp/nvim-autopairs",
    event = "InsertEnter",
    dependencies = {
      "hrsh7th/nvim-cmp",
    },
    config = function()
      local autopairs = require("nvim-autopairs")
      local cmp_autopairs = require("nvim-autopairs.completion.cmp")
      local cmp = require("cmp")

      autopairs.setup({
        check_ts = true, -- Enable treesitter
        ts_config = {
          lua = { "string" }, -- Don't add pairs in lua string treesitter nodes
          javascript = { "template_string" }, -- Don't add pairs in JS template strings
          java = false, -- Don't check treesitter on java
        },
        disable_filetype = { "TelescopePrompt", "vim" },
        disable_in_macro = true, -- Disable when recording or executing a macro
        disable_in_visualblock = false, -- Disable when in visual block mode
        disable_in_replace_mode = true,
        ignored_next_char = [=[[%w%%%'%[%"%.%`%$]]=],
        enable_moveright = true,
        enable_afterquote = true, -- Add bracket pairs after quote
        enable_check_bracket_line = true, -- Check bracket in same line
        enable_bracket_in_quote = true,
        enable_abbr = false, -- Trigger abbreviation
        break_undo = true, -- Switch for basic rule break undo sequence
        check_comma = true,
        map_cr = true,
        map_bs = true, -- Map the <BS> key
        map_c_h = false, -- Map the <C-h> key to delete a pair
        map_c_w = false, -- Map <c-w> to delete a pair if possible
      })

      -- Integration with nvim-cmp
      cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done())

      -- Treesitter rules
      local ts_conds = require("nvim-autopairs.ts-conds")

      -- Press % => %% only while inside a comment or string
      autopairs.add_rules({
        require("nvim-autopairs.rule")("%", "%", "lua"):with_pair(ts_conds.is_ts_node({ "string", "comment" })),
        require("nvim-autopairs.rule")("$", "$", "lua"):with_pair(ts_conds.is_not_ts_node({ "function" })),
      })

      -- Add spaces between parentheses
      local Rule = require("nvim-autopairs.rule")
      local cond = require("nvim-autopairs.conds")

      autopairs.add_rules({
        Rule(" ", " ")
          :with_pair(function(opts)
            local pair = opts.line:sub(opts.col - 1, opts.col)
            return vim.tbl_contains({ "()", "[]", "{}" }, pair)
          end)
          :with_move(cond.none())
          :with_cr(cond.none())
          :with_del(function(opts)
            local col = vim.api.nvim_win_get_cursor(0)[2]
            local context = opts.line:sub(col - 1, col + 2)
            return vim.tbl_contains({ "(  )", "[  ]", "{  }" }, context)
          end),
        Rule("", " )")
          :with_pair(cond.none())
          :with_move(function(opts)
            return opts.char == ")"
          end)
          :with_cr(cond.none())
          :with_del(cond.none())
          :use_key(")"),
        Rule("", " }")
          :with_pair(cond.none())
          :with_move(function(opts)
            return opts.char == "}"
          end)
          :with_cr(cond.none())
          :with_del(cond.none())
          :use_key("}"),
        Rule("", " ]")
          :with_pair(cond.none())
          :with_move(function(opts)
            return opts.char == "]"
          end)
          :with_cr(cond.none())
          :with_del(cond.none())
          :use_key("]"),
      })
    end,
  },
}
