# shellcheck shell=dash

___x_cmd_rg___fzf(){
    ___x_cmd_rg___fzfapp "$@"
}

___x_cmd_rg___fzfapp(){
    [ $# -gt 0 ] || set -- -

    local fp="$1"
    if [ "$fp" = '-' ] && [ -t 0 ];then
        fp='.'
    fi

    local pidofsubshell; pidofsubshell="$(___x_cmd pidofsubshell)"
    local ___X_CMD_RG_FZF_APP_DIR=
    local _rg_fp=

    case "${fp}" in
        -)
            ___X_CMD_RG_FZF_APP_DIR="$___X_CMD_ROOT_TMP/rg.${pidofsubshell}.d"
            ___x_cmd mkdirp "$___X_CMD_RG_FZF_APP_DIR"
            ___x_cmd_cmds cat            > "$___X_CMD_RG_FZF_APP_DIR/source"
            _rg_fp="$___X_CMD_RG_FZF_APP_DIR/source"
            ;;
        *)
            if [ -f "$fp" ]; then
                _rg_fp="$fp"
            elif [ -d "$fp" ];then
                _rg_fp="$fp"
            else
                M="The specified file or directory does not exist --> $fp" N=rg log:ret:1
            fi
            ;;
    esac

    ___x_cmd_compat_syncfs "$_rg_fp" 2>/dev/null

    (
        trap '___x_cmd rmrf "$___X_CMD_RG_FZF_APP_DIR";' EXIT
        local filter=
        local result=
        while true; do
            result="$( ___X_CMD_RUNMODE="$___X_CMD_RUNMODE" ___x_cmdexe rg --fzdata "$_rg_fp" "$filter" )"

            filter="$(printf "%s" "$result" | ___x_cmd_cmds head -n 1)"
            result="$(printf "%s" "$result" | ___x_cmd_cmds tail -n +2)"
            [ -n "$filter" ] || return $?
            local id=
            ___x_cmd ui select id "Next for filter: $filter"    \
                "Copy this filter to pastboard"                 \
                "Copy this part of the data to pastboard"       \
                "Print filter"                                  \
                "Print data"                                    \
                "Modify the filter"                             \
                "Exit"
            case "$id" in
                1)      printf "%s" "$filter" | ___x_cmd pb
                        return 0 ;;
                2)      printf "%s" "$result" | ___x_cmd pb
                        return 0 ;;
                3)      printf "%s\n" "$filter"
                        return 0 ;;
                4)      printf "%s\n" "$result"
                        return 0 ;;
                5)      continue ;;
                *)      return 0 ;;
            esac
        done
    )

}

___x_cmd_rg___fz_data(){
    ___x_cmd_rg___fzfapp___actual "$@"
}

___x_cmd_rg___fzfapp___actual(){
    local _rg_fp="${1:-.}"
    local filter="${2:-""}"

    local ___X_CMD_RG___FZF_RELOAD="reload:sleep 0.05; ___x_cmdexe rg --cmd -H --line-number --color=always {q} ${_rg_fp} || :"
    local ___X_CMD_RG___FZF_PREVIEW='___x_cmdexe rg --fzfapppreview {2} {1}'
    local ___X_CMD_RG___FZF_EDIT='
        if [[ $FZF_SELECT_COUNT -eq 0 ]]; then
            x vim {1} +{2} ;
        else
            x vim +cw -q {+f};
        fi
    '
    local ___X_CMD_RG___FZF_MODEL='
        if [[ ! $FZF_PROMPT =~ ripgrep ]]; then
            printf "rebind(change)+change-prompt(1.ripgrep> )+disable-search+transform-query:printf \{q}";
        else
            printf "unbind(change)+change-prompt(2.fzf> )+enable-search+transform-query:printf \{q}";
        fi
    '
    ___x_cmd fzf --disabled --ansi --multi --prompt '1.ripgrep> '           \
        --reverse --no-info --height='90%'                                  \
        --bind "start:$___X_CMD_RG___FZF_RELOAD"                            \
        --bind "change:$___X_CMD_RG___FZF_RELOAD"                           \
        --bind 'alt-a:select-all,alt-d:deselect-all,ctrl-/:toggle-preview'  \
        --bind "ctrl-o:execute:$___X_CMD_RG___FZF_EDIT"                     \
        --bind "ctrl-t:transform:$___X_CMD_RG___FZF_MODEL"                  \
        --color "hl:-1:underline,hl+:-1:underline:reverse"                  \
        --delimiter ':' --query "$filter" --print-query                     \
        --preview "$___X_CMD_RG___FZF_PREVIEW"                              \
        --preview-window='down,60%,border-bottom,+{2}+3/3,~3'
}


___x_cmd_rg___fzfapppreview(){
    local line="$1"
    local fp="$2"
    if [ -f "$fp" ];then
        ___x_cmd bat --color=always --highlight-line "$line" "$fp"
    else
        printf "\n\n==== Preview ====\n"
        rg:info --ctrl-/ 'Hide/Show Preview Windo.' "Preview content is empty."
    fi
}
