# shellcheck shell=dash

xrc:mod:lib cd  util back pick relm/_index search history/_index main_origin

! ___x_cmd_is_suitable_advise_env || xrc:mod:lib cd  advise

# jump to a folder contains: jc bin
# jump to a child folder: jc lib
# jump to a father folder: jo opening; c .

# c backward
# c forward = find
# c - = history in environment
# c <filename> = unmatch to search history or rules

# c /u/l/

# TODO:
# c ... code
# c :xb code        # go to the folder and open code in the folder

# TODO:
# c :gh/        ==> gh ==> https://github.com/
# c :ggh/       ==> git@github.com

___x_cmd_cd___main(){
    if [ "$#" -eq 0 ]; then
        ___x_cmd_cd_search___history
        return
    fi

    local p="$1";           shift
    if [ -d "$p" ]; then
        ___x_cmd_cd_origin "$p" "$@";
        return
    fi

    if [ -f "$p" ]; then
        ___x_cmd_cd_origin___file "${p}"
        return
    fi

    # if ___x_cmd_cd_origin___mcd "$p"; then
    #     return 0
    # fi

    case "$p" in
        -h|--help)          M='cd' help:ret:0 ;;
        -r|--relm)          ___x_cmd_cd_relm                    "$@" ;;
        -b)                 ___x_cmd_cd_back                    "$@" ;;

        '~')                ___x_cmd_cd_origin "$HOME"               ;;
        '~'/*)              ___x_cmd_cd_origin "$HOME"/"${p#*/}"     ;;

        # -i)                 ___x_cmd_cd_pick_set                "$@" ;;

        # cd-forward        # Might cause some problem
        -f)                 ___x_cmd_cd_forward                 "$@" ;;
        --history)          ___x_cmd_cd_history                 "$@" ;;

        -)                  ___x_cmd_cd_origin "$OLDPWD"        "$@" ;;
        ""|~*|-L|-P|-q|-s|-*|+*)
                            ___x_cmd_cd_origin "$p"             "$@" ;;

        :|:*)               ___x_cmd_cd_relm___magic "${p}"     "$@" ;;
        *://*)              ___x_cmd_cd_origin "$p"             "$@" ;;

        # cd-back
        ...)                ___x_cmd_cd_back___nv               "$@" ;;
        .../*)              ___x_cmd_cd_back    "${p#*/}"       "$@" ;;

        # ../*)               ___x_cmd_cd_back    "${p#*/}"       "$@" ;;
        # ./*)                ___x_cmd_cd_forward "${p#./}"       "$@" ;;

        # history
        ,*)                 ___x_cmd_cd_search___history_match "${p#,}" "$@" ;;

        # cd with find
        # %*)                 ___x_cmd_cd_search___xcmdroot   "${p#*%}"   "$@" ;;      # consider
        # ,*)                 ___x_cmd_cd_search___home       "${p#*,}"   "$@" ;;      # = is not support in zsh, that's why we use ,

        *)                  ___x_cmd_cd_main___other "$p"       "$@" ;;
    esac
}

___x_cmd_cd_main___other(){
    local p="$1";           shift
    [ -z "$___X_CMD_CD_COMPATIBLE" ] ||  {
        # N=cd M="Invalid path -> $p" log:ret:1
        ___x_cmd_cd_origin___mcd "$p"
        return
    }

    case "$p" in
        /*)
                ___x_cmd_cd_origin___mcd_or_pick    "$p"    /   "${p#/}"    "$@"
                ;;

        ./*)
                ___x_cmd_cd_origin___mcd_or_pick    "$p"    .   "${p#"./"}" "$@"
                ;;
        ../*)
                ___x_cmd_cd_origin___mcd_or_pick    "$p"    ..  "${p#"../"}" "$@"
                ;;
        *)
                ___x_cmd_cd_origin___mcd_or_pick    "$p"    .   "$p"        "$@"
                ;;
    esac
}

___x_cmd_cd_init(){
    ___x_cmd_cd_relm___init
}

___x_cmd_cd_init

___x_cmd_cd_forward(){
    [ "$#" -gt 0 ] || set -- .
    ___x_cmd_cd_search___path "$@"
}
