"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.AppPackageHelper = exports.nsisTemplatesDir = undefined;

var _bluebirdLst;

function _load_bluebirdLst() {
    return _bluebirdLst = require("bluebird-lst");
}

var _bluebirdLst2;

function _load_bluebirdLst2() {
    return _bluebirdLst2 = _interopRequireDefault(require("bluebird-lst"));
}

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

var _fsExtraP;

function _load_fsExtraP() {
    return _fsExtraP = require("fs-extra-p");
}

var _pathManager;

function _load_pathManager() {
    return _pathManager = require("../../util/pathManager");
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const nsisTemplatesDir = exports.nsisTemplatesDir = (0, (_pathManager || _load_pathManager()).getTemplatePath)("nsis");
class AppPackageHelper {
    constructor() {
        this.archToFileInfo = new Map();
        this.infoToIsDelete = new Map();
        /** @private */
        this.refCount = 0;
    }
    packArch(arch, target) {
        var _this = this;

        return (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* () {
            let infoPromise = _this.archToFileInfo.get(arch);
            if (infoPromise == null) {
                infoPromise = target.buildAppPackage(target.archs.get(arch), arch);
                _this.archToFileInfo.set(arch, infoPromise);
            }
            const info = yield infoPromise;
            if (target.isWebInstaller) {
                _this.infoToIsDelete.set(info, false);
            } else if (!_this.infoToIsDelete.has(info)) {
                _this.infoToIsDelete.set(info, true);
            }
            return info;
        })();
    }
    finishBuild() {
        var _this2 = this;

        return (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* () {
            if (--_this2.refCount > 0) {
                return;
            }
            const filesToDelete = [];
            for (const _ref of _this2.infoToIsDelete.entries()) {
                var _ref2 = _slicedToArray(_ref, 2);

                const info = _ref2[0];
                const isDelete = _ref2[1];

                if (isDelete) {
                    filesToDelete.push(info.file);
                }
            }
            yield (_bluebirdLst2 || _load_bluebirdLst2()).default.map(filesToDelete, function (it) {
                return (0, (_fsExtraP || _load_fsExtraP()).unlink)(it);
            });
        })();
    }
}
exports.AppPackageHelper = AppPackageHelper; //# sourceMappingURL=nsisUtil.js.map