"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const AuthType_1 = require("../enums/AuthType");
const RequestBodyType_1 = require("../enums/RequestBodyType");
const BodyUtils_1 = require("../utils/BodyUtils");
const js_sha256_1 = require("js-sha256");
class StcHttpRequestObj {
    constructor(config, method, sid, version, tags, params, header, bodyObj, authType, bodyType) {
        this.header = {};
        this.params = {};
        this.header = header;
        this.params = params;
        this.method = method;
        this.header["sid"] = sid;
        this.header["version"] = version;
        this.header["tags"] = tags;
        this.header[config.headerVariableName] = config.appId;
        this.header["envCode"] = config.envCode;
        const list = [];
        //RAW_JSON请求时，http协议不会对数据进行encode，所以签名也就不用encode
        BodyUtils_1.default.bodyToArr("", bodyObj, list, bodyType != RequestBodyType_1.RequestBodyType.RAW_JSON);
        let bodyStr = BodyUtils_1.default.arrToStr(list);
        let timestamp = Number.parseInt((new Date().getTime() / 1000).toString());
        this.header["timestamp"] = timestamp.toString();
        let sign, signSrc = "";
        switch (authType) {
            case AuthType_1.AuthType.NONE:
                break;
            case AuthType_1.AuthType.BASE:
                signSrc = sid + version + timestamp + config.appKey;
                break;
            case AuthType_1.AuthType.ADVANCED:
                signSrc = sid + version + bodyStr + timestamp + config.appKey;
                break;
        }
        sign = js_sha256_1.sha256(signSrc);
        this.header["sign"] = sign;
        this.header["signSrc"] = signSrc;
        if (bodyObj != null && !BodyUtils_1.default.isPrimitiveDataTypes(bodyObj)) {
            this.bodyContent = JSON.stringify(bodyObj);
        }
    }
}
exports.StcHttpRequestObj = StcHttpRequestObj;
//# sourceMappingURL=StcHttpRequestObj.js.map