{ pkgs
, lib
, config
, ...
}:
with lib;
with builtins; let
  cfg = config.customNeovim.plugins.telescope;
in
{
  options.customNeovim.plugins.telescope = {
    enable = mkEnableOption "Enable telescope";
  };

  config = mkIf cfg.enable {
    customNeovim.installedPlugins = [
      "plenary"
      "telescope"
      "telescope-fzf-native"
    ];
    customNeovim.configRC = [
      {
        priority = 1;
        content =
          # INIT-LUA
          ''
            require('telescope').setup {
                defaults = {
                    -- Sets up live_grep
                    vimgrep_arguments = {
                        "${pkgs.ripgrep}/bin/rg",
                        "--color=never",
                        "--no-heading",
                        "--with-filename",
                        "--line-number",
                        "--column",
                        "--smart-case",
                        "--hidden",
                        "--no-ignore",
                    },
                    pickers = {
                        find_command = {
                            "${pkgs.fd}/bin/fd",
                        },
                      },
                    mappings = {
                        i = {
                            ['<C-u>'] = false,
                            ['<C-d>'] = false,
                        },
                    },
                },
            }
            pcall(require('telescope').load_extension, 'fzf')
            vim.keymap.set('n', '<leader>?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' })
            vim.keymap.set('n', '<leader><space>', require('telescope.builtin').buffers, { desc = '[ ] Find existing buffers' })
            vim.keymap.set('n', '<leader>/', function()
              require('telescope.builtin').current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
                winblend = 10,
                previewer = false,
              })
            end, { desc = '[/] Fuzzily search in current buffer' })

            vim.keymap.set('n', '<leader>sf', require('telescope.builtin').find_files, { desc = '[S]earch [F]iles' })
            vim.keymap.set('n', '<leader>sh', require('telescope.builtin').help_tags, { desc = '[S]earch [H]elp' })
            vim.keymap.set('n', '<leader>sw', require('telescope.builtin').grep_string, { desc = '[S]earch current [W]ord' })
            vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' })
            vim.keymap.set('n', '<leader>sd', require('telescope.builtin').diagnostics, { desc = '[S]earch [D]iagnostics' })
          '';
      }
    ];
  };
}
