import { readFile, rm, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
const CURRENT_VERSION_MARKER_FILE = 'current.version';
const PREVIOUS_VERSION_MARKER_FILE = 'previous.version';
function resolveMarkerPath(layout, markerFileName) {
    return join(layout.installRoot, markerFileName);
}
async function readOptionalTrimmedFile(path) {
    try {
        const value = (await readFile(path, 'utf8')).trim();
        return value.length > 0 ? value : null;
    }
    catch {
        return null;
    }
}
export async function readInstalledVersionMarkers(layout) {
    return {
        currentVersionId: await readOptionalTrimmedFile(resolveMarkerPath(layout, CURRENT_VERSION_MARKER_FILE)),
        previousVersionId: await readOptionalTrimmedFile(resolveMarkerPath(layout, PREVIOUS_VERSION_MARKER_FILE)),
    };
}
export async function writeInstalledVersionMarker(params) {
    const markerPath = resolveMarkerPath(params.layout, params.marker === 'current' ? CURRENT_VERSION_MARKER_FILE : PREVIOUS_VERSION_MARKER_FILE);
    if (!params.versionId) {
        await rm(markerPath, { force: true });
        return;
    }
    await writeFile(markerPath, `${params.versionId}\n`, 'utf8');
}
//# sourceMappingURL=versionMarkers.js.map