local M = {
  'nvim-telescope/telescope.nvim',
  dependencies = {
    'nvim-lua/plenary.nvim',
    'natecraddock/telescope-zf-native.nvim',
    -- {
    --   'nvim-telescope/telescope-fzf-native.nvim',
    --   build =
    --   'cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release && cmake --build build --config Release && cmake --install build --prefix build',
    -- },
  },
  cmd = { 'Telescope' },
  keys = {
    { '<Space>b',  [[<Cmd>Telescope buffers<CR>]],              desc = 'Buffers' },
    { '<Space>ff', [[<Cmd>Telescope find_files<CR>]],           desc = 'Search file' },
    { '<Space>fg', [[<Cmd>Telescope live_grep<CR>]],            desc = 'Grep through files' },
    { '<Space>Ta', [[<Cmd>Telescope<CR>]],                      desc = 'Telescope all' },
    { '<Space>Tg', [[<Cmd>Telescope grep_string<CR>]],          desc = 'Telescope grep string' },
    { '<Space>Td', [[<Cmd>Telescope diagnostics<CR>]],          desc = 'Telescope workspace diagnostics' },
    { '<Space>Tj', [[<Cmd>Telescope jumplist<CR>]],             desc = 'Telescope jumplist' },
    { '<Space>Th', [[<Cmd>Telescope help_tags<CR>]],            desc = 'Telescope help' },
    { '<Space>TT', [[<Cmd>Telescope resume<CR>]],               desc = 'Telescope resume' },
    { '<Space>Ts', [[<Cmd>Telescope lsp_document_symbols<CR>]], desc = 'Telescope document symbols' },
  },
}

function M.config()
  local telescope = require('telescope')
  local actions = require('telescope.actions')
  local layout_actions = require('telescope.actions.layout')

  local opts = {
    defaults = {
      layout_config = { prompt_position = 'top' },
      sorting_strategy = 'ascending',
      preview = {
        hide_on_startup = true,
      },
      mappings = {
        i = {
          ['<M-i>'] = layout_actions.toggle_preview,
        },
        n = {
          ['<M-i>'] = layout_actions.toggle_preview,
        },
      },
    },
    pickers = {
      buffers = {
        show_all_buffers = true,
        sort_lastused = false,
        sort_mru = true,
        --      theme = "dropdown",
        mappings = {
          i = {
            ['<C-d>'] = actions.delete_buffer,
          },
          n = {
            ['<C-d>'] = actions.delete_buffer,
          },
        },
      },
      find_files = {
        hidden = true,
      },
      live_grep = {
        mappings = {
          i = {
            ['<C-Space>'] = actions.to_fuzzy_refine,
          },
        },
      },
    },
    extensions = {
      ["zf-native"] = {},
    },
  }

  telescope.setup(opts)

  for ext, _ in pairs(opts.extensions) do
    telescope.load_extension(ext)
  end
end

return M
