require('fzf-lua').setup({
    previewers = {
        builtin = {
            extensions = {
                ['png'] = { "chafa" },
                ['jpg'] = { "chafa" },
                ['svg'] = { "chafa" },
            }
        }
    },
    files = {
        cwd_prompt_shorten_len = 20
    }
})

-- Register fzf-lua as vim.ui.select handler
require('fzf-lua').register_ui_select({
  prompt = 'Make a selection:',
  format_item = function(item)
    return "-> " .. tostring(item)
  end,
})

vim.keymap.set('n', '<leader>fb', function() require('fzf-lua').buffers() end, {desc='open [b]uffers'})
vim.keymap.set('n', '<leader>ff', function() require('fzf-lua').files() end, {desc='[r]esume last command'})
vim.keymap.set('n', '<leader>fr', function() require('fzf-lua').files() end, {desc='[f]iles'})

-- git related keymaps
vim.keymap.set('n', '<leader>fgb', function() require('fzf-lua').git_branches() end, {desc='Git [b]ranches'})
vim.keymap.set('n', '<leader>fgc', function() require('fzf-lua').git_commits() end, {desc='Git [c]ommits'})
vim.keymap.set('n', '<leader>fgC', function() require('fzf-lua').git_bcommits() end, {desc='Git current buffer [C]ommits'})
vim.keymap.set('n', '<leader>fgf', function() require('fzf-lua').git_files() end, {desc='Git [f]iles'})
vim.keymap.set('n', '<leader>fgs', function() require('fzf-lua').git_stash() end, {desc='Git [s]tash'})
vim.keymap.set('n', '<leader>fgS', function() require('fzf-lua').git_status() end, {desc='Git [S]tash'})

-- grep related keymaps
vim.keymap.set('n', '<leader>fGb', function() require('fzf-lua').grep_curbuf() end, {desc='grep in current [b]uffer'})
vim.keymap.set('n', '<leader>fGc', function() require('fzf-lua').grep_cword() end, {desc='grep word under the [c]ursor'})
vim.keymap.set('n', '<leader>fGg', function() require('fzf-lua').grep() end, {desc='[g]rep'})
vim.keymap.set('n', '<leader>fGl', function() require('fzf-lua').live_grep() end, {desc='[l]ive grep'})
vim.keymap.set('n', '<leader>fGr', function() require('fzf-lua').grep_last() end, {desc='[r]erun last grep'})
