/**
 * Agent-facing context aggregator.
 *
 * Combines work meta + focus snapshot + composition summary into a single
 * JSON shape the CLI / chat / terminal surfaces can all read. H0.4 will
 * extend this with trends + profile fields.
 */
import { join } from "node:path";
import { homedir } from "node:os";
import { readFile } from "node:fs/promises";
import { read as readFocus } from "../focus/index.js";
import { CompositionSchema } from "../shared/composition.js";
function worksRoot() {
    return process.env.AUTOVIRAL_WORKS_ROOT ?? join(homedir(), ".autoviral/works");
}
async function readCompositionSummary(workId) {
    const path = join(worksRoot(), workId, "composition.yaml");
    try {
        const raw = await readFile(path, "utf-8");
        // composition.yaml is YAML on disk; for the summary we just need rough
        // counts. Parse via the existing JSON-or-YAML helper if present,
        // otherwise skip. Keep this best-effort — context endpoint must never
        // fail because of a malformed composition file.
        const parsed = JSON.parse(raw); // try JSON first (some works are JSON)
        const comp = CompositionSchema.safeParse(parsed);
        if (!comp.success)
            return null;
        return summarize(comp.data);
    }
    catch {
        return null;
    }
}
function summarize(comp) {
    return {
        duration: comp.duration,
        trackCount: comp.tracks.length,
        captionCount: comp.captions?.segments.length ?? 0,
        hasVariables: (comp.variables?.length ?? 0) > 0,
    };
}
export async function getContext(workId, opts = {}) {
    const focus = readFocus(workId);
    const composition = await readCompositionSummary(workId);
    // Trends + profile are opt-in so the default context call stays fast.
    // CLI `autoviral context` enables them by default; `autoviral trends` /
    // `autoviral profile` hit dedicated endpoints for finer-grained control.
    const result = { workId, focus, composition };
    if (opts.includeProfile) {
        const { getProfile } = await import("./profile.js");
        result.profile = await getProfile();
    }
    if (opts.includeTrends) {
        const { getTrends } = await import("./trends.js");
        result.trends = await getTrends();
    }
    return result;
}
export { getProfile, clearProfileCache } from "./profile.js";
export { getTrends } from "./trends.js";
//# sourceMappingURL=index.js.map