"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Copyright 2017-2020 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 files_1 = require("./files");
var progress_1 = require("./progress");
var zlib = require("zlib");
var tar = require("tar-fs");
var request = require("request");
var path = require("path");
var fs = require("fs");
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);
            files_1.removeSync(downloadDir);
            files_1.mkdirsSync(downloadDir);
            var responsePipe = request
                .get({
                url: url,
                rejectUnauthorized: false,
                agent: false,
                headers: headers
            })
                .on('response', function (response) { return 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);
                        files_1.unzipSync(zipPath, downloadDir);
                    }
                    resolve(downloadDir);
                });
            }
        }
        catch (error) {
            console.error('Unexpected error', error);
            reject(error);
        }
    });
}
exports.downlo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