export FZF_SHOW_HELP_OPTS="$(
cat <<-EOF

        FZF Keybinds Shortcut

        ?           Toggle/Hide Preview
        C-space     Change preview layout
        C-e         Open in Editor
        C-v         Open in VsCode
        C-o         Launch Application Chooser
        M-o         Open in Default Appllication
        C-/         Directory: Navigate on broot            
        C-/         File: Open in Pager (bat)

        M-s         Toggle Sort
        C-y         Copy/Yank
        C-M-y       Copy/Yank Working Directory
        C-a         Select all
        C-M-d       Deselect All
        Del         Delete/Remove file

        Alt-?       Help (this page)
        ESC         Quit
EOF
)"

export FZF_DEFAULT_OPTS=" \
--color=bg+:#363a4f,bg:#24273a,spinner:#f4dbd6,hl:#ed8796 \
--color=fg:#cad3f5,header:#ed8796,info:#c6a0f6,pointer:#f4dbd6 \
--color=marker:#f4dbd6,fg+:#cad3f5,prompt:#c6a0f6,hl+:#ed8796"

export FZF_PREVIEW_OPTS="--preview
'([[ {} =~ ('.jpg'|'.jpeg'|'.png'|'.gif'|'.bmp'|'.svg'|'.mp4'|'.mkv')$ ]] && (chafa --center=on {} && exiftool {})) || 
([[ -f {} ]] && (bat --style=header,numbers,changes,plain --color=always --language=sh --line-range :500 {} || cat {})) || 
([[ -d {} ]] && (lsd  -all --long --tree --depth=5 --group-dirs=first -I=.git {} )) || echo {} 3>/dev/null | head -n 500'
"

#'([[ \$(file -bL --mime-type {} 2> /dev/null = image) ]] && (catimg -w 100 {})) || # throwing an stb error cant silence

export FZF_PREVIEW_KEYBIND_OPTS="
--bind '?:toggle-preview'
--bind 'alt-?:preview(printf \"${FZF_SHOW_HELP_OPTS}\")'
--bind 'alt-j:preview-down'
--bind 'alt-k:preview-up'
--bind 'ctrl-d:preview-page-down'
--bind 'ctrl-u:preview-page-up'
--bind 'ctrl-t:preview-top'
--bind 'ctrl-b:preview-bottom'
--bind 'ctrl-l:clear-screen+clear-query+first'
--bind 'ctrl-space:change-preview-window(right,80%,nohidden|down,80%,border-top,nohidden|down,50%,nohidden|up,80%,border-down,nohidden|up,50%,nohidden|left,80%,nohidden|left,50%,nohidden|down:3:nohidden:wrap|up:3,nohidden:wrap|right,50%,nohidden)'
"
export FZF_KEYBIND_SHORTCUTS="
$FZF_PREVIEW_KEYBIND_OPTS
--bind 'alt-o:execute(xdg-open {+})'
--bind 'alt-s:toggle-sort'
--bind 'ctrl-/:execute(
if [[ -d {} ]]; then
  broot {} < /dev/tty > /dev/tty 2>&1
elif [[ {} =~ ('.jpg'|'.jpeg'|'.png'|'.gif'|'.bmp'|'.svg'|'.mp4'|'.mkv')$ ]]; then 
  chafa --center {} | less > /dev/tty
else
  bat --paging=always --style=plain --color=always --language=sh {} > /dev/tty
fi)'
--bind 'ctrl-a:select-all'
--bind 'ctrl-alt-d:deselect-all'
--bind 'ctrl-o:execute(flatpak run re.sonny.Junction {+})'
--bind 'ctrl-y:execute-silent(wl-copy {+})'
--bind 'ctrl-alt-y:execute-silent(readlink -f {+} | wl-copy)'
--bind 'ctrl-e:execute(${EDITOR} {} > /dev/tty)'
--bind 'ctrl-v:execute(code {+})'
--bind 'del:execute(rm -iv {};)+reload($FZF_DEFAULT_COMMAND)+clear-screen'
"
# --bind 'ctrl-/:execute(if [[ -f {} ]]; then bat --paging=always --style=\"header,numbers,changes\" --language=sh {}  < /dev/tty > /dev/tty 2>&1; else broot {} < /dev/tty > /dev/tty 2>&1
# ; fi)'

export FZF_DEFAULT_COMMAND="fd --color=always --hidden --exclude .git"

export FZF_DEFAULT_OPTS="
"$FZF_PREVIEW_OPTS"
"$FZF_KEYBIND_SHORTCUTS"
"$FZF_THEME_CATPPUCCIN_MOCHA"
-i
--ansi
--multi
--height=90%
--info=inline
--no-separator
--layout=reverse
--preview-window=:hidden
"

export FZF_ALT_C_COMMAND="fd --type=d --color=always --hidden --exclude .git"

export FZF_ALT_C_OPTS="
--preview 'lsd --all --long --tree --depth=3 {} | head -500'
--preview-window 'nohidden,<50(down,75%,border-top)'
--bind 'alt-h:reload(fd --type=d --color=always --follow --exclude .git)'
--bind 'alt-c:reload(fd -p ~ --color=always --hidden --type=d --follow)'
"

export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"

export FZF_CTRL_T_OPTS="
--exit-0
--select-1
--info=default
--layout=reverse-list
--preview-window '50%,<50(up,75%,border-down)'
--header 'Alt-D: Directories | Alt-F: Files | Alt-H: Hide Files'
--bind 'alt-d:change-prompt(  Directories > )+reload("$FZF_ALT_C_COMMAND")'
--bind 'alt-f:change-prompt(  Files > )+reload("$FZF_DEFAULT_COMMAND")'
--bind 'alt-h:change-prompt(  Hide Files > )+reload(fd --type=f --color=always --follow)'
--bind 'ctrl-t:change-prompt(Home > )+reload(fd --base-directory ~ --color=always --hidden --exclude .git)'
"

export FZF_CTRL_R_OPTS="
--preview 'echo {+} | bat --color=always --wrap never --language=sh --style=plain'
--preview-window 'down:3:nohidden:wrap'"

# export FZF_TMUX_OPTS='-p80% --color=border:blue'
# FZF_TMUX_CTRL_R_OPT="fzf-tmux -p $FZF_CTRL_R_OPTS"
# --bind 'alt-p:execute($'FZF_TMUX_OPTS'='-p90% --color=border:blue')'

# fzf completion '**' doesn't preview files (idk if it is a bug)
_fzf_compgen_path() {
  fd --color=always --hidden --follow --exclude ".git" . "$1"
}
_fzf_compgen_dir() {
  fd --color=always --type d --hidden --follow --exclude ".git" . "$1"
}
