# shellcheck shell=dash disable=SC2016,SC1001


# V="abc" x assert =- 'a.*' 'b.*'
# V="abc" x assert=- 'a.*' 'b.*'
___x_cmd_assert___main_lefteq(){
    local op="${1:?Provide op}";    shift
    case "$op" in
        \=\~)                       ___x_cmd_assert_main_regex      "$@"    ;;
        \=\=\=)                     ___x_cmd_assert_main_eq         "$@"    ;;
        \=\=)                       ___x_cmd_assert_main_within     "$@"    ;;
        \=-)                        ___x_cmd_assert_main_case       "$@"    ;;

        \=-?)   SEP="${op#=-}"      ___x_cmd_assert_main_incaselist "$@"    ;;
        \=\=?)  SEP="${op#==}"      ___x_cmd_assert_main_withinlist "$@"    ;;
        \=\~?)  SEP="${op#=~}"      ___x_cmd_assert_main_reglist    "$@"    ;;
        *)      M="Unknown operator $op" _assert:return 1 ;;
    esac
}

___x_cmd_assert_main_eq(){
    [ "$#" -ge 1 ]                      || M="ERROR: Please provide candidate list."        _assert:return 1
    ___x_cmd_is_eq "$@"                 || M="Not eq [ $* ]"                                _assert:return 1
}

___x_cmd_assert_main_within(){
    [ "$#" -gt 0 ]                      || M="ERROR: Please provide candidate list."        _assert:return 1
    local V="$1"; shift
    ___x_cmd_is_within "$V" "$@"        || M="$V not equal any of [ $* ]"                   _assert:return 1
}

___x_cmd_assert_main_case(){
    [ "$#" -gt 0 ]                      || M="ERROR: Please provide candidate list."        _assert:return 1
    local V="$1"; shift
    ___x_cmd cama "$V" "$@"             || M="$V not match any case [ $* ]"                 _assert:return 1
}

___x_cmd_assert_main_regex(){
    [ "$#" -gt 0 ]                      || M="ERROR: Please provide candidate list."        _assert:return 1
    local V="$1"; shift
    ___x_cmd_is_within_regex "$V" "$@"  || M="ERROR: '$V' NOT match any regex defined"      _assert:return 1
}

# Using V and SEP
___x_cmd_assert_main_withinlist(){
    [ "$#" -gt 0 ]                            || M="ERROR: Please provide candidate list."        _assert:return 1
    local V="$1"; shift
    local e; while ___x_cmd_isplit; do
        [ -n "$e" ] || continue
        ___x_cmd_assert_main_within "$e" "$@" || return
    done
}

___x_cmd_assert_main_incaselist(){
    [ "$#" -gt 0 ]                          || M="ERROR: Please provide case list."             _assert:return 1
    local V="$1"; shift
    local e; while ___x_cmd_isplit; do
        [ -n "$e" ] || continue
        ___x_cmd_assert_main_case "$e" "$@" || return
    done
}

___x_cmd_assert_main_reglist(){
    [ "$#" -gt 0 ]                          || M="ERROR: Please provide candidate list."        _assert:return 1
    local V="$1"; shift
    local e; while ___x_cmd_isplit; do
        [ -n "$e" ] || continue
        ___x_cmd_assert_main_regex "$e" "$@" || return
    done
}

# Consider make it public or rename to ___x_cmd_assert___isplit
___x_cmd_isplit(){
    SEP="${SEP:-
}"
    [ -n "$V" ] || return 1
    e="${V%%"${SEP}"*}"
    if [ "${e}" = "${V}" ]; then     V=
    else                             V="${V#*"${SEP}"}"
    fi
}

