"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGroupedPatches = exports.getPatchFiles = void 0;
const PackageDetails_1 = require("./PackageDetails");
const path_1 = require("./path");
const klaw_sync_1 = __importDefault(require("klaw-sync"));
const getPatchFiles = (patchesDir) => {
    try {
        return klaw_sync_1.default(patchesDir, { nodir: true })
            .map(({ path }) => path_1.relative(patchesDir, path))
            .filter((path) => path.endsWith(".patch"));
    }
    catch (e) {
        return [];
    }
};
exports.getPatchFiles = getPatchFiles;
const getGroupedPatches = (patchesDirectory) => {
    const files = exports.getPatchFiles(patchesDirectory);
    if (files.length === 0) {
        return {
            numPatchFiles: 0,
            pathSpecifierToPatchFiles: {},
            warnings: [],
        };
    }
    const warnings = [];
    const pathSpecifierToPatchFiles = {};
    for (const file of files) {
        const details = PackageDetails_1.getPackageDetailsFromPatchFilename(file);
        if (!details) {
            warnings.push(`Unrecognized patch file in patches directory ${file}`);
            continue;
        }
        if (!pathSpecifierToPatchFiles[details.pathSpecifier]) {
            pathSpecifierToPatchFiles[details.pathSpecifier] = [];
        }
        pathSpecifierToPatchFiles[details.pathSpecifier].push(details);
    }
    for (const arr of Object.values(pathSpecifierToPatchFiles)) {
        arr.sort((a, b) => {
            var _a, _b;
            return ((_a = a.sequenceNumber) !== null && _a !== void 0 ? _a : 0) - ((_b = b.sequenceNumber) !== null && _b !== void 0 ? _b : 0);
        });
    }
    return {
        numPatchFiles: files.length,
        pathSpecifierToPatchFiles,
        warnings,
    };
};
exports.getGroupedPatches = getGroupedPatches;
//# sourceMappingURL=data:application/json;base64,[encoded data, 4296 characters]