# shellcheck shell=dash disable=SC2034

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

    local op="$1"; shift
    case "$op" in
        -h|--help)      ___x_cmd help -m kimi --cfg ;;
        apikey=*)       ___x_cmd_kimi_cfg___invoke "$op" "$@"
                        ;;
        def_)           ___x_cmd_kimi_def_ "$@" ;;
        *)              ___x_cmd_kimi_cfg___invoke "$op" "$@" ;;
    esac
}

___x_cmd_kimi_def_(){
    local op="$1";
    case "$op" in
        model)      x_="$___X_CMD_KIMI_DEFAULT_FIRST_MODEL" ;;
        endpoint)   x_="$___X_CMD_KIMI_DEFAULT_ENDPOINT" ;;
        *) ;;
    esac
}


___x_cmd_kimi_cur(){
    local X_help_cmd=; X_help_cmd='___x_cmd help -m kimi --cur' help:arg:parse
    ___x_cmd_kimi_cfg --current "$@"
}

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

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

    kimi:info "To get Kimi Code API key -> https://www.kimi.com/code/console"

    ___x_cmd_kimi_cfg___invoke --init ${cur_apikey:+"--ctrl_exit_strategy"} \
        apikey      "Set up the API key of Kimi Code"                         \
                    "$cur_apikey"   '=~*' "^[A-Za-z0-9-]+$" --                  \
        model       "Set up the Kimi Code AI model"                              \
                    "${cur_model:-"$___X_CMD_KIMI_DEFAULT_FIRST_MODEL"}"   '=' kimi-for-coding  --  \
        maxtoken    "Set up the Kimi Code AI maxtoken"                           \
                    "${cur_maxtoken:-200000}"   '=~' "^[0-9]+$" --              \
        temperature "Set up the Kimi Code AI 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_KIMI_DEFAULT_ENDPOINT"}"|| return $?

    ! ___x_cmd_kimi_has_apikey 2>/dev/null >&2 || ___x_cmd chat --setalias kimi kimi
}

___X_CMD_KIMI_CFG_VARLIST="apikey,model,maxtoken,temperature,seed,proxy,endpoint"
___x_cmd_kimi_cfg___invoke(){
    ___x_cmd_cfgy_obj                                                 \
        --prefix            ___X_CMD_KIMI_CFG_DATA                \
        --default-config    "${___X_CMD_ROOT_CFG}/kimi/X.cfg.yml" \
        --current-config    "${___X_CMD_KIMI_LOCAL_CONFIG}"       \
        --current-profile   "${___X_CMD_KIMI_LOCAL_PROFILE}"      \
        --varlist           "$___X_CMD_KIMI_CFG_VARLIST"          \
        "$@"
}

___x_cmd_kimi_has_apikey(){
    local apikey=
    ___x_cmd_kimi_cur apikey:= 2>/dev/null
    [ -n "$apikey" ] || {
        kimi:error "Please setting up your apikey first ==> 'x kimi --cfg apikey=<your apikey>'"
        kimi:info "You can get the gemini apikey from ==> https://www.kimi.com/code/console"
        return 1
    }
}

___x_cmd_kimi___get_apikey_endpoint_proxy_(){
    apikey=""; endpoint=""; proxy=""
    ___x_cmd_kimi_has_apikey || return $?
    ___x_cmd_kimi_cur apikey:=  endpoint:= proxy:= 2>/dev/null
    endpoint="${endpoint:-"$___X_CMD_KIMI_DEFAULT_ENDPOINT"}"
}


