#!/usr/bin/env zsh
rm -f /tmp/rg-fzf-{r,f} 2>/dev/null
RG_PREFIX="rg --with-filename --column --line-number --color=always --smart-case --field-match-separator $'\u00a0'"

FILES_TO_ADD=""
for item in "$@"; do
  if [ -f "$item" ]; then
    FILES_TO_ADD="$FILES_TO_ADD$item"$'\n'
  fi
done

(fd . -H -t f "$@" 2>/dev/null & printf "%s" "$FILES_TO_ADD") | fzf \
  --ansi \
  --delimiter $'\u00a0' \
  --disabled \
  --prompt '1. ripgrep> ' \
  --header 'CTRL-Y: Switch between ripgrep/fzf' \
  --bind "change:reload:sleep 0.1; $RG_PREFIX {q}  $([ $# -gt 0 ] && printf "%q " "$@") || true" \
--bind 'ctrl-y:transform:[[ ! $FZF_PROMPT =~ ripgrep ]] &&
    echo "rebind(change)+change-prompt(1. ripgrep> )+disable-search+transform-query:echo \{q} > /tmp/rg-fzf-f; cat /tmp/rg-fzf-r" ||
    echo "unbind(change)+change-prompt(2. fzf> )+enable-search+transform-query:echo \{q} > /tmp/rg-fzf-r; cat /tmp/rg-fzf-f"' \
  --color "hl:-1:underline,hl+:-1:underline:reverse" \
  --preview '[ -n "{2}" ] && bat --color=always {1} --style=numbers --highlight-line {2} || bat --color=always {} --style=numbers' \
  --preview-window 'down,60%,+{2}+3/3,~3' \
  --bind 'ctrl-p:change-preview-window(right,60%|hidden|down,60%)' \
  --bind 'focus:transform-preview-label:echo $(basename {1})' \
  --bind 'ctrl-o:execute(kitty -1 sh -c "nvim {1} +{2}" & )' \
  --bind 'ctrl-x:execute-silent(setsid kitty -1 nnn {1} &)' \
  --bind 'ctrl-a:execute-silent(setsid kitty -1 yazi {1} &)' \
  --bind 'ctrl-s:execute-silent(setsid thunar {1} &)' \
  --color 'label:117:bold'

