# shellcheck shell=dash

___x_cmd_qywx_jqu(){
    local IFS=" "
    printf "%s" "$*" | ___x_cmd_cmds_awk '
{
    text = (text == "") ? $0 : (text "\n" $0)
}
END{
    gsub(/\\/, "\\\\", text)
    gsub("\n", "\\n", text)
    gsub("\t", "\\t", text)
    gsub("\"", "\\\"", text)
    printf("\"%s\"", text)
}
'
}

___x_cmd_qywx___get_key_(){
    local webhook="$1"
    [ -n "$webhook" ] || {
        ___x_cmd_qywx_cur webhook:= 2>/dev/null
        [ -n "$webhook" ] || N=qywx M="Please setting up your webhook first ==> 'x qywx --cfg webhook=<your webhook url>'" log:ret:1
    }

    local key="${webhook#*"key="}"
    x_="${key%%&*}"
}

___x_cmd_qywx_isready(){
    param:void

    local bot_id
    local bot_secret
    ___x_cmd_qywx_cur bot_id:=bot_id bot_secret:=bot_secret 2>/dev/null

    [ -n "$bot_id" ] && [ -n "$bot_secret" ]
}

___x_cmd_qywx_ensureinit(){
    if ! ___x_cmd_qywx_isready; then
        qywx:error --cmd "x qywx init" "Please use 'x qywx init' to configure the Qywx bot's bot_id and bot_secret."
        ___x_cmd qywx init || return $?
    fi
}
