# shellcheck shell=dash disable=SC2034,2120

___x_cmd_cd_pick_set(){
    local X_help_cmd="x help -m cd -i"
    case "$1" in
        pick|fzf)
                    cd:info "Interactive search is set to [tool=$1]"
                    ___x_cmd_cd_util___dict_set dict interactive_tool "$1"
                    ;;
        --help|-h)  help:show:ret:0  ;;
        *)          help:show:ret:64 ;;
    esac
}

___x_cmd_cd_pick___app(){
    # printf "%s\n" "$___X_CMD_PICK_DATA" | x fzf --layout=reverse --height=30% ;;
    ___x_cmd tty update
    local row="$(( LINES / 2 ))"
    if [ "${LINES}" -lt 20 ];     then    row="$LINES"
    elif [ "${row}" -lt 20 ];     then    row=20
    fi
    ___x_cmd pick --width "$COLUMNS" --row "$row" --ctrl-c-clear "$@"
}

___x_cmd_cd_pick___(){
    ___X_CMD_PICK_DATA="$___X_CMD_PICK_DATA" \
    ___X_CMD_SHBIN_CODE='
        xrc cd ;
        "$@"
    ' ___x_cmd antizshbin ___x_cmd_cd_pick___app "$@"
}

___x_cmd_cd_pick_(){
    x_=""
    local ___X_CMD_PICK_DATA=""; ___X_CMD_PICK_DATA="$( "$@" 2>/dev/null )"
    local second
    {
        read -r x_
        read -r second
    } <<A
$___X_CMD_PICK_DATA
A

    [ -n "$x_" ] || { cd:error "Not found directory path"; return 1; }
    [ -z "$second" ] || {
        ___x_cmd_runmode_allow_manual || return $?
        ___x_cmd_compat_syncfs "$___X_CMD_DEVTTY_FP"
        x_="$( ___x_cmd_cd_pick___ <<A
$___X_CMD_PICK_DATA
A
)" || {
            local c="$?"
            cd:info "current directory stay unchanged → $PWD"
            cd:info "FYI, previous directory → $OLDPWD"
            return "$c"
        }
    }

    [ -n "$x_" ] || {
        cd:info "No directory selected"
        return 1
    }

    case "$x_" in
        "mkdir -p "*)       return 0 ;;
    esac

    [ -d "$x_" ] || {
        cd:info "Directory is not existed → $x_"
        if ___x_cmd_cd_history has "$x_"; then
            ___x_cmd_cd_history rm "$x_"
        fi
        return 1
    }
}
