dofile(vim.g.base46_cache .. 'telescope')

return {
  {
    'nvim-telescope/telescope.nvim',
    tag = '0.1.8',
    dependencies = {
      'nvim-lua/plenary.nvim',
      { 'nvim-telescope/telescope-fzf-native.nvim', build = 'make' },
      'nvim-telescope/telescope-ui-select.nvim',
    },
    config = function()
      local actions = require('telescope.actions')
      local action_layout = require('telescope.actions.layout')

      local telescope = require('telescope')
      telescope.setup({
        defaults = {
          prompt_prefix = '  ',
          selection_caret = '  ',
          entry_prefix = '  ',
          initial_mode = 'insert',
          selection_strategy = 'reset',
          sorting_strategy = 'ascending',
          layout_strategy = 'horizontal',
          layout_config = {
            horizontal = {
              prompt_position = 'top',
              preview_width = 0.55,
              results_width = 0.8,
            },
            vertical = {
              mirror = false,
            },
            width = 0.87,
            height = 0.80,
            preview_cutoff = 120,
          },
          borderchars = { '─', '│', '─', '│', '┌', '┐', '┘', '└' },
          -- Default configuration for telescope goes here:
          -- config_key = value,
          mappings = {
            i = {
              ['<esc>'] = actions.close,
              ['<C-[>'] = actions.close,
              ['<C-e>'] = actions.close,

              -- ['<C-n>'] = actions.cycle_history_next,
              -- ['<C-p>'] = actions.cycle_history_prev,
              ['<M-p>'] = action_layout.toggle_preview,
              ['<C-j>'] = actions.move_selection_next,
              ['<C-k>'] = actions.move_selection_previous,

              ['<Tab>'] = actions.toggle_selection + actions.move_selection_worse,
              ['<S-Tab>'] = actions.toggle_selection + actions.move_selection_better,
              ['<C-q>'] = actions.send_to_qflist + actions.open_qflist,
              ['<M-q>'] = actions.send_selected_to_qflist + actions.open_qflist,
              ['<C-l>'] = actions.complete_tag,
              ['<C-_>'] = actions.which_key, -- keys from pressing <C-/>
            },

            n = {
              ['<M-p>'] = action_layout.toggle_preview,

              ['<esc>'] = actions.close,
              ['<C-[>'] = actions.close,

              ['<Tab>'] = actions.toggle_selection + actions.move_selection_worse,
              ['<S-Tab>'] = actions.toggle_selection + actions.move_selection_better,
              ['<C-q>'] = actions.send_to_qflist + actions.open_qflist,
              ['<M-q>'] = actions.send_selected_to_qflist + actions.open_qflist,

              ['j'] = actions.move_selection_next,
              ['k'] = actions.move_selection_previous,
              ['H'] = actions.move_to_top,
              ['M'] = actions.move_to_middle,
              ['L'] = actions.move_to_bottom,

              ['gg'] = actions.move_to_top,
              ['G'] = actions.move_to_bottom,

              ['<C-u>'] = actions.preview_scrolling_up,
              ['<C-d>'] = actions.preview_scrolling_down,

              ['?'] = actions.which_key,
            },
          },
        },
        pickers = {
          find_files = {
            previewer = false,
          },
        },
        extensions = {
          ['ui-select'] = {
            require('telescope.themes').get_dropdown({
              previewer = true,
              initial_mode = 'insert',
              sorting_strategy = 'ascending',
              layout_strategy = 'horizontal',
              layout_config = {
                horizontal = {
                  prompt_position = 'top',
                  width = 0.5,
                  height = 0.4,
                  preview_width = 0.6,
                },
              },
              borderchars = { '─', '│', '─', '│', '┌', '┐', '┘', '└' },
            }),
          },
        },
      })

      local builtin = require('telescope.builtin')
      vim.keymap.set('n', '<leader>ff', builtin.find_files, { desc = 'Telescope find files' })
      vim.keymap.set('n', '<leader>fg', builtin.live_grep, { desc = 'Telescope live grep' })
      vim.keymap.set('n', '<leader>fb', builtin.buffers, { desc = 'Telescope buffers' })
      vim.keymap.set('n', '<leader>fh', builtin.help_tags, { desc = 'Telescope help tags' })
      vim.keymap.set('n', '<leader>fq', builtin.quickfix, { desc = 'Telescope quickfix' })

      telescope.load_extension('fzf')
      telescope.load_extension('ui-select')
    end,
  },
}
