"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const http = require("http");
const https = require("https");
const url = require("url");
const gzip_1 = require("./gzip");
const options_1 = require("./options");
class ActiveRequest {
    static send(reqUrl, type, header, data) {
        return new Promise(async (resolve, reject) => {
            const urlParsed = url.parse(reqUrl, false);
            const lib = reqUrl.startsWith("https") ? https : http;
            let enableGZip = options_1.ActiveOptions.get("experimental", {}).gzip || false;
            let options = {
                hostname: urlParsed.hostname,
                path: urlParsed.path,
                port: urlParsed.port,
                method: type.toUpperCase(),
                headers: {}
            };
            if (enableGZip) {
                options.headers["Accept-Encoding"] = "gzip";
            }
            if (header) {
                let i = header.length;
                while (i--) {
                    const [name, value] = header[i].split(":");
                    options.headers[name] = value;
                }
            }
            if (data && (options.method == "POST" || options.method == "PUT")) {
                data = JSON.stringify(data);
                if (enableGZip) {
                    data = await gzip_1.ActiveGZip.gzip(data);
                    options.headers["Content-Encoding"] = "gzip";
                }
                options.headers["Content-Type"] = "application/json";
                options.headers["Content-Length"] = data.length;
            }
            const request = lib.request(options, (response) => {
                if (response.statusCode &&
                    (response.statusCode < 200 || response.statusCode > 299)) {
                    reject(new Error("URL Request Failed" + response.statusCode));
                }
                const body = [];
                response.on("data", chunk => body.push(chunk));
                response.on("end", async () => {
                    if (body.length) {
                        try {
                            const bodyBuffer = Buffer.concat(body);
                            let gdata;
                            if (response.headers["content-encoding"] == "gzip") {
                                gdata = await gzip_1.ActiveGZip.ungzip(bodyBuffer);
                            }
                            resolve({
                                data: JSON.parse((gdata || bodyBuffer).toString())
                            });
                        }
                        catch (error) {
                            reject(new Error("Failed to parse body"));
                        }
                    }
                    else {
                        resolve();
                    }
                });
            });
            request.on("error", err => reject(err));
            if (data && (options.method == "POST" || options.method == "PUT")) {
                request.write(data);
            }
            request.end();
        });
    }
}
exports.ActiveRequest = ActiveRequest;
//# sourceMappingURL=request.js.map