# shellcheck shell=dash

___X_CMD_CLAUDE_BAIDU_DEFAULT_ENDPOINT="https://qianfan.baidubce.com/anthropic/coding/v1/messages"
___X_CMD_CLAUDE_BAIDU_DEFAULT_FIRST_MODEL="qianfan-code-latest"

___x_cmd_claude_baidu(){
    local op="$1"
    case "$op" in
        -h|--help)      ___x_cmd help -m claude baidu; return 0 ;;
        --cfg)          shift;
                        ___x_cmd_claude_baidu_cfg "$@"; return $? ;;
        --)             shift ;;
    esac

    local model=""
    local apikey=""
    local endpoint=""
    local x_=""

    claude:provider:fetch:init
    ___x_cmd_claude_baidu___fetch_   || return $?

    ANTHROPIC_API_KEY="" API_TIMEOUT_MS="$x_timeout"        \
        ___x_cmd_claude_other___use     baidu               \
        "$x_url" "$x_apikey" "$x_model" "$x_fastmodel" "$x_nonessential_traffic" "$x_effortlevel" "$@"
}

___x_cmd_claude_baidu___fetch_(){
    local model=""
    local apikey=""
    local endpoint=

    ___x_cmd_claude_baidu_cur model:=    apikey:=    endpoint:= 2>/dev/null

    [ -n "$apikey" ] || {
        claude:warn \
            --tip1 'x claude baidu --cfg'                           \
            --tip2 'x claude baidu --cfg apikey=<YOUR-API-KEY>'     \
            "Please setup baidu coding plan API-KEY."

        if ___x_cmd_runmode_allow_manual; then
            ___x_cmd claude baidu --cfg || return $?
            ___x_cmd_claude_baidu_cur model:=    apikey:=    endpoint:= 2>/dev/null
        fi
        [ -n "$apikey" ] || N=claude M="Failed to retrieve API-KEY. Please check your configuration." log:ret:1
    }

    [ -n "$endpoint" ] || endpoint="$___X_CMD_CLAUDE_BAIDU_DEFAULT_ENDPOINT"

    x_url="$endpoint"
    x_apikey="$apikey"

    x_model="${model}"
    [ -n "$x_model" ] || x_model="$___X_CMD_CLAUDE_BAIDU_DEFAULT_FIRST_MODEL"

    x_fastmodel="${x_model}"
    x_effortlevel=auto

    x_timeout="${API_TIMEOUT_MS:-"3000000"}"
}


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

    local op="$1"; shift
    case "$op" in
        -h|--help)      ___x_cmd help -m claude baidu --cfg ;;
        *)              ___x_cmd_claude_baidu_cfg___invoke "$op" "$@" ;;
    esac
}


___x_cmd_claude_baidu_cur(){
    local X_help_cmd=; X_help_cmd='___x_cmd help -m claude baidu --cur' help:arg:parse
    ___x_cmd_claude_baidu_cfg --current "$@"
}

___x_cmd_claude_baidu_init(){
    local cur_apikey=; local cur_model=; local cur_endpoint

    if [ -f "$(___x_cmd_claude_baidu_cur --get config)" ]; then
        ___x_cmd_claude_baidu_cur cur_apikey:=apikey cur_model:=model cur_endpoint:=endpoint 2>/dev/null
    fi

    claude:info "To get baidu coding plan API key -> https://console.bce.baidu.com/qianfan/resource/subscribe"

    ___x_cmd_claude_baidu_cfg___invoke --init ${cur_apikey:+"--ctrl_exit_strategy"} \
        apikey      "Set up the baidu coding plan API key"                     \
                    "$cur_apikey"   '=~*' "^[A-Za-z0-9_-]+$" --                \
        model       "Set up the baidu API model"                               \
                    "${cur_model:-"$___X_CMD_CLAUDE_BAIDU_DEFAULT_FIRST_MODEL"}" '=' qianfan-code-latest Kimi-K2.5 DeepSeek-V3.2 GLM-5 MiniMax-M2.5  --  \
        endpoint    "Set up the endpoint (optional)"                            \
                    "${cur_endpoint:-"$___X_CMD_CLAUDE_BAIDU_DEFAULT_ENDPOINT"}" || return $?
}


___X_CMD_CLAUDE_BAIDU_CFG_VARLIST="apikey,model,endpoint"
___x_cmd_claude_baidu_cfg___invoke(){
    ___x_cmd_cfgy_obj   \
        --prefix            ___X_CMD_CLAUDE_BAIDU_CFG_DATA                \
        --default-config    "${___X_CMD_ROOT_CFG}/claude/baidu/X.cfg.yml" \
        --current-config    "${___X_CMD_CLAUDE_BAIDU_LOCAL_CONFIG}"       \
        --current-profile   "${___X_CMD_CLAUDE_BAIDU_LOCAL_PROFILE}"      \
        --varlist           "$___X_CMD_CLAUDE_BAIDU_CFG_VARLIST"          \
        "$@"
}


