# shellcheck shell=dash

xrc:mod:lib weixin util/curl util/upload

___x_cmd_weixin_u_subcmd_invalid(){
    local subcmd="${1+"$1 "}"
    local arg="$2"
    if [ -z "$arg" ]; then
        ___x_cmd_ui_tf false "Subcommand required. See 'x weixin ${subcmd}--help|-h':" >&2
    else
        ___x_cmd_ui_tf false "'$arg' is not a subcommand. See 'x weixin ${subcmd}--help|-h':" >&2
    fi
    return 1
}

___x_cmd_weixin___help(){
    ___x_cmd help -m weixin "$@"
}


___x_cmd_weixin___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_weixin_parse_aes_key___(){
    local aes_key_raw="$1"
    [ -z "$aes_key_raw" ] || [ "$aes_key_raw" = "null" ] && return 1

    local aes_key_base
    aes_key_base="$(printf "%s" "$aes_key_raw" | ___x_cmd base64 -d)"

    if [ ${#aes_key_base} -eq 16 ]; then
        printf "%s" "$aes_key_base" | ___x_cmd_cmds xxd -p | ___x_cmd_cmds tr -d ' \n'
    elif [ ${#aes_key_base} -eq 32 ]; then
        local content; content=$(printf "%s" "$aes_key_base" | ___x_cmd_cmds tr -d ' \n')
        if printf "%s" "$content" | ___x_cmd grep -Eq '^[0-9a-fA-F]{32}$'; then
            printf "%s" "$content"
        else
            printf "%s" "$aes_key_base" |___x_cmd_cmds xxd -p | ___x_cmd_cmds tr -d ' \n'
        fi
    else
        return 1
    fi
}
