# Author:       Li Junhao       l@x-cmd.com             # xrc
# shellcheck    shell=sh        disable=SC2039,SC3043

# jo q a=.bcd b=.cde c=.abc

# TODO: Add merge
# TODO: Fix color and fmt definition

xrc:mod:lib ja      share/index json2csv

___x_cmd_jo(){
    case "${1}" in
        data)               shift; ___x_cmd_ja_data             "$@" ;;

        n|normalize)        shift; ___x_cmd_ja_normalize        "$@" ;;
        t|tokenize)         shift; ___x_cmd_ja_tokenize         "$@" ;;
        f|fmt)              shift; ___x_cmd_ja_fmt              "$@" ;;
        color)              shift; ___x_cmd_ja_color            "$@" ;;
        2y|toyml)           shift; ___x_cmd_ja_toyml            "$@" ;;
        2c|tocsv)           shift; ___x_cmd_ja_json_tocsv       "$@" ;;
        list2line)          shift; ___x_cmd_jo_list2line        "$@" ;;

        "")                 if [ -t 0 ]; then   ___x_cmd help -m jo >&2; return 64
                            else                ___x_cmd_ja___normalize_stream
                            fi
                            ;;
        \{*|\[*)            ___x_cmd_ja___normalize_argstring   "$@" ;;

        # v|valid)            ;;
        # u|uniq)             ;;
        l|list)             shift; ___x_cmd_jo_list "$@" | ___x_cmd_ja___normalize_stream ;;
        d|dict)             shift; ___x_cmd_jo_dict "$@" | ___x_cmd_ja___normalize_stream ;;

        # q)                shift; ___x_cmd_ja___warp_color_iftty
        ed)                 shift; ___x_cmd_jo_ed               "$@" ;;

        # qu)                 shift; ___x_cmd_jo_quote_ "$@" ;;
        # uq)                 shift; ___x_cmd_jo_unquote_ "$@" ;;

        q0|q1|q)
                            local ___X_CMD_JO_QUERY_JSTR="$1";
                            shift; ___x_cmd_jo_query            "$@" ;;
        env)                shift; ___x_cmd_jo_env              "$@" ;;
        nav)                shift; ___x_cmd_jo_nav              "$@" ;;
        -h|--help)          ___x_cmd help -m jo ;;
        *)
                            local ___X_CMD_JO_QUERY_JSTR="q1";
                            ___x_cmd_jo_query "$@"
                            ;;

        #   We will add some query functions and modified function here ...
        # *)
        #             if [ "$#" -eq 0 ]; then         ___x_cmd_ja___warp_color_iftty ___x_cmd_ja_normalize
        #             else                    local IFS=" "; printf "%s" "$*" | \
        #                                             ___x_cmd_ja___warp_color_iftty ___x_cmd_ja_normalize
        #             fi
    esac
}

# Section: subcmd

if [ -n "${BASH_VERSION}${ZSH_VERSION}${KSH_VERSION}" ]; then
    xrc:mod jo/lib/list_dict
else
    xrc:mod jo/lib/list_dict_posix
fi

xrc:mod jo/lib/query jo/lib/env jo/lib/ed jo/lib/list2line jo/lib/nav/_index

# EndSection



xrc setmain ___x_cmd_jo
