"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.moveFiles = exports.moveFolder = void 0;
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const rxjs_1 = require("rxjs");
const index_js_1 = require("rxjs/operators/index.js");
const globby = require("globby");
const copyFile = (from, to) => (file) => (0, fs_extra_1.copy)((0, path_1.join)(from, file), (0, path_1.join)(to, file), { overwrite: true });
function moveFolder({ from, to, patterns }, progressHandler = () => { }) {
    return __awaiter(this, void 0, void 0, function* () {
        const files = yield globby(patterns, { cwd: from, dot: true });
        return moveFiles({ from, to, files }, progressHandler);
    });
}
exports.moveFolder = moveFolder;
function moveFiles({ from, to, files, fileHandler = copyFile }, progressHandler = () => { }) {
    const copyFilesActions = files.map(fileHandler(from, to));
    return trackProgress(copyFilesActions, progressHandler);
}
exports.moveFiles = moveFiles;
function trackProgress(arrayOfPromises, onProgressCallback = () => { }) {
    const count = arrayOfPromises.length;
    return (0, rxjs_1.scheduled)(arrayOfPromises, rxjs_1.asyncScheduler)
        .pipe((0, index_js_1.mergeAll)(), (0, index_js_1.scan)(acc => acc + 1, 0), (0, index_js_1.map)(progress => progress / count * 100), (0, index_js_1.tap)(onProgressCallback))
        .toPromise();
}
//# sourceMappingURL=move.js.map