# shellcheck shell=dash disable=SC2034

xrc cfgy
___x_cmd_zhipu_cfg(){
    [ "$#" -gt 0 ] || {
        ___x_cmd_zhipu_init
        return
    }

    local op="$1"; shift
    case "$op" in
        -h|--help)      x help -m zhipu --cfg ;;
        apikey=*)       ___x_cmd_zhipu_cfg___invoke "$op" "$@"
                        ! ___x_cmd_zhipu_has_apikey 2>/dev/null >&2 || ___x_cmd chat --setalias zhipu glm
                        ;;
        *)              ___x_cmd_zhipu_cfg___invoke "$op" "$@" ;;
    esac
}

___x_cmd_zhipu_cur(){
    local X_help_cmd=; X_help_cmd='x help -m zhipu --cur' help:arg:parse
    ___x_cmd_zhipu_cfg --current "$@"
}

___x_cmd_zhipu_init(){
        local cur_apikey=; local cur_model=; local cur_maxtoken=; local cur_temperature=; local cur_proxy=; local cur_endpoint

    if [ -f "$(___x_cmd_zhipu_cur --get config)" ]; then
        ___x_cmd_zhipu_cur cur_apikey:=apikey cur_model:=model cur_maxtoken:=maxtoken cur_temperature:=temperature cur_proxy:=proxy cur_endpoint:=endpoint 2>/dev/null
    fi

    zhipu:info "To get Zhipu API key -> https://bigmodel.cn/usercenter/proj-mgmt/apikeys"

    ___x_cmd_zhipu_cfg___invoke --init ${cur_apikey:+"--ctrl_exit_strategy"} \
        apikey      "Set up the Zhipu API key"                               \
                    "$cur_apikey"   '*' --                  \
        model       "Set up the Zhipu API model"                             \
                    "${cur_model:-"$___X_CMD_ZHIPU_DEFAULT_FIRST_MODEL"}" '=' \
                    glm-4.5 glm-4.5-air glm-4.5-x glm-4.5-airx glm-4.5-flash glm-4-plus glm-4-air-250414 glm-4-airx glm-4-flashx glm-4-flashx-250414 glm-z1-air glm-z1-airx glm-z1-flash glm-z1-flashx --  \
        maxtoken    "Set up the Zhipu API maxtoken"                          \
                    "${cur_maxtoken:-4096}" --         \
        temperature "Set up the Zhipu API temperature"                       \
                    "${cur_temperature:-"1.0"}" --                              \
        proxy       "Set up the network proxy for API requests (optional)"      \
                    "$cur_proxy" '=~'    "^([a-z0-9]+://)?[0-9.:]+$" --        \
        endpoint    "Set up the endpoint (optional)"                            \
                    "${cur_endpoint:-"$___X_CMD_ZHIPU_DEFAULT_ENDPOINT"}"|| return $?

    ! ___x_cmd_zhipu_has_apikey 2>/dev/null >&2 || ___x_cmd chat --setalias zhipu glm

}

___x_cmd_zhipu_has_apikey(){
    local apikey=
    ___x_cmd_zhipu_cur apikey:= 2>/dev/null
    [ -n "$apikey" ] || {
        zhipu:error "Please setting up your apikey first ==> 'x zhipu --cfg apikey=<your apikey>'"
        zhipu:warn "You can get the Zhipu API Key from ==>  https://bigmodel.cn/usercenter/proj-mgmt/apikeys"
        return 1
    }
}
___X_CMD_ZHIPU_CFG_VARLIST="apikey,model,maxtoken,temperature,seed,proxy,endpoint"
___x_cmd_zhipu_cfg___invoke(){
    ___x_cmd_cfgy_obj                                               \
        --prefix            ___X_CMD_ZHIPU_CFG_DATA                \
        --default-config    "${___X_CMD_ROOT_CFG}/zhipu/X.cfg.yml" \
        --current-config    "${___X_CMD_ZHIPU_LOCAL_CONFIG}"       \
        --current-profile   "${___X_CMD_ZHIPU_LOCAL_PROFILE}"      \
        --varlist           "$___X_CMD_ZHIPU_CFG_VARLIST"          \
        "$@"
}

