"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get axiosHttp2Adapter () {
        return axiosHttp2Adapter;
    },
    get downloadFile () {
        return downloadFile;
    },
    get downloadFileToPath () {
        return downloadFileToPath;
    }
});
function _nodefs() {
    const data = require("node:fs");
    _nodefs = function() {
        return data;
    };
    return data;
}
function _stream() {
    const data = /*#__PURE__*/ _interop_require_wildcard(require("stream"));
    _stream = function() {
        return data;
    };
    return data;
}
function _util() {
    const data = require("util");
    _util = function() {
        return data;
    };
    return data;
}
function _axios() {
    const data = /*#__PURE__*/ _interop_require_default(require("axios"));
    _axios = function() {
        return data;
    };
    return data;
}
function _http2wrapper() {
    const data = /*#__PURE__*/ _interop_require_wildcard(require("http2-wrapper"));
    _http2wrapper = function() {
        return data;
    };
    return data;
}
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const axiosHttp2Adapter = (config)=>{
    let req = undefined;
    config.transport = {
        request: function request(options, handleResponse) {
            req = _http2wrapper().request(options, handleResponse);
            return req;
        }
    };
    const ret = _axios().default.defaults.adapter(config);
    // Remove the axios action `socket.setKeepAlive` because the HTTP/2 sockets should not be directly manipulated
    const listeners = req.listeners("socket");
    if (listeners.length) req.removeListener("socket", listeners[0]);
    return ret;
};
const _pipeline = (0, _util().promisify)(_stream().pipeline);
const downloadFile = async (url, writeStream, axiosInstance = _axios().default)=>{
    const req = await axiosInstance({
        method: "get",
        url,
        responseType: "stream"
    });
    await _pipeline(req.data, writeStream);
};
const downloadFileToPath = async (url, path, axiosInstance)=>{
    // const writeStream = createWriteStream(path);
    // await downloadFile(url, writeStream);
    const finishedDownload = (0, _util().promisify)(_stream().finished);
    const writer = (0, _nodefs().createWriteStream)(path, {
        autoClose: true,
        flags: "w"
    });
    // content-type is application/octet-stream"
    const response = await (axiosInstance ?? _axios().default)({
        method: "GET",
        url: url,
        responseType: "stream",
        maxBodyLength: Infinity
    });
    response.data.pipe(writer);
    await finishedDownload(writer);
};
