"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeUpload = void 0;
const req_1 = require("@applitools/req");
const zlib_1 = require("zlib");
const utils = __importStar(require("@applitools/utils"));
function makeUpload({ settings, logger, }) {
    return async function upload({ name, resource, gzip }) {
        logger === null || logger === void 0 ? void 0 : logger.log(`Upload called for ${name} resource`);
        if (utils.types.isNull(resource) || utils.types.isHttpUrl(resource))
            return resource;
        else if (resource instanceof URL)
            return resource.href;
        const url = settings.uploadUrl.replace('__random__', utils.general.guid());
        const body = gzip ? (0, zlib_1.gzipSync)(resource) : resource;
        const response = await (0, req_1.req)(url, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/octet-stream',
                Date: new Date().toISOString(),
                'x-ms-blob-type': 'BlockBlob',
            },
            body,
            proxy: settings.proxy,
            retry: {
                limit: 5,
                timeout: 500,
                statuses: [404, 500, 502, 503, 504],
                codes: ['ECONNRESET', 'ECONNABORTED', 'ETIMEDOUT', 'ENOTFOUND', 'EAI_AGAIN'],
            },
            hooks: {
                beforeRetry({ response, error, attempt }) {
                    logger === null || logger === void 0 ? void 0 : logger.warn(`Upload of ${name} resource will be retried due to ${error
                        ? `an error with message "${error.message}"`
                        : `unexpected status ${response.statusText}(${response.status})`} in previous attempt (${attempt})`);
                },
            },
        });
        if (response.status !== 201) {
            throw new Error(`Upload of ${name} resource failed due to unexpected status ${response.statusText}(${response.status})`);
        }
        logger === null || logger === void 0 ? void 0 : logger.log(`Upload of ${name} resource finished successfully in location`, url);
        return url;
    };
}
exports.makeUpload = makeUpload;
