/**
 * Thin reader for .cleo/project-info.json.
 *
 * The file is written by scaffold.ts (ensureProjectInfo). This module
 * provides a typed read interface for consumers that need projectHash
 * and projectId without importing the full scaffold machinery.
 *
 * @task T5333
 */
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { getCleoDirAbsolute } from './paths.js';
// ── Implementation ───────────────────────────────────────────────────
/**
 * Read project-info.json and return a typed ProjectInfo.
 *
 * Falls back gracefully when projectId is missing (pre-T5333 installs)
 * by returning an empty string, allowing callers to detect and handle.
 *
 * @throws {Error} If .cleo/project-info.json does not exist or is invalid JSON.
 */
export async function getProjectInfo(cwd) {
    const projectRoot = cwd ?? process.cwd();
    const cleoDir = getCleoDirAbsolute(projectRoot);
    const infoPath = join(cleoDir, 'project-info.json');
    const raw = await readFile(infoPath, 'utf-8');
    const data = JSON.parse(raw);
    if (typeof data.projectHash !== 'string' || data.projectHash.length === 0) {
        throw new Error(`project-info.json missing required field: projectHash`);
    }
    const segments = projectRoot.replace(/[\\/]+$/, '').split(/[\\/]/);
    const projectName = segments[segments.length - 1] ?? 'unknown';
    return {
        projectHash: data.projectHash,
        projectId: typeof data.projectId === 'string' ? data.projectId : '',
        projectRoot,
        projectName,
    };
}
/**
 * Synchronous variant for use in hot paths where async is not feasible.
 * Returns null if the file is missing or unparseable.
 */
export function getProjectInfoSync(cwd) {
    const projectRoot = cwd ?? process.cwd();
    const cleoDir = getCleoDirAbsolute(projectRoot);
    const infoPath = join(cleoDir, 'project-info.json');
    if (!existsSync(infoPath))
        return null;
    try {
        const raw = readFileSync(infoPath, 'utf-8');
        const data = JSON.parse(raw);
        if (typeof data.projectHash !== 'string' || data.projectHash.length === 0) {
            return null;
        }
        const segments = projectRoot.replace(/[\\/]+$/, '').split(/[\\/]/);
        const projectName = segments[segments.length - 1] ?? 'unknown';
        return {
            projectHash: data.projectHash,
            projectId: typeof data.projectId === 'string' ? data.projectId : '',
            projectRoot,
            projectName,
        };
    }
    catch {
        return null;
    }
}
/**
 * Update the project name in project-info.json.
 * Used by `cleo upgrade --name` and programmatic consumers.
 */
export function updateProjectName(cwd, name) {
    const cleoDir = getCleoDirAbsolute(cwd);
    const infoPath = join(cleoDir, 'project-info.json');
    if (!existsSync(infoPath))
        return;
    const data = JSON.parse(readFileSync(infoPath, 'utf-8'));
    data.projectName = name;
    data.lastUpdated = new Date().toISOString();
    writeFileSync(infoPath, `${JSON.stringify(data, null, 2)}\n`);
}
//# sourceMappingURL=project-info.js.map