
# shellcheck shell=dash

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

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

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

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

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

    ___x_cmd moonshot --cur model:= apikey:= endpoint:= 2>/dev/null

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

        if ___x_cmd_runmode_allow_manual; then
            ___x_cmd moonshot --cfg || return $?
            ___x_cmd moonshot --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
    }

    # endpoint transformation
    case "$endpoint" in
        https://*.moonshot.ai/*)    endpoint="https://api.moonshot.ai/anthropic" ;;
        https://*.moonshot.cn/*|"") endpoint="https://api.moonshot.cn/anthropic" ;;
    esac

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

    x_model="${model}"
    [ -n "$x_model" ] || x_model="kimi-k2-thinking-turbo"

    x_fastmodel="${model}"
    [ -n "$x_fastmodel" ] || x_fastmodel="kimi-k2.5"

    x_effortlevel=auto

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

