"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPatchDetailsFromCliString = exports.getPackageDetailsFromPatchFilename = exports.parseNameAndVersion = void 0;
const path_1 = require("./path");
function parseNameAndVersion(str) {
    const parts = str
        .split("+")
        .map((s) => s.trim())
        .filter(Boolean);
    if (parts.length === 0) {
        return null;
    }
    if (parts.length === 1) {
        return { packageName: str };
    }
    const versionIndex = parts.findIndex((part) => part.match(/^\d+\.\d+\.\d+.*$/));
    if (versionIndex === -1) {
        const [scope, name] = parts;
        return { packageName: `${scope}/${name}` };
    }
    const nameParts = parts.slice(0, versionIndex);
    let packageName;
    switch (nameParts.length) {
        case 0:
            return null;
        case 1:
            packageName = nameParts[0];
            break;
        case 2:
            const [scope, name] = nameParts;
            packageName = `${scope}/${name}`;
            break;
        default:
            return null;
    }
    const version = parts[versionIndex];
    const sequenceParts = parts.slice(versionIndex + 1);
    if (sequenceParts.length === 0) {
        return { packageName, version };
    }
    // expect sequenceParts[0] to be a number, strip leading 0s
    const sequenceNumber = parseInt(sequenceParts[0].replace(/^0+/, ""), 10);
    if (isNaN(sequenceNumber)) {
        return null;
    }
    switch (sequenceParts.length) {
        case 1: {
            return { packageName, version, sequenceNumber };
        }
        case 2: {
            return {
                packageName,
                version,
                sequenceName: sequenceParts[1],
                sequenceNumber,
            };
        }
        default: {
            return null;
        }
    }
    return null;
}
exports.parseNameAndVersion = parseNameAndVersion;
function getPackageDetailsFromPatchFilename(patchFilename) {
    const parts = patchFilename
        .replace(/(\.dev)?\.patch$/, "")
        .split("++")
        .map(parseNameAndVersion)
        .filter((x) => x !== null);
    if (parts.length === 0) {
        return null;
    }
    const lastPart = parts[parts.length - 1];
    if (!lastPart.version) {
        return null;
    }
    return {
        name: lastPart.packageName,
        version: lastPart.version,
        path: path_1.join("node_modules", parts.map(({ packageName: name }) => name).join("/node_modules/")),
        patchFilename,
        pathSpecifier: parts.map(({ packageName: name }) => name).join("/"),
        humanReadablePathSpecifier: parts
            .map(({ packageName: name }) => name)
            .join(" => "),
        isNested: parts.length > 1,
        packageNames: parts.map(({ packageName: name }) => name),
        isDevOnly: patchFilename.endsWith(".dev.patch"),
        sequenceName: lastPart.sequenceName,
        sequenceNumber: lastPart.sequenceNumber,
    };
}
exports.getPackageDetailsFromPatchFilename = getPackageDetailsFromPatchFilename;
function getPatchDetailsFromCliString(specifier) {
    const parts = specifier.split("/");
    const packageNames = [];
    let scope = null;
    for (let i = 0; i < parts.length; i++) {
        if (parts[i].startsWith("@")) {
            if (scope) {
                return null;
            }
            scope = parts[i];
        }
        else {
            if (scope) {
                packageNames.push(`${scope}/${parts[i]}`);
                scope = null;
            }
            else {
                packageNames.push(parts[i]);
            }
        }
    }
    const path = path_1.join("node_modules", packageNames.join("/node_modules/"));
    return {
        packageNames,
        path,
        name: packageNames[packageNames.length - 1],
        humanReadablePathSpecifier: packageNames.join(" => "),
        isNested: packageNames.length > 1,
        pathSpecifier: specifier,
    };
}
exports.getPatchDetailsFromCliString = getPatchDetailsFromCliString;
//# sourceMappingURL=data:application/json;base64,[encoded data, 10492 characters]