# shellcheck shell=dash
# TODO: make it look good
___x_cmd_cd_history___ls(){
    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_chatty; then
        ___x_cmd_cd_history___ls_app "$@"
    else
        ___x_cmd_cd_history___ls_raw "$@"
    fi
}

___x_cmd_cd_history___ls_app(){
    local x_; x_="$( ___x_cmd_cd_pick___ ___x_cmd_cd_history___ls_raw "$@" )" || return $?
    [ -n "$x_" ] || return $?

    local id;
    ___x_cmd ui select id "Next for folder -> $x_" \
        "cd into folder"                    \
        "open navigator folder"             \
        "echo folder path"                  \
        "remove this folder from history"   \
        "Abort" || return $?

    case "$id" in
        1)  ___x_cmd_cd_origin "$x_"    ;;
        2)  ___x_cmd ls "$x_"           ;;
        3)  printf "%s\n" "$x_"         ;;
        4)  ___x_cmd_cd_history rm "$x_" ;;
        *)  return
    esac
}

___x_cmd_cd_history___ls_raw(){
    ___x_cmd_compat_syncfs "$___X_CMD_CD_DATA/history"
    ___x_cmd_compat_syncfs "$___X_CMD_CD_DATA/history.reverse"
    ___x_cmd_cd_history___uniq || return 0
    [ "$1" = --rev ] || {
        ___x_cmd rat "$___X_CMD_CD_DATA/history"
        return
    }
    ___x_cmd rat "$___X_CMD_CD_DATA/history.reverse"
}

