import path from "node:path";
import { auditClaims as auditClaimsFree } from "./audit.js";
import { explainPage as explainPageFree } from "./explain.js";
import { ingest as ingestFree } from "./ingest.js";
import { listPages, readPage } from "./pages.js";
import { queryMemory } from "./query.js";
import { searchMemory } from "./search.js";
import { statusOf } from "./status.js";
import { computeTokenStats } from "./tokens.js";
import { appendToPage, clearMemory, writePage } from "./write.js";
export function openMemory(opts) {
    if (!path.isAbsolute(opts.root)) {
        throw new Error(`openMemory: root must be absolute, got ${opts.root}`);
    }
    const root = opts.root;
    const defaultAgent = opts.agent;
    return {
        root,
        listPages: async () => await listPages(root),
        readPage: async (relPath) => await readPage(root, relPath),
        searchMemory: async (query) => await searchMemory(root, query),
        statusOf: async () => await statusOf(root),
        computeTokenStats: async () => await computeTokenStats(root),
        explainPage: async (options) => await explainPageFree(root, withDefaultAgent(options, defaultAgent)),
        writePage: async (relPath, body, options) => await writePage(root, relPath, body, options),
        appendToPage: async (relPath, content, options) => await appendToPage(root, relPath, content, options),
        clearMemory: async () => await clearMemory(root),
        query: async (options) => await queryMemory(root, withDefaultAgent(options, defaultAgent)),
        ingest: async (options) => await ingestFree(root, withDefaultAgent(options, defaultAgent)),
        auditClaims: async ({ repoRoot, ...options }) => await auditClaimsFree(root, repoRoot, options)
    };
}
function withDefaultAgent(options, agent) {
    if (options.agent !== undefined || agent === undefined) {
        return options;
    }
    return {
        ...options,
        agent
    };
}
