import * as fs from 'node:fs';
import * as path from 'node:path';
import { getConfigTier } from './config-schema.js';
/**
 * Gather project description from filesystem — no LLM, no embedder, fast.
 * Requires a pre-loaded config and the resolved config root directory.
 */
export function describeProject(config, configRoot) {
    const totemDir = path.join(configRoot, config.totemDir);
    // Project name + description from package.json
    let project = path.basename(configRoot);
    let description;
    try {
        const pkgPath = path.join(configRoot, 'package.json');
        if (fs.existsSync(pkgPath)) {
            const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
            if (pkg.name)
                project = pkg.name;
            if (pkg.description)
                description = pkg.description;
        }
    }
    catch {
        // package.json missing or malformed — use directory name
    }
    const tier = getConfigTier(config);
    // Rule count from compiled rules. `compiled-rules.json` is an object of
    // shape `{ version, rules: CompiledRule[], nonCompilable: [...] }` — NOT
    // an array. Reading `parsed.rules.length` is the canonical access
    // pattern used by `loadCompiledRulesFile` and the rest of the codebase
    // (mmnto-ai/totem#1884 R1 — prior `Array.isArray(parsed)` check always
    // failed, reporting 0 rules in every orientation banner).
    let rules = 0;
    try {
        const rulesPath = path.join(totemDir, 'compiled-rules.json');
        if (fs.existsSync(rulesPath)) {
            const parsed = JSON.parse(fs.readFileSync(rulesPath, 'utf-8'));
            if (parsed && typeof parsed === 'object' && Array.isArray(parsed.rules)) {
                rules = parsed.rules.length;
            }
        }
    }
    catch {
        // compiled-rules.json missing or malformed
    }
    // Lesson count
    let lessons = 0;
    try {
        const lessonsDir = path.join(totemDir, 'lessons');
        if (fs.existsSync(lessonsDir)) {
            lessons = fs.readdirSync(lessonsDir).filter((f) => f.endsWith('.md')).length;
        }
    }
    catch {
        // lessons dir missing or unreadable
    }
    const targets = config.targets.map((t) => `${t.glob} (${t.type}/${t.strategy})`);
    const partitions = config.partitions ?? {};
    // Git hooks
    const hooks = [];
    try {
        const hooksDir = path.join(configRoot, '.git', 'hooks');
        if (fs.existsSync(hooksDir)) {
            for (const file of fs.readdirSync(hooksDir)) {
                if (file.endsWith('.sample'))
                    continue;
                const stat = fs.statSync(path.join(hooksDir, file));
                if (stat.isFile())
                    hooks.push(file);
            }
        }
    }
    catch {
        // .git/hooks unreadable
    }
    return { project, description, tier, rules, lessons, targets, partitions, hooks };
}
//# sourceMappingURL=describe.js.map