# shellcheck shell=dash

xrc ui

___x_cmd log init clawhub
xrc:mod:lib     clawhub              \
                util/index           \
                cfg                  \
                publish              \
                account/index        \
                skill/_index

___X_CMD_CLAWHUB_TMP="$___X_CMD_ROOT_TMP/clawhub"
___X_CMD_CLAWHUB_DATA="$___X_CMD_ROOT_DATA/clawhub"
___x_cmd mkdirp "${___X_CMD_CLAWHUB_DATA}" "${___X_CMD_CLAWHUB_TMP}"

___x_cmd_clawhub___main(){
    case "$1" in -h|--help) shift; ___x_cmd help -m clawhub "$@" ; return 0 ;; esac

    local ___X_CMD_CLAWHUB_LOCAL_CONFIG="${___X_CMD_CLAWHUB_LOCAL_CONFIG}"
    local ___X_CMD_CLAWHUB_LOCAL_PROFILE="${___X_CMD_CLAWHUB_LOCAL_PROFILE}"

    local op=
    while [ $# -gt 0 ]; do
        op="$1"; shift
        case "$op" in
            :*:*)
                        ___X_CMD_CLAWHUB_LOCAL_CONFIG="${op#:}"
                        ___X_CMD_CLAWHUB_LOCAL_CONFIG="${___X_CMD_CLAWHUB_LOCAL_CONFIG%%:*}"
                        ___X_CMD_CLAWHUB_LOCAL_PROFILE="${op#:"$___X_CMD_CLAWHUB_LOCAL_CONFIG":}"
                        ;;
            :*)         ___X_CMD_CLAWHUB_LOCAL_PROFILE="${op#:}"         ;;
            --profile)  ___X_CMD_CLAWHUB_LOCAL_PROFILE="$1";       shift ;;
            --config)   ___X_CMD_CLAWHUB_LOCAL_CONFIG="$1";        shift ;;
            *=*)        ___x_cmd_clawhub_cur "$op" "$@"
                        return ;;
            *)          break ;;
        esac
    done

    local endpoint; ___x_cmd_clawhub_cur endpoint:=endpoint 2>/dev/null
    local ___X_CMD_CLAWHUB_PRIMARY_URL="${endpoint:-"https://clawhub.ai"}"
    local ___X_CMD_CLAWHUB_SERVICE_URL="${endpoint}"
    if [ -z "$___X_CMD_CLAWHUB_SERVICE_URL" ]; then
        if ___x_cmd websrc is cn 2>/dev/null; then
            ___X_CMD_CLAWHUB_SERVICE_URL="https://cn.clawhub-mirror.com"
            clawhub:debug --reference "$___X_CMD_CLAWHUB_SERVICE_URL/about" \
                "Using cn.clawhub-mirror (ByteDance/Volcano Engine mirror) for improved accessibility in your region."
        else
            ___X_CMD_CLAWHUB_SERVICE_URL="https://clawhub.ai"
            clawhub:debug --reference "$___X_CMD_CLAWHUB_SERVICE_URL/about" \
                "Using clawhub.ai (OpenCLAW Skill Registry), the primary Skill hosting platform."
        fi
    fi

    case $op in
        --cfg|--cur)
                    "___x_cmd_clawhub_${op#--}"         "$@" ;;

        init|cfg)
                    ___x_cmd_clawhub_cfg "$@"           ;;

        is-login)
                    ___x_cmd_clawhub_account_is_login    "$@" ;;

        login|logout|whoami)
                    ___x_cmd_clawhub_account_"$op"      "$@" ;;

        account)
                    ___x_cmd_clawhub_account             "$@" ;;

        search|resolve|explore)
                    ___x_cmd_clawhub_skill_"$op"         "$@" ;;

        skill)
                    ___x_cmd_clawhub_skill               "$@" ;;

        download)
                    ___x_cmd_clawhub_skill_download      "$@" ;;

        # sanitize|preflight|version|semver)
        #             ___x_cmd_clawhub_cat "$op"            ;;
        check)
                    ___x_cmd_clawhub_check               "$@" ;;
        preview)
                    ___x_cmd_clawhub_preview             "$@" ;;
        publish)
                    ___x_cmd_clawhub_publish             "$@" ;;

        --|--runmain)
                    ___x_cmd_clawhub___runmain "$@" ;;
        *)
                    ___x_cmd_clawhub_u_subcmd_invalid "" "$op" ;;
    esac
}

___x_cmd_clawhub___runmain(){
    ___x_cmd npx clawhub "$@"
}

___x_cmd_clawhub_cat() {
    local op="$1"
    local file="${0%/*}/../data"
    case "$op" in
        sanitize)  file="$file/sanitize.md" ;;
        preflight) file="$file/preflight.checklist.yml" ;;
        version|semver) file="$file/VERSIONING.md" ;;
        *) M="Unknown info command: $op" N=clawhub log:ret:64 ;;
    esac
    if [ -f "$file" ]; then
        cat "$file"
    else
        printf "Not found: %s\n" "$file" >&2
        return 1
    fi
}
