import { getMemoryState } from "../branch.js";
import { observationPoolTokens as estimateObservationPoolTokens, observationsToMarkdownList, reflectionsToMarkdownList, } from "../compaction.js";
import { countByRelevance, formatRelevanceHistogram } from "../relevance.js";
import { estimateStringTokens } from "../tokens.js";
import { reflectionContent } from "../types.js";
export function registerViewCommand(ext, runtime) {
    ext.registerCommand("om-view", {
        description: "Print observational memory details (reflections + observations)",
        handler: async (_args, ctx) => {
            runtime.ensureConfig(ctx.cwd);
            const entries = ctx.sessionManager.getBranch();
            const { reflections: committedRefs, committedObs, pendingObs } = getMemoryState(entries);
            const committedRefItems = committedRefs;
            const committedRefTokens = committedRefItems.reduce((s, r) => s + estimateStringTokens(reflectionContent(r)), 0);
            const committedRefCount = committedRefItems.length;
            const committedObsTokens = estimateObservationPoolTokens(committedObs);
            const committedObsCount = committedObs.length;
            const pendingObsTokens = estimateObservationPoolTokens(pendingObs);
            const pendingObsCount = pendingObs.length;
            const totalObsCount = committedObsCount + pendingObsCount;
            const totalObsTokens = estimateObservationPoolTokens([...committedObs, ...pendingObs]);
            const totalTokens = committedRefTokens + totalObsTokens;
            const relevanceHistogram = countByRelevance([...committedObs, ...pendingObs]);
            const plural = (n, singular, plural) => (n === 1 ? singular : plural);
            const sections = [];
            sections.push(`## Memory Overview\n\n` +
                `${committedRefCount} ${plural(committedRefCount, "reflection", "reflections")} · ` +
                `${totalObsCount} ${plural(totalObsCount, "observation", "observations")} ` +
                `(${committedObsCount} committed, ${pendingObsCount} pending) · ` +
                `~${totalTokens.toLocaleString()} tokens · ` +
                `relevance ${formatRelevanceHistogram(relevanceHistogram)}`);
            sections.push(`## Reflections (${committedRefCount} ${plural(committedRefCount, "entry", "entries")}, ~${committedRefTokens.toLocaleString()} tokens)`);
            sections.push("");
            if (committedRefItems.length > 0) {
                sections.push(reflectionsToMarkdownList(committedRefItems));
            }
            else {
                sections.push("*(none)*");
            }
            sections.push("");
            sections.push(`## Observations (committed: ${committedObsCount} ${plural(committedObsCount, "observation", "observations")}, ~${committedObsTokens.toLocaleString()} tokens)`);
            sections.push("");
            if (committedObs.length > 0) {
                sections.push(observationsToMarkdownList(committedObs));
            }
            else {
                sections.push("*(none)*");
            }
            sections.push("");
            sections.push(`## Observations (pending: ${pendingObsCount} ${plural(pendingObsCount, "observation", "observations")}, ~${pendingObsTokens.toLocaleString()} tokens)`);
            sections.push("");
            if (pendingObs.length > 0) {
                sections.push(observationsToMarkdownList(pendingObs));
            }
            else {
                sections.push("*(none)*");
            }
            sections.push("");
            sections.push("*Tip: use /tree to browse the raw messages still live in the session.*");
            ctx.ui.notify(sections.join("\n"), "info");
        },
    });
}
