/**
 * File-backed runtime workspace repository.
 *
 * Owns catalog paths and JSON file I/O. Workspace semantics live in
 * `RuntimeWorkspaceService`; callers should not read or write workspace catalog
 * files directly.
 */
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import { WorkspaceCatalogSchema } from './schemas.js';
export class FileWorkspaceRepository {
    catalogPath;
    constructor(args) {
        this.catalogPath = args.catalogPath;
    }
    static resolveCatalogPath(stateRoot) {
        return join(stateRoot, 'workspaces.catalog.json');
    }
    exists() {
        return existsSync(this.catalogPath);
    }
    readRaw() {
        return JSON.parse(readFileSync(this.catalogPath, 'utf8'));
    }
    save(catalog) {
        mkdirSync(dirname(this.catalogPath), { recursive: true });
        writeFileSync(this.catalogPath, `${JSON.stringify(WorkspaceCatalogSchema.parse(catalog), null, 2)}\n`, 'utf8');
    }
    path() {
        return this.catalogPath;
    }
    static forStateRoot(stateRoot) {
        return new FileWorkspaceRepository({
            catalogPath: FileWorkspaceRepository.resolveCatalogPath(resolve(stateRoot)),
        });
    }
}
//# sourceMappingURL=repository.js.map