/**
 * Facts-layer scoping — restrict structural facts to files under the named
 * top-level subdirs. Used when ATTACHING to a full (un-scoped) standard index
 * but the caller passed `--scope`: the index has the whole repo, so we keep
 * only in-scope facts at extraction time (rather than re-indexing a subset).
 *
 * File-bearing facts (entities/views/routes/candidateMethods/…) are kept when
 * their path's first segment is in scope; name-referencing facts
 * (relationships/lifecycles/suggestedComponents) are kept only when their
 * referenced entity survives.
 */
import { resolve } from 'path';
/** Mutates + returns `facts`, keeping only items under the scoped subtrees. */
export function scopeFilterFacts(facts, scope, sourceDir) {
    const scopes = scope.map((s) => s.replace(/^[./]+|\/+$/g, '')).filter(Boolean);
    if (scopes.length === 0)
        return facts;
    const abs = resolve(sourceDir);
    const inScope = (filePath) => {
        if (!filePath)
            return true; // no path → can't filter; keep
        let rel = filePath;
        if (rel.startsWith(abs + '/'))
            rel = rel.slice(abs.length + 1);
        rel = rel.replace(/^\.?\//, '');
        return scopes.includes(rel.split('/')[0]);
    };
    facts.entities = facts.entities.filter((e) => inScope(e.filePath));
    const surviving = new Set(facts.entities.map((e) => e.name));
    // Name-referencing facts: keep only when their entity/model survives.
    facts.relationships = facts.relationships.filter((r) => surviving.has(r.from) && surviving.has(r.to));
    facts.lifecycles = facts.lifecycles.filter((l) => surviving.has(l.model));
    if (facts.suggestedComponents) {
        facts.suggestedComponents = facts.suggestedComponents
            .map((c) => ({ ...c, entities: (c.entities ?? []).filter((n) => surviving.has(n)) }))
            .filter((c) => (c.entities ?? []).length > 0);
    }
    // File-bearing facts: keep when their path is in scope.
    if (facts.views)
        facts.views = facts.views.filter((v) => inScope(v.filePath));
    if (facts.routes)
        facts.routes = facts.routes.filter((r) => inScope(r.filePath));
    if (facts.candidateMethods)
        facts.candidateMethods = facts.candidateMethods.filter((c) => inScope(c.filePath));
    if (facts.implementsRelationships)
        facts.implementsRelationships = facts.implementsRelationships.filter((i) => inScope(i.classFilePath));
    if (facts.constSchemas)
        facts.constSchemas = facts.constSchemas.filter((c) => inScope(c.filePath));
    if (facts.methodFactSheets)
        facts.methodFactSheets = facts.methodFactSheets.filter((m) => inScope(m.filePath));
    return facts;
}
//# sourceMappingURL=facts-scope.js.map