# shellcheck shell=dash

___x_cmd_cd_history___maxnum(){
    case "$1" in
        "")         local x_=; ___x_cmd_cd_history___maxnum_get_
                    printf "%s\n" "$x_"
                    ;;
        *[!0-9]*)   N=cd M="Max num accepts only one integer arguments. Now get '$*'" log:ret:64 ;;
        *)          ___x_cmd_cd_util___dict_set dict maxnum "$1" ;;
    esac
}

___x_cmd_cd_history___maxnum_get_(){
    ___x_cmd_cd_util___dict_get_ dict maxnum
    [ -n "$x_" ] || x_=500
}


___x_cmd_cd_history___ps1(){
    [ ! -f "$___X_CMD_CD_DATA/history" ] || {
        local l=; local lastpath=;
        while read -r l; do
            lastpath="$l"
        done < "$___X_CMD_CD_DATA/history"
        [ "$lastpath" != "$PWD" ] || return 0
    }
    ___x_cmd_cd_history___top "$PWD"
}


___x_cmd_cd_history___top(){
    local dirpath="$1"
    cd:debug "Top -> $dirpath"
    [ -f "$___X_CMD_CD_DATA/history" ] || ___x_cmd touch "$___X_CMD_CD_DATA/history"
    printf "%s\n" "$dirpath" >> "$___X_CMD_CD_DATA/history"
}


___x_cmd_cd_history___uniq(){
    [ -f "$___X_CMD_CD_DATA/history" ] || return $?
    < "$___X_CMD_CD_DATA/history" ___x_cmd_cd_util___reverse | ___x_cmd_cd_util___uniq | ___x_cmd_cd_util___head "$1" > "$___X_CMD_CD_DATA/history.reverse"
    < "$___X_CMD_CD_DATA/history.reverse" ___x_cmd_cd_util___reverse > "$___X_CMD_CD_DATA/history"
}

___x_cmd_cd_history___match(){
    ___x_cmd_cd_history___ls --rev | ___x_cmd_cmds grep -i "$1"
}
