local keymap = {} --- Add a new |mapping|. --- Examples: ---
---   -- Can add mapping to Lua functions
---   vim.keymap.set('n', 'lhs', function() print("real lua function") end)
---
---   -- Can use it to map multiple modes
---   vim.keymap.set({'n', 'v'}, 'lr', vim.lsp.buf.references, { buffer=true })
---
---   -- Can add mapping for specific buffer
---   vim.keymap.set('n', 'w', "w", { silent = true, buffer = 5 })
---
---   -- Expr mappings
---   vim.keymap.set('i', '', function()
---     return vim.fn.pumvisible() == 1 and "" or ""
---   end, { expr = true })
---   --  mappings
---   vim.keymap.set('n', '[%%', '(MatchitNormalMultiBackward)')
---          
---
--- Note that in a mapping like:
--- 
---    vim.keymap.set('n', 'asdf', require('jkl').my_fun)
--- 
---
--- the require('jkl') gets evaluated during this call in order to access the function. If you want to
--- avoid this cost at startup you can wrap it in a function, for example:
--- 
---    vim.keymap.set('n', 'asdf', function() return require('jkl').my_fun() end)
--- 
---
---@param mode string|table   Same mode short names as |nvim_set_keymap()|.
---                            Can also be list of modes to create mapping on multiple modes.
---@param lhs string          Left-hand side |{lhs}| of the mapping.
---@param rhs string|function  Right-hand side |{rhs}| of the mapping. Can also be a Lua function.
--
---@param opts table A table of |:map-arguments| such as "silent". In addition to the options
---                  listed in |nvim_set_keymap()|, this table also accepts the following keys:
---                  - replace_keycodes: (boolean, default true) When both this and expr is "true",
---                  |nvim_replace_termcodes()| is applied to the result of Lua expr maps.
---                  - remap: (boolean) Make the mapping recursive. This is the
---                  inverse of the "noremap" option from |nvim_set_keymap()|.
---                  Default `true` if `lhs` is a string starting with `
---   vim.keymap.del('n', 'lhs')
---
---   vim.keymap.del({'n', 'i', 'v'}, 'w', { buffer = 5 })
---  
---@param opts table A table of optional arguments:
---                  - buffer: (number or boolean) Remove a mapping from the given buffer.
---                  When "true" or 0, use the current buffer.
---@see |vim.keymap.set()|
---
function keymap.del(modes, lhs, opts)
  vim.validate {
    mode = {modes, {'s', 't'}},
    lhs = {lhs, 's'},
    opts = {opts, 't', true}
  }
  opts = opts or {}
  modes = type(modes) == 'string' and {modes} or modes
  local buffer = false
  if opts.buffer ~= nil then
    buffer = opts.buffer == true and 0 or opts.buffer
    opts.buffer = nil
  end
  if buffer == false then
    for _, mode in ipairs(modes) do
      vim.api.nvim_del_keymap(mode, lhs)
    end
  else
    for _, mode in ipairs(modes) do
      vim.api.nvim_buf_del_keymap(buffer, mode, lhs)
    end
  end
end
return keymap