import { nowIso, readJsonFile, withFileLock, writeJsonFile } from "../../shared/index.js";
export async function mutateRevisionedJsonFile(file, mutate, options = {}) {
    return withFileLock(file, async () => {
        const current = await readJsonFile(file);
        const timestamp = options.timestamp ?? nowIso();
        const next = await mutate(current);
        const revisioned = {
            ...next,
            state_revision: nextStateRevision(current?.state_revision),
            updated_at: timestamp,
        };
        await writeJsonFile(file, revisioned);
        return revisioned;
    });
}
export function nextStateRevision(currentRevision) {
    return typeof currentRevision === "number" && Number.isInteger(currentRevision) && currentRevision > 0
        ? currentRevision + 1
        : 1;
}
//# sourceMappingURL=mutation.js.map