import { getClassHierarchy } from '../db/index.js';
/**
 * Resolve all methods in the class hierarchy that share the given method name.
 *
 * Accepts either a raw BetterSqlite3Database handle (legacy) or a Repository
 * instance (preferred — works with both SqliteRepository and NativeRepository).
 */
export function resolveMethodViaHierarchy(dbOrRepo, methodName) {
    // Detect Repository vs raw DB by duck-typing on findNodesWithFanIn
    if (typeof dbOrRepo.findNodesWithFanIn === 'function' &&
        typeof dbOrRepo.getClassHierarchy === 'function') {
        return resolveViaRepo(dbOrRepo, methodName);
    }
    return resolveViaRawDb(dbOrRepo, methodName);
}
/** Strip fan_in from NodeRowWithFanIn to produce a plain NodeRow. */
function stripFanIn(rows) {
    return rows.map(({ fan_in: _, ...rest }) => rest);
}
function resolveViaRepo(repo, methodName) {
    const methods = stripFanIn(repo.findNodesWithFanIn(`%.${methodName}`, { kinds: ['method'] }));
    const results = [...methods];
    for (const m of methods) {
        const className = m.name.split('.')[0];
        const classNodes = repo.findNodesWithFanIn(className, {
            kinds: ['class'],
            file: m.file,
        });
        const classNode = classNodes[0];
        if (!classNode)
            continue;
        const ancestors = repo.getClassHierarchy(classNode.id);
        for (const ancestorId of ancestors) {
            const ancestor = repo.findNodeById(ancestorId);
            if (!ancestor)
                continue;
            const parentMethods = stripFanIn(repo.findNodesWithFanIn(`${ancestor.name}.${methodName}`, { kinds: ['method'] }));
            results.push(...parentMethods);
        }
    }
    return results;
}
function resolveViaRawDb(db, methodName) {
    const methods = db
        .prepare(`SELECT * FROM nodes WHERE kind = 'method' AND name LIKE ?`)
        .all(`%.${methodName}`);
    const results = [...methods];
    for (const m of methods) {
        const className = m.name.split('.')[0];
        const classNode = db
            .prepare(`SELECT * FROM nodes WHERE name = ? AND kind = 'class' AND file = ?`)
            .get(className, m.file);
        if (!classNode)
            continue;
        const ancestors = getClassHierarchy(db, classNode.id);
        for (const ancestorId of ancestors) {
            const ancestor = db.prepare('SELECT name FROM nodes WHERE id = ?').get(ancestorId);
            if (!ancestor)
                continue;
            const parentMethods = db
                .prepare(`SELECT * FROM nodes WHERE name = ? AND kind = 'method'`)
                .all(`${ancestor.name}.${methodName}`);
            results.push(...parentMethods);
        }
    }
    return results;
}
//# sourceMappingURL=hierarchy.js.map