local ignore_patterns = {
  ".git/",
  ".cache/",
  "external/",
  "*.exe",
  "*.dll",
  "*.class",
  "*.jar",
  "*.sln",
  "*.vcxproj",
  "*.png",
  "*.jpg",
  "*.pyc",
}
local fzf = require("fzf-lua")
fzf.setup({
  { "hide" },
  keymap = {
    builtin = {
      ["<A-j>"] = "preview-down",
      ["<A-k>"] = "preview-up",
      ["<A-d>"] = "preview-page-down",
      ["<A-u>"] = "preview-page-up",
      ["<C-r>"] = "preview-reset",
    },
    fzf = {
      ["ctrl-d"] = "half-page-down",
      ["ctrl-u"] = "half-page-up",
    },
  },
  fzf_opts = { ["--layout"] = false },
  fzf_colors = true,
  winopts = {
    fullscreen = true,
    border = { "▁", "▁", "▁", "", "", "", "", "" },
    title_flags = false,
    preview = {
      layout = "horizontal",
      horizontal = "right:70%",
      border = { "▁", "▁", "▁", "", "", "", "▏", "▏" },
    },
  },
  files = {
    fd_opts = Fzf_util.generate_fd_opts("--color=never --hidden --type f", ignore_patterns),
    rg_opts = Fzf_util.generate_rg_opts("--color=never --hidden --files", ignore_patterns),
    winopts = { border = { "▁", "▁", "▁", "▕", "▕", "", "", "" } },
  },
  grep = {
    rg_opts = Fzf_util.generate_rg_opts(
      "--color=always --hidden --no-heading --line-number --column --max-columns=4096",
      ignore_patterns
    ) .. " -e",
    hidden = true,
    winopts = {
      title = false,
      preview = {
        layout = "vertical",
        vertical = "up:60%",
        border = { "▁", "▁", "▁", "", "", "", "", "" },
      },
    },
  },
  git = { files = { winopts = { border = { "▁", "▁", "▁", "▕", "▕", "", "", "" } } } },
  buffers = { winopts = { border = { "▁", "▁", "▁", "▕", "▕", "", "", "" } } },
  keymaps = {
    winopts = {
      title = false,
      preview = {
        layout = "vertical",
        vertical = "up:25%",
        border = { "▁", "▁", "▁", "", "", "", "", "" },
      },
    },
  },
  helptags = { winopts = { border = { "▁", "▁", "▁", "▕", "▕", "", "", "" } } },
})
fzf.register_ui_select()
vim.keymap.set("n", "<LEADER>pf", function() fzf.files() end, { desc = "Find files in project" })
vim.keymap.set("n", "<LEADER>ps", function() fzf.grep() end, { desc = "Search substring in project" })
vim.keymap.set("n", "<LEADER>pw", function() fzf.grep_cword() end, { desc = "Search word under cursor in project" })
vim.keymap.set("n", "<LEADER>pW", function() fzf.grep_cWORD() end, { desc = "Search WORD under cursor in project" })
vim.keymap.set("v", "<LEADER>p", function() fzf.grep_visual() end, { desc = "Search selection in project" })
vim.keymap.set("n", "<LEADER>pl", function() fzf.live_grep() end, { desc = "Live search substring in project" })
vim.keymap.set("n", "<LEADER>pg", function() fzf.git_files() end, { desc = "Find files in git repository" })
vim.keymap.set("n", "<LEADER>pb", function() fzf.buffers() end, { desc = "Find open buffers" })
vim.keymap.set("n", "<LEADER>pk", function() fzf.keymaps() end, { desc = "Show keymaps" })
vim.keymap.set("n", "<LEADER>ph", function() fzf.help_tags() end, { desc = "Show help tags" })
