local g={"\110\118\105\109-\116\101\108\101\115\099\111\112\101\047\116\101\108\101\115\099\111\112\101\046\110\118\105\109",cmd="\084\101\108\101\115\099\111\112\101",dependencies={"\110\118\105\109-\116\101\108\101\115\099\111\112\101\047\116\101\108\101\115\099\111\112\101-\115\121\109\098\111\108\115\046\110\118\105\109";"\110\118\105\109-\116\101\108\101\115\099\111\112\101\047\116\101\108\101\115\099\111\112\101-\102\105\108\101-\098\114\111\119\115\101\114\046\110\118\105\109";"\112\116\101\116\104\110\103\047\116\101\108\101\115\099\111\112\101-\109\097\107\101\102\105\108\101";{"\110\118\105\109-\116\101\108\101\115\099\111\112\101\047\116\101\108\101\115\099\111\112\101-\102\122\102-\110\097\116\105\118\101\046\110\118\105\109";build="\109\097\107\101"}};keys={{"\060\108\101\097\100\101\114\062\115\099";"\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \099\111\109\109\097\110\100\115\060\099\114\062",desc="\067\111\109\109\097\110\100\115"},{"\060\108\101\097\100\101\114\062\115\116";"\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \108\105\118\101\095\103\114\101\112\060\099\114\062";desc="\083\116\114\105\110\103\115"};{"\060\108\101\097\100\101\114\062\115\063","\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \104\101\108\112\095\116\097\103\115\060\099\114\062";desc="\072\101\108\112"},{"\060\108\101\097\100\101\114\062\115\107","\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \107\101\121\109\097\112\115\060\099\114\062",desc="\075\101\121\109\097\112\115"},{"\060\108\101\097\100\101\114\062\115\079";"\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \118\105\109\095\111\112\116\105\111\110\115\060\099\114\062",desc="\086\105\109 \079\112\116\105\111\110\115"},{"\060\108\101\097\100\101\114\062\115\082","\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \114\101\103\105\115\116\101\114\115\060\099\114\062";desc="\082\101\103\105\115\116\101\114\115"};{"\060\108\101\097\100\101\114\062\115\115","\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \103\114\101\112\095\115\116\114\105\110\103\060\099\114\062",desc="\084\101\120\116 \117\110\100\101\114 \099\117\114\115\111\114"};{"\060\108\101\097\100\101\114\062\115\083";"\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \115\121\109\098\111\108\115\060\099\114\062",desc="\069\109\111\106\105"},{"\060\108\101\097\100\101\114\062\115\058";"\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \115\101\097\114\099\104\095\104\105\115\116\111\114\121\060\099\114\062",desc="\083\101\097\114\099\104 \072\105\115\116\111\114\121"},{"\060\108\101\097\100\101\114\062\115\059";"\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \099\111\109\109\097\110\100\095\104\105\115\116\111\114\121\060\099\114\062",desc="\067\111\109\109\097\110\100 \104\105\115\116\111\114\121"};{"\060\108\101\097\100\101\114\062\115\102","\060\099\109\100\062\108\117\097 \114\101\113\117\105\114\101\'\116\101\108\101\115\099\111\112\101\046\098\117\105\108\116\105\110\'\046\103\114\101\112\095\115\116\114\105\110\103\123 \115\104\111\114\116\101\110\095\112\097\116\104 \061 \116\114\117\101\044 \119\111\114\100\095\109\097\116\099\104 \061 \'-\119\'\044 \111\110\108\121\095\115\111\114\116\095\116\101\120\116 \061 \116\114\117\101\044 \115\101\097\114\099\104 \061 \'\' \125\060\099\114\062";desc="\070\117\122\122\121 \115\101\097\114\099\104"},{"\060\108\101\097\100\101\114\062\103\104","\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \103\105\116\095\098\114\097\110\099\104\101\115\060\099\114\062";desc="\066\114\097\110\099\104\101\115"},{"\060\108\101\097\100\101\114\062\103\103";"\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \103\105\116\095\115\116\097\116\117\115\060\099\114\062";desc="\083\116\097\116\117\115"};{"\060\108\101\097\100\101\114\062\103\109","\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \103\105\116\095\099\111\109\109\105\116\115\060\099\114\062",desc="\067\111\109\109\105\116\115"},{"\060\108\101\097\100\101\114\062\102\098";"\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \102\105\108\101\095\098\114\111\119\115\101\114 \103\114\111\117\112\101\100\061\116\114\117\101\060\099\114\062",desc="\070\105\108\101\098\114\111\119\115\101\114"};{"\060\108\101\097\100\101\114\062\102\102","\060\099\109\100\062"..((require("\099\111\114\101\046\117\116\105\108\115\046\102\117\110\099\116\105\111\110\115")).project_files().."\060\099\114\062");desc="\079\112\101\110 \102\105\108\101"};{"\060\108\101\097\100\101\114\062\102\070";"\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \102\105\110\100\095\102\105\108\101\115\060\099\114\062";desc="\079\112\101\110 \102\105\108\101 \040\105\103\110\111\114\101 \103\105\116\041"},{"\060\108\101\097\100\101\114\062\102\114","\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \111\108\100\102\105\108\101\115\060\099\114\062",desc="\082\101\099\101\110\116 \102\105\108\101\115"};{"\060\108\101\097\100\101\114\062\109\116","\060\099\109\100\062\084\101\108\101\115\099\111\112\101\060\099\114\062",desc="\084\101\108\101\115\099\111\112\101"};{"\060\108\101\097\100\101\114\062\098\098","\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \098\117\102\102\101\114\115\060\099\114\062";desc="\066\117\102\102\101\114\108\105\115\116"};{"\060\067-\102\062";"\060\099\109\100\062\084\101\108\101\115\099\111\112\101 \099\117\114\114\101\110\116\095\098\117\102\102\101\114\095\102\117\122\122\121\095\102\105\110\100\060\099\114\062",desc="\083\101\097\114\099\104 \105\110 \098\117\102\102\101\114"}},config=function()local g=require("\099\111\114\101\046\115\101\116\116\105\110\103\115")local z=require("\116\101\108\101\115\099\111\112\101")local P=require("\116\101\108\101\115\099\111\112\101\046\099\111\110\102\105\103")local O=require("\116\101\108\101\115\099\111\112\101\046\097\099\116\105\111\110\115")local H=require("\116\101\108\101\115\099\111\112\101\046\097\099\116\105\111\110\115\046\108\097\121\111\117\116")local S=(require("\116\101\108\101\115\099\111\112\101")).extensions.file_browser.actions local m=require("\099\111\114\101\046\117\116\105\108\115\046\105\099\111\110\115")local c={unpack(P.values.vimgrep_arguments)}if g.telescope_grep_hidden then table.insert(c,"--\104\105\100\100\101\110")end table.insert(c,"--\116\114\105\109")local M={fuzzy=true;override_generic_sorter=true,override_file_sorter=true;case_mode="\115\109\097\114\116\095\099\097\115\101"}z.setup({extensions={fzf=M,file_browser={theme="\105\118\121",hijack_netrw=false;hidden=true,mappings={i={["\060\099-\110\062"]=S.create;["\060\099-\114\062"]=S.rename,["\060\099-\104\062"]=S.toggle_hidden;["\060\099-\120\062"]=S.remove;["\060\099-\112\062"]=S.move,["\060\099-\121\062"]=S.copy;["\060\099-\097\062"]=S.select_all}}}};pickers={find_files={hidden=false};oldfiles={cwd_only=true};buffers={ignore_current_buffer=true,sort_lastused=true},live_grep={sorter=z.extensions.fzf.native_fzf_sorter(M),only_sort_text=true;mappings={i={["\060\099-\102\062"]=(require("\116\101\108\101\115\099\111\112\101\046\097\099\116\105\111\110\115")).to_fuzzy_refine}}}},defaults={file_ignore_patterns=g.telescope_file_ignore_patterns,vimgrep_arguments=c;mappings={i={["\060\101\115\099\062"]=O.close,["\060\067-\106\062"]=O.move_selection_next,["\060\080\097\103\101\085\112\062"]=O.results_scrolling_up,["\060\080\097\103\101\068\111\119\110\062"]=O.results_scrolling_down;["\060\067-\117\062"]=O.preview_scrolling_up;["\060\067-\100\062"]=O.preview_scrolling_down;["\060\067-\107\062"]=O.move_selection_previous,["\060\067-\113\062"]=O.send_selected_to_qflist;["\060\067-\108\062"]=O.send_to_qflist,["\060\084\097\098\062"]=O.toggle_selection+O.move_selection_worse;["\060\083-\084\097\098\062"]=O.toggle_selection+O.move_selection_better,["\060\099\114\062"]=O.select_default,["\060\099-\118\062"]=O.select_vertical,["\060\099-\115\062"]=O.select_horizontal,["\060\099-\116\062"]=O.select_tab;["\060\099-\112\062"]=H.toggle_preview,["\060\099-\111\062"]=H.toggle_mirror;["\060\099-\104\062"]=O.which_key,["\060\099-\120\062"]=O.delete_buffer}},prompt_prefix=table.concat({m.arrows.ChevronRight," "});selection_caret=m.arrows.CurvedArrowRight,entry_prefix="  ",multi_icon=m.arrows.Diamond,initial_mode="\105\110\115\101\114\116",scroll_strategy="\099\121\099\108\101",selection_strategy="\114\101\115\101\116",sorting_strategy="\100\101\115\099\101\110\100\105\110\103",layout_strategy="\118\101\114\116\105\099\097\108";layout_config={width=.95;height=.85,prompt_position="\116\111\112",horizontal={preview_width=function(g,z,P)if z>200 then return math.floor(z*.4)else return math.floor(z*.6)end end},vertical={width=.9;height=.95;preview_height=.5},flex={horizontal={preview_width=.9}}};winblend=0;border={},borderchars={"\226\148\128","\226\148\130";"\226\148\128";"\226\148\130","\226\149\173";"\226\149\174","\226\149\175","\226\149\176"};color_devicons=true,use_less=true;set_env={COLORTERM="\116\114\117\101\099\111\108\111\114"}}})if g.enable_noice then z.load_extension("\110\111\105\099\101")end z.load_extension("\102\122\102")end}return g
local M = {
  'nvim-telescope/telescope.nvim',
  cmd = 'Telescope',
  dependencies = {
    -- 'jvgrootveld/telescope-zoxide',
    -- 'crispgm/telescope-heading.nvim',
    'nvim-telescope/telescope-symbols.nvim',
    'nvim-telescope/telescope-file-browser.nvim',
    -- 'nvim-telescope/telescope-ui-select.nvim',
    -- 'nvim-telescope/telescope-project.nvim',
    'ptethng/telescope-makefile',
    { 'nvim-telescope/telescope-fzf-native.nvim', build = 'make' },
    -- 'nvim-lua/plenary.nvim',
  },
  keys = {
    -- Search stuff
    { '<leader>sc', '<cmd>Telescope commands<cr>', desc = 'Commands' },
    { '<leader>st', '<cmd>Telescope live_grep<cr>', desc = 'Strings' },
    { '<leader>s?', '<cmd>Telescope help_tags<cr>', desc = 'Help' },
    -- { '<leader>sh', '<cmd>Telescope heading<cr>', desc = 'Headings' },
    { '<leader>sk', '<cmd>Telescope keymaps<cr>', desc = 'Keymaps' },
    { '<leader>sO', '<cmd>Telescope vim_options<cr>', desc = 'Vim Options' },
    -- { '<leader>sp', '<cmd>Telescope project<cr>', desc = 'Projects' },
    { '<leader>sR', '<cmd>Telescope registers<cr>', desc = 'Registers' },
    { '<leader>ss', '<cmd>Telescope grep_string<cr>', desc = 'Text under cursor' },
    { '<leader>sS', '<cmd>Telescope symbols<cr>', desc = 'Emoji' },
    { '<leader>s:', '<cmd>Telescope search_history<cr>', desc = 'Search History' },
    { '<leader>s;', '<cmd>Telescope command_history<cr>', desc = 'Command history' },
    {
      '<leader>sf',
      "<cmd>lua require'telescope.builtin'.grep_string{ shorten_path = true, word_match = '-w', only_sort_text = true, search = '' }<cr>",
      desc = 'Fuzzy search',
    },
    -- Git
    { '<leader>gh', '<cmd>Telescope git_branches<cr>', desc = 'Branches' },
    { '<leader>gg', '<cmd>Telescope git_status<cr>', desc = 'Status' },
    { '<leader>gm', '<cmd>Telescope git_commits<cr>', desc = 'Commits' },
    -- files
    { '<leader>fb', '<cmd>Telescope file_browser grouped=true<cr>', desc = 'Filebrowser' },
    -- { '<leader>fz', '<cmd>Telescope zoxide list<cr>', desc = 'Zoxide' },
    { '<leader>ff', '<cmd>' .. require('core.utils.functions').project_files() .. '<cr>', desc = 'Open file' },
    { '<leader>fF', '<cmd>Telescope find_files<cr>', desc = 'Open file (ignore git)' },
    { '<leader>fr', '<cmd>Telescope oldfiles<cr>', desc = 'Recent files' },
    -- misc
    -- { '<leader>mm', '<cmd>Telescope make<cr>', desc = 'Run make' },
    { '<leader>mt', '<cmd>Telescope<cr>', desc = 'Telescope' },
    -- Other
    { '<leader>bb', '<cmd>Telescope buffers<cr>', desc = 'Bufferlist' },
    { '<C-f>', '<cmd>Telescope current_buffer_fuzzy_find<cr>', desc = 'Search in buffer' },
  },
  config = function()
    local settings = require 'core.settings'
    local telescope = require 'telescope'
    local telescopeConfig = require 'telescope.config'
    local actions = require 'telescope.actions'
    local action_layout = require 'telescope.actions.layout'
    local fb_actions = require('telescope').extensions.file_browser.actions
    local icons = require 'core.utils.icons'

    local vimgrep_arguments = { unpack(telescopeConfig.values.vimgrep_arguments) }
    if settings.telescope_grep_hidden then
      table.insert(vimgrep_arguments, '--hidden')
    end
    -- trim the indentation at the beginning of presented line
    table.insert(vimgrep_arguments, '--trim')

    local fzf_opts = {
      fuzzy = true,
      override_generic_sorter = true,
      override_file_sorter = true,
      case_mode = 'smart_case',
    }

    telescope.setup {
      extensions = {
        fzf = fzf_opts,
        -- ['ui-select'] = {
        --   require('telescope.themes').get_dropdown {},
        -- },
        file_browser = {
          theme = 'ivy',
          hijack_netrw = false,
          hidden = true,
          mappings = {
            i = {
              ['<c-n>'] = fb_actions.create,
              ['<c-r>'] = fb_actions.rename,
              -- ["<c-h>"] = actions.which_key,
              ['<c-h>'] = fb_actions.toggle_hidden,
              ['<c-x>'] = fb_actions.remove,
              ['<c-p>'] = fb_actions.move,
              ['<c-y>'] = fb_actions.copy,
              ['<c-a>'] = fb_actions.select_all,
            },
          },
        },
      },
      pickers = {
        find_files = {
          hidden = false,
        },
        oldfiles = {
          cwd_only = true,
        },
        buffers = {
          ignore_current_buffer = true,
          sort_lastused = true,
        },
        live_grep = {
          sorter = telescope.extensions.fzf.native_fzf_sorter(fzf_opts),
          only_sort_text = true, -- grep for content and not file name/path
          mappings = {
            i = { ['<c-f>'] = require('telescope.actions').to_fuzzy_refine },
          },
        },
      },
      defaults = {
        file_ignore_patterns = settings.telescope_file_ignore_patterns,
        vimgrep_arguments = vimgrep_arguments,
        mappings = {
          i = {
            -- Close on first esc instead of going to normal mode
            -- https://github.com/nvim-telescope/telescope.nvim/blob/master/lua/telescope/mappings.lua
            ['<esc>'] = actions.close,
            ['<C-j>'] = actions.move_selection_next,
            ['<PageUp>'] = actions.results_scrolling_up,
            ['<PageDown>'] = actions.results_scrolling_down,
            ['<C-u>'] = actions.preview_scrolling_up,
            ['<C-d>'] = actions.preview_scrolling_down,
            ['<C-k>'] = actions.move_selection_previous,
            ['<C-q>'] = actions.send_selected_to_qflist,
            ['<C-l>'] = actions.send_to_qflist,
            ['<Tab>'] = actions.toggle_selection + actions.move_selection_worse,
            ['<S-Tab>'] = actions.toggle_selection + actions.move_selection_better,
            ['<cr>'] = actions.select_default,
            ['<c-v>'] = actions.select_vertical,
            ['<c-s>'] = actions.select_horizontal,
            ['<c-t>'] = actions.select_tab,
            ['<c-p>'] = action_layout.toggle_preview,
            ['<c-o>'] = action_layout.toggle_mirror,
            ['<c-h>'] = actions.which_key,
            ['<c-x>'] = actions.delete_buffer,
          },
        },
        prompt_prefix = table.concat { icons.arrows.ChevronRight, ' ' },
        selection_caret = icons.arrows.CurvedArrowRight,
        entry_prefix = '  ',
        multi_icon = icons.arrows.Diamond,
        initial_mode = 'insert',
        scroll_strategy = 'cycle',
        selection_strategy = 'reset',
        sorting_strategy = 'descending',
        layout_strategy = 'vertical',
        layout_config = {
          width = 0.95,
          height = 0.85,
          -- preview_cutoff = 120,
          prompt_position = 'top',
          horizontal = {
            preview_width = function(_, cols, _)
              if cols > 200 then
                return math.floor(cols * 0.4)
              else
                return math.floor(cols * 0.6)
              end
            end,
          },
          vertical = { width = 0.9, height = 0.95, preview_height = 0.5 },
          flex = { horizontal = { preview_width = 0.9 } },
        },
        winblend = 0,
        border = {},
        borderchars = { '─', '│', '─', '│', '╭', '╮', '╯', '╰' },
        color_devicons = true,
        use_less = true,
        set_env = { ['COLORTERM'] = 'truecolor' }, -- default = nil,
      },
    }

    -- Load all used extensions
    -- telescope.load_extension 'project'
    -- telescope.load_extension 'zoxide' -- Remember accessed directories
    -- telescope.load_extension 'heading' -- List document headings
    -- telescope.load_extension 'ui-select'
    -- telescope.load_extension 'make' -- Run Makefile target scripts within neovim with toggleterm.
    if settings.enable_noice then
      telescope.load_extension 'noice'
    end
    telescope.load_extension 'fzf'
    -- telescope.load_extension 'harpoon'
  end,
}

return M
