local function map(mode, combo, mapping, opts)
    local options = {noremap = true}
    if opts then 
        options = vim.tbl_extend('force', options, opts)
    end
    vim.api.nvim_set_keymap(mode, combo, mapping, options)
end

vim.g.mapleader = ' '

-- Navigate through panes using lead key and vim direction
map('', '<leader>h', ':wincmd h<CR>')
map('', '<leader>j', ':wincmd j<CR>')
map('', '<leader>k', ':wincmd k<CR>')
map('', '<leader>l', ':wincmd l<CR>')

-- Create splits and tabs
map('', '<leader>v', ':vsp<CR>')
map('', '<leader>b', ':sp<CR>')
map('', '<leader>W', ':tabnew<CR>')

-- Move to tab using lead key plus number
map('', '<leader>1', '1gt')
map('', '<leader>2', '2gt')
map('', '<leader>3', '3gt')
map('', '<leader>4', '4gt')
map('', '<leader>5', '5gt')
map('', '<leader>6', '6gt')
map('', '<leader>7', '7gt')
map('', '<leader>8', '8gt')
map('', '<leader>9', '9gt')
map('', '<leader>0', '10gt')

-- Open undotree panel
map('', '<leader>u', ':UndotreeToggle<CR>')

--- Telescope ---
-- List project files
vim.keymap.set('', '<C-p>', function() require("telescope-config").project_files() end)

-- Grep through files in project 
vim.keymap.set('', '<C-f>', ":Telescope live_grep<CR>")

--- Miscellaneous
-- Toggle line wrap
vim.keymap.set('', '<leader>w', function()
    vim.cmd("set wrap! linebreak! nolist!")
end)

-- Format markdown tables
map('', '<leader>t', ":TableFormat<CR>")

-- Open lazy.nvim menu
map('', '<leader>lh', ":Lazy home<CR>")

-- Allow transparent scrolling through wrapped lines
vim.api.nvim_set_keymap("n", "k", "v:count == 0 ? 'gk' : 'k'", { noremap = true, expr = true, silent = true })
vim.api.nvim_set_keymap("n", "j", "v:count == 0 ? 'gj' : 'j'", { noremap = true, expr = true, silent = true })
