"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const rrdir_1 = __importDefault(require("rrdir"));
class Scanner {
    static async *scan(root, minSize, maxSize) {
        let currentList = [];
        let currentSize = 0;
        for await (const entry of (0, rrdir_1.default)(root, {
            stats: true, followSymlinks: true
        })) {
            if (entry.directory) {
                continue;
            }
            if (entry.err) {
                throw entry.err;
            }
            const newSize = currentSize + entry.stats.size;
            if (newSize <= maxSize) {
                currentList.push({
                    size: entry.stats.size,
                    path: entry.path
                });
                currentSize = newSize;
                if (newSize >= minSize) {
                    yield currentList;
                    currentList = [];
                    currentSize = 0;
                }
            }
            else {
                let remaining = entry.stats.size;
                do {
                    let splitSize = minSize - currentSize;
                    if (splitSize > remaining) {
                        splitSize = remaining;
                    }
                    currentList.push({
                        size: entry.stats.size,
                        start: entry.stats.size - remaining,
                        end: entry.stats.size - remaining + splitSize,
                        path: entry.path
                    });
                    currentSize += splitSize;
                    remaining -= splitSize;
                    if (currentSize >= minSize) {
                        yield currentList;
                        currentList = [];
                        currentSize = 0;
                    }
                } while (remaining > 0);
            }
        }
        if (currentList.length > 0) {
            yield currentList;
        }
    }
}
exports.default = Scanner;
//# sourceMappingURL=Scanner.js.map