# shellcheck shell=dash

xrc cfgy

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

    [ "$#" -gt 0 ] || {
        ___x_cmd_clawhub_init
        return
    }

    local op="$1"; shift
    case "$op" in
        init)               ___x_cmd_clawhub_init "$@" ;;
        *)                  ___x_cmd_clawhub_cfg___invoke "$op" "$@" ;;
    esac
}

___x_cmd_clawhub_cur(){
    case "$1" in -h|--help) shift; ___x_cmd help -m clawhub --cur "$@" ; return 0 ;; esac
    ___x_cmd_clawhub_cfg --current "$@"
}

___x_cmd_clawhub_init(){
    local cur_token=; local cur_endpoint=

    if [ -f "$(___x_cmd_clawhub_cur --get config)" ]; then
        ___x_cmd_clawhub_cur cur_token:=token cur_endpoint:=endpoint 2>/dev/null
    fi

    clawhub:info "Get your ClawHub token from https://clawhub.ai/settings/tokens"

    ___x_cmd_clawhub_cfg___invoke --init ${cur_token:+"--ctrl_exit_strategy"}  \
        token      "Set up the ClawHub API token (clh_...)"                    \
                   "$cur_token"    '=~*' "^clh_" --                            \
        endpoint   "Set up the API endpoint (optional)"                        \
                   "${cur_endpoint:-"https://clawhub.ai"}" || return $?
}

___X_CMD_CLAWHUB_CFG_VARLIST="token,endpoint"
___x_cmd_clawhub_cfg___invoke(){
    ___x_cmd_cfgy_obj   \
        --prefix            ___X_CMD_CLAWHUB_CFG_DATA                     \
        --default-config    "${___X_CMD_ROOT_CFG}/clawhub/X.cfg.yml"      \
        --current-config    "${___X_CMD_CLAWHUB_LOCAL_CONFIG}"            \
        --current-profile   "${___X_CMD_CLAWHUB_LOCAL_PROFILE}"           \
        --varlist           "$___X_CMD_CLAWHUB_CFG_VARLIST"               \
        "$@"
}
