export function parseRevertSelector(input) {
    const trimmed = input.trim();
    if (!trimmed)
        throw new Error("Usage: /revert <id>, /revert last, /revert last N, or /revert A..B.");
    const lastMatch = trimmed.match(/^last(?:\s+(\d+))?$/iu);
    if (lastMatch) {
        const count = lastMatch[1] ? Number(lastMatch[1]) : 1;
        if (!Number.isInteger(count) || count <= 0)
            throw new Error("Expected /revert last N with N greater than zero.");
        return { type: "last", count };
    }
    const rangeMatch = trimmed.match(/^(\d+)\.\.(\d+)$/u);
    if (rangeMatch) {
        const from = Number(rangeMatch[1]);
        const to = Number(rangeMatch[2]);
        if (from > to)
            throw new Error("Revert ranges must be ascending, for example /revert 3..7.");
        return { type: "range", from, to };
    }
    const id = Number(trimmed);
    if (Number.isInteger(id) && id > 0)
        return { type: "id", id };
    throw new Error("Usage: /revert <id>, /revert last, /revert last N, or /revert A..B.");
}
export function resolveRevertSelection(allChangeSets, selector) {
    const sorted = [...allChangeSets].sort((a, b) => a.id - b.id);
    let selected;
    if (selector.type === "id") {
        selected = sorted.filter((changeSet) => changeSet.id === selector.id);
    }
    else if (selector.type === "last") {
        selected = sorted.filter((changeSet) => changeSet.status === "active").slice(-selector.count);
    }
    else {
        selected = sorted.filter((changeSet) => changeSet.id >= selector.from && changeSet.id <= selector.to);
        const expectedCount = selector.to - selector.from + 1;
        if (selected.length !== expectedCount)
            throw new Error("Revert range is not contiguous in the change-set timeline.");
    }
    if (!selected.length)
        throw new Error("No matching change sets found to revert.");
    const inactive = selected.find((changeSet) => changeSet.status !== "active");
    if (inactive)
        throw new Error(`Change set #${inactive.id} is already ${inactive.status}.`);
    return { selector, changeSets: selected.sort((a, b) => b.id - a.id) };
}
//# sourceMappingURL=revert-selection.js.map