import { promises as fs } from "node:fs";
import path from "node:path";
import { loadConfig, writeConfig } from "../config.js";
import { getBackend } from "../agents/index.js";
import { ensureReflexIgnoreTemplate, snapshotScope } from "../kb.js";
import { reflexRoot } from "../paths.js";
export async function runInit(dirInput, options = {}) {
    const root = path.resolve(dirInput);
    await assertDir(root);
    await fs.mkdir(reflexRoot(root), { recursive: true });
    const cfg = await loadConfig(root);
    await writeConfig(root, cfg); // persist normalized config
    await ensureReflexIgnoreTemplate(root);
    if (options.scaffoldOnly) {
        process.stdout.write(`Reflex scaffolded at ${reflexRoot(root)} (scaffold-only).\n`);
        return;
    }
    const effectiveCfg = options.harness
        ? { ...cfg, agentBackend: options.harness }
        : cfg;
    const snapshot = await snapshotScope({ root, scope: root });
    const scopeWithOverrides = {
        ...snapshot,
        ...(options.model ? { model: options.model } : {}),
        ...(options.language ? { language: options.language } : {}),
    };
    process.stdout.write(`Running ${effectiveCfg.agentBackend}${options.model ? ` (${options.model})` : ""}${options.language ? ` [lang=${options.language}]` : ""} over ${root} (${snapshot.files.length} files visible)…\n`);
    const backend = getBackend(effectiveCfg);
    await backend.analyzeScope(scopeWithOverrides);
    process.stdout.write(`Done. KB written under ${reflexRoot(root)}\n`);
}
async function assertDir(p) {
    const stat = await fs.stat(p).catch(() => null);
    if (!stat || !stat.isDirectory()) {
        throw new Error(`Not a directory: ${p}`);
    }
}
//# sourceMappingURL=init.js.map