local status, telescope = pcall(require, 'telescope')
if (not status) then return end

local actions = require('telescope.actions')

local function telescope_buffer_dir()
  return vim.fn.expand('%:p:h')
end

local fb_actions = require 'telescope'.extensions.file_browser.actions

telescope.setup {
  defaults = {
    file_ignore_patterns = {
      ".git",
      "node_modules",
      "wallpapers",
      "icons",
      "__pycache__/",
    },
    mappings = {
      n = {
        ['q'] = actions.close
      },
    },
    vimgrep_arguments = {
      "rg",
      "-L",
      "--color=never",
      "--no-heading",
      "--with-filename",
      "--line-number",
      "--column",
      "--smart-case",
    },
    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,
    },
    file_sorter = require("telescope.sorters").get_fuzzy_file,
    generic_sorter = require("telescope.sorters").get_generic_fuzzy_sorter,
    path_display = { "truncate" },
    winblend = 0,
    border = {},
    borderchars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" },
    color_devicons = true,
    set_env = { ["COLORTERM"] = "truecolor" }, -- default = nil,
    file_previewer = require("telescope.previewers").vim_buffer_cat.new,
    grep_previewer = require("telescope.previewers").vim_buffer_vimgrep.new,
    qflist_previewer = require("telescope.previewers").vim_buffer_qflist.new,
    -- Developer configurations: Not meant for general override

  },
  extensions = {
    file_browser = {
      theme = 'dropdown',
      -- disables netrw add use telescope-file-browser in its place
      hijack_netrw = true,
      mappings = {
        -- your custom insert mode mappings
        ['i'] = {
          ['<C-w>'] = function() vim.cmd('normal vbd') end,
        },
        ['n'] = {
          ['N'] = fb_actions.create,
          ['h'] = fb_actions.goto_parent_dir,
          ['/'] = function() vim.cmd('startinsert') end,
        }
      }
    }
  }
}

telescope.load_extension('file_browser')

local opts = { noremap = true, silent = true }
vim.keymap.set(
  'n',
  '<C-p>',
  '<cmd>lua require("telescope.builtin").find_files({ no_ignore = false, hidden = true })<cr>',
  opts
)
vim.keymap.set(
  'n',
  '<C-f>',
  '<cmd>lua require("telescope.builtin").live_grep()<cr>',
  opts
)
vim.keymap.set(
  'n',
  '<leader>b',
  '<cmd>lua require("telescope.builtin").buffers()<cr>',
  opts
)
-- vim.keymap.set(
--   'n',
--   ',t',
--   '<cmd>lua require("telescope.builtin").help_tags()<cr>',
--   opts
-- )
-- vim.keymap.set(
--   'n',
--   ';;',
--   '<cmd>lua require("telescope.builtin").resume()<cr>',
--   opts
-- )
vim.keymap.set(
  'n',
  '<leader>d',
  '<cmd>lua require("telescope.builtin").diagnostics()<cr>',
  opts
)
vim.keymap.set(
  'n',
  '<leader>sf',
  '<cmd>lua require("telescope").extensions.file_browser.file_browser({ path = "%:p:h", respect_git_ignore = false, hidden = true, grouped = true, previewer = false, initial_mode = "normal", layout_config = { height = 40 } } ) <cr>'
  ,
  opts
)
