"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sign_type = void 0;
exports.getT544 = getT544;
exports.getSign = getSign;
exports.requestSignToken = requestSignToken;
exports.submitSsoPacket = submitSsoPacket;
exports.getApiQQVer = getApiQQVer;
exports.getCmdWhiteList = getCmdWhiteList;
exports.apiPing = apiPing;
const axios_1 = __importDefault(require("axios"));
const base_client_1 = require("./base-client");
const constants_1 = require("./constants");
const http_1 = __importDefault(require("http"));
const https_1 = __importDefault(require("https"));
exports.sign_type = "sign";
const axios = axios_1.default.create({
    httpAgent: new http_1.default.Agent({
        keepAlive: true,
        keepAliveMsecs: 60000
    }),
    httpsAgent: new https_1.default.Agent({
        keepAlive: true,
        keepAliveMsecs: 60000
    }),
    timeout: 20000,
    headers: {
        "Connection": "keep-alive"
    }
});
async function getT544(cmd) {
    let sign = constants_1.BUF0;
    if (this.sig.sign_api_addr && this.apk.qua) {
        const time = Date.now();
        let post_params = {
            ver: this.apk.ver,
            uin: this.uin || 0,
            data: cmd,
            guid: this.device.guid.toString("hex"),
            version: this.apk.sdkver
        };
        const url = new URL(this.sig.sign_api_addr);
        url.pathname += "energy";
        const data = await get.bind(this)(url.href, post_params, true);
        const log = `getT544:${cmd} result(${Date.now() - time}ms):${JSON.stringify(data)}`;
        if (data.code === 0) {
            this.emit("internal.verbose", log, base_client_1.VerboseLevel.Debug);
            if (typeof (data.data) === "string") {
                sign = Buffer.from(data.data, "hex");
            }
            else if (typeof (data.data?.sign) === "string") {
                sign = Buffer.from(data.data.sign, "hex");
                if (typeof (data.data.t553) === "string")
                    this.sig.t553 = Buffer.from(data.data.t553, "hex");
            }
        }
        else {
            this.emit("internal.verbose", `签名api异常：${log}`, base_client_1.VerboseLevel.Error);
        }
    }
    return this.generateT544Packet(cmd, sign);
}
async function getSign(cmd, seq, body) {
    let params = constants_1.BUF0;
    if (!this.sig.sign_api_addr)
        return params;
    let qImei36 = this.device.qImei36 || this.device.qImei16;
    if (this.apk.qua) {
        const time = Date.now();
        let post_params = {
            ver: this.apk.ver,
            qua: this.apk.qua,
            uin: this.uin || 0,
            cmd: cmd,
            seq: seq,
            androidId: this.device.android_id,
            qimei36: qImei36 || this.device.android_id,
            guid: this.device.guid.toString("hex"),
            buffer: body.toString("hex")
        };
        const url = new URL(this.sig.sign_api_addr);
        url.pathname += "sign";
        const data = await get.bind(this)(url.href, post_params, true);
        const log = `sign:${cmd} seq:${seq} result(${Date.now() - time}ms):${JSON.stringify(data)}`;
        if (data.code === 0) {
            this.emit("internal.verbose", log, base_client_1.VerboseLevel.Debug);
            const Data = data.data || {};
            params = this.generateSignPacket(Data.sign, Data.token, Data.extra);
            let list = Data.ssoPacketList || Data.requestCallback || [];
            if (list.length > 0)
                this.ssoPacketListHandler(list);
        }
        else {
            this.emit("internal.verbose", `签名api异常：${log}`, base_client_1.VerboseLevel.Error);
        }
    }
    return params;
}
async function requestSignToken() {
    if (!this.sig.sign_api_addr)
        return [];
    let qImei36 = this.device.qImei36 || this.device.qImei16;
    if (this.apk.qua) {
        const time = Date.now();
        let post_params = {
            ver: this.apk.ver,
            qua: this.apk.qua,
            uin: this.uin || 0,
            androidId: this.device.android_id,
            qimei36: qImei36 || this.device.android_id,
            guid: this.device.guid.toString("hex")
        };
        const url = new URL(this.sig.sign_api_addr);
        url.pathname += "request_token";
        const data = await get.bind(this)(url.href, post_params, true);
        this.emit("internal.verbose", `requestSignToken result(${Date.now() - time}ms): ${JSON.stringify(data)}`, base_client_1.VerboseLevel.Debug);
        if (data.code === 0) {
            let ssoPacketList = data.data?.ssoPacketList || data.data?.requestCallback || data.data;
            if (!ssoPacketList || ssoPacketList.length < 1)
                return [];
            return ssoPacketList;
        }
    }
    return [];
}
async function submitSsoPacket(cmd, callbackId, body) {
    if (!this.sig.sign_api_addr)
        return [];
    let qImei36 = this.device.qImei36 || this.device.qImei16;
    if (this.apk.qua) {
        const time = Date.now();
        let post_params = {
            ver: this.apk.ver,
            qua: this.apk.qua,
            uin: this.uin || 0,
            cmd: cmd,
            callbackId: callbackId,
            androidId: this.device.android_id,
            qimei36: qImei36 || this.device.android_id,
            buffer: body.toString("hex"),
            guid: this.device.guid.toString("hex")
        };
        const url = new URL(this.sig.sign_api_addr);
        url.pathname += "submit";
        const data = await get.bind(this)(url.href, post_params, true);
        this.emit("internal.verbose", `submitSsoPacket result(${Date.now() - time}ms): ${JSON.stringify(data)}`, base_client_1.VerboseLevel.Debug);
        if (data.code === 0) {
            let ssoPacketList = data.data?.ssoPacketList || data.data?.requestCallback || data.data;
            if (!ssoPacketList || ssoPacketList.length < 1)
                return [];
            return ssoPacketList;
        }
    }
    return [];
}
async function getApiQQVer() {
    let QQVer = this.config.ver;
    if (!this.sig.sign_api_addr)
        return QQVer;
    const apks = this.getApkInfoList(this.config.platform);
    const packageName = this.apk.id;
    const url = new URL(this.sig.sign_api_addr);
    url.pathname += "ver";
    const data = await get.bind(this)(url.href);
    if (data.code === 0) {
        const vers = data?.data[packageName];
        if (vers && vers.length > 0) {
            for (let ver of vers) {
                if (apks.find(val => val.ver === ver)) {
                    QQVer = ver;
                    break;
                }
            }
        }
    }
    return QQVer;
}
async function getCmdWhiteList() {
    let whiteList = [];
    if (!this.sig.sign_api_addr)
        return whiteList;
    const url = new URL(this.sig.sign_api_addr);
    url.pathname += "cmd_whitelist";
    const data = await get.bind(this)(url.href, {
        ver: this.apk.ver,
        uin: this.uin || 0
    });
    if (data.code === 0) {
        whiteList = data?.data?.list || [];
    }
    return whiteList;
}
async function apiPing(pathname) {
    if (!this.sig.sign_api_addr)
        return false;
    const url = new URL(this.sig.sign_api_addr);
    url.pathname += pathname || 'ping';
    const ret = await axios.get(url.href, {
        headers: {
            "User-Agent": `icqq@${this.pkg.version} (Released on ${this.pkg.upday})`,
            "Content-Type": "application/x-www-form-urlencoded"
        }
    }).catch(err => ({ data: { code: -1, msg: err?.message } }));
    return ret?.data || { code: -1 };
}
async function get(url, params = {}, post = false) {
    let data = { code: -1 };
    let num = 0;
    while (data.code === -1 && num < 3) {
        if (num > 0)
            await new Promise((resolve) => setTimeout(resolve, 2000));
        num++;
        if (post) {
            data = await axios.post(url, params, {
                headers: {
                    "User-Agent": `icqq@${this.pkg.version} (Released on ${this.pkg.upday})`,
                    "Content-Type": "application/x-www-form-urlencoded"
                }
            }).catch(err => ({ data: { code: -1, msg: err?.message } }));
        }
        else {
            data = await axios.get(url, {
                params, headers: {
                    "User-Agent": `icqq@${this.pkg.version} (Released on ${this.pkg.upday})`,
                    "Content-Type": "application/x-www-form-urlencoded"
                }
            }).catch(err => ({ data: { code: -1, msg: err?.message } }));
        }
        data = data.data;
    }
    return data;
}
