"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.downloadPackage = void 0;
/**
 * Copyright 2017-2022 Plexus Interop Deutsche Bank AG
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var fs = require("fs");
var path = require("path");
var request = require("request");
var tar = require("tar-fs");
var zlib = require("zlib");
var files_1 = require("./files");
var progress_1 = require("./progress");
function downloadPackage(url, downloadDir, title, headers) {
    if (title === void 0) { title = 'Package'; }
    if (headers === void 0) { headers = {}; }
    return new Promise(function (resolve, reject) {
        try {
            var isZip_1 = url.endsWith('.zip');
            var isTarGz = url.endsWith('.tar.gz');
            var fileName_1 = url.split('/').pop();
            console.log("Downloading " + title + " from: ", url);
            console.log('Target dir: ', downloadDir);
            (0, files_1.removeSync)(downloadDir);
            (0, files_1.mkdirsSync)(downloadDir);
            var responsePipe = request
                .get({
                url: url,
                rejectUnauthorized: false,
                agent: false,
                headers: headers,
            })
                .on('response', function (response) { return (0, progress_1.printProgress)(response, "Downloading " + title); })
                .on('error', function (error) {
                console.error(title + " download failed", error);
                reject(error);
            });
            if (isTarGz) {
                responsePipe
                    .pipe(zlib.createUnzip())
                    .pipe(tar.extract(downloadDir))
                    .on('end', function () {
                    console.log(title + " download finished");
                    resolve(downloadDir);
                });
            }
            else {
                responsePipe.pipe(fs.createWriteStream(path.join(downloadDir, fileName_1))).on('close', function () {
                    console.log(title + " download finished");
                    if (isZip_1) {
                        var zipPath = path.join(downloadDir, fileName_1);
                        (0, files_1.unzipSync)(zipPath, downloadDir);
                    }
                    resolve(downloadDir);
                });
            }
        }
        catch (error) {
            console.error('Unexpected error', error);
            reject(error);
        }
    });
}
exports.downloadPackage = downloadPackage;
//# sourceMappingURL=download.js.map