import { readFile } from "node:fs/promises";
import { join } from "node:path";
const MAX_DOC_BYTES = 64 * 1024;
const COMMAND_PATTERN = /(?:npm run|pnpm|yarn|cargo|go test|go run|python|pytest)\s+[a-zA-Z0-9:_./-]+/g;
export async function discoverDocsSemantics(cwd, inventory) {
    const docs = inventory.documents
        .filter((document) => isUsefulDoc(document.path) && document.sizeBytes <= MAX_DOC_BYTES)
        .sort((a, b) => docPriority(a.path) - docPriority(b.path))
        .slice(0, 16);
    const architectureHints = [];
    const validationStrategy = [];
    const risks = [];
    const unknowns = [];
    let projectPurpose;
    for (const doc of docs) {
        const text = await safeReadFile(join(cwd, doc.path));
        if (!text)
            continue;
        projectPurpose ??= extractPurpose(text, doc.path);
        architectureHints.push(...extractArchitectureHints(text, doc.path));
        validationStrategy.push(...extractCommandHints(text, doc.path));
        risks.push(...extractRiskHints(text, doc.path));
    }
    if (!projectPurpose) {
        unknowns.push({
            value: "No README/docs purpose statement found.",
            confidence: "high",
            sources: docs.map((doc) => doc.path),
        });
    }
    return { projectPurpose, architectureHints, validationStrategy, risks, unknowns };
}
function isUsefulDoc(path) {
    if (/^docs\/adr\/\d+-threadsuite-/i.test(path))
        return false;
    return /(^README\.md$|PROJECT_PLAN\.md$|.*_PLAN\.md$|CONTRIBUTING\.md$|CONTEXT\.md$|STATE\.md$|ACTIVE_QUEUE\.md$|AUTONOMOUS_EXECUTION\.md$|^docs\/.*\.md$)/i.test(path);
}
function docPriority(path) {
    if (/^README\.md$/i.test(path))
        return 0;
    if (/PROJECT_PLAN\.md$/i.test(path))
        return 1;
    if (/.*_PLAN\.md$/i.test(path))
        return 2;
    if (/^docs\//i.test(path) && !/^docs\/adr\//i.test(path))
        return 3;
    if (/^CONTEXT\.md$/i.test(path))
        return 4;
    if (/^docs\/adr\//i.test(path))
        return 8;
    return 5;
}
async function safeReadFile(path) {
    try {
        return await readFile(path, "utf-8");
    }
    catch {
        return undefined;
    }
}
function extractPurpose(text, source) {
    const lines = text
        .split(/\r?\n/)
        .map((line) => line.trim())
        .filter((line) => line && !line.startsWith("---") && !line.startsWith("<!--"));
    const heading = lines.find((line) => line.startsWith("# "))?.replace(/^#\s+/, "");
    const paragraph = lines.find((line) => !line.startsWith("#") && line.length > 30 && !line.startsWith("- ") && !line.includes("ThreadSuite"));
    const value = paragraph ?? (heading ? `Project documented as ${heading}.` : undefined);
    return value
        ? { value: value.slice(0, 500), confidence: paragraph ? "medium" : "low", sources: [source] }
        : undefined;
}
function extractArchitectureHints(text, source) {
    return extractLines(text, /\b(architecture|module|package|crate|workspace|cli|server|client|api|plugin|extension)\b/i, source)
        .slice(0, 8)
        .map((value) => ({ value, confidence: "medium", sources: [source] }));
}
function extractCommandHints(text, source) {
    const commands = Array.from(new Set(text.match(COMMAND_PATTERN) ?? [])).slice(0, 12);
    return commands.map((command) => ({
        value: `Documented command: ${command}`,
        confidence: "medium",
        sources: [source],
    }));
}
function extractRiskHints(text, source) {
    return extractLines(text, /\b(risk|warning|caution|todo|fixme|limitation|known issue|blocker)\b/i, source)
        .slice(0, 8)
        .map((value) => ({ value, confidence: "medium", sources: [source] }));
}
function extractLines(text, pattern, _source) {
    return text
        .split(/\r?\n/)
        .map((line) => line.trim().replace(/^[-*]\s+/, ""))
        .filter((line) => line.length > 10 && line.length < 300 && pattern.test(line));
}
//# sourceMappingURL=docs-reader.js.map