"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.KnowledgeSiloDetector = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class KnowledgeSiloDetector {
    siloPath;
    constructor(cellDir) {
        this.siloPath = path.join(cellDir, 'silos');
        this.ensureDirectory(this.siloPath);
    }
    analyzeModule(module, contributors, totalContributors) {
        const contributorRatio = contributors.length / Math.max(1, totalContributors);
        const busFactor = contributors.length;
        let riskScore = 0;
        if (contributorRatio <= 0.1)
            riskScore = 0.9;
        else if (contributorRatio <= 0.2)
            riskScore = 0.7;
        else if (contributorRatio <= 0.3)
            riskScore = 0.5;
        else if (contributorRatio <= 0.5)
            riskScore = 0.3;
        else
            riskScore = 0.1;
        let recommendation = '';
        if (riskScore >= 0.7) {
            recommendation = `CRITICAL: ${module} has only ${busFactor} contributor(s). Generate docs + suggest pair-programming immediately.`;
        }
        else if (riskScore >= 0.5) {
            recommendation = `WARNING: ${module} has low bus factor (${busFactor}). Schedule knowledge sharing session.`;
        }
        else if (riskScore >= 0.3) {
            recommendation = `NOTE: ${module} could benefit from more contributors. Consider rotating team members.`;
        }
        else {
            recommendation = `OK: ${module} has healthy contributor distribution.`;
        }
        return { module, riskScore, contributors, busFactor, recommendation };
    }
    analyzeProject(moduleContributors) {
        const allContributors = new Set();
        for (const contributors of Object.values(moduleContributors)) {
            for (const c of contributors)
                allContributors.add(c);
        }
        const reports = [];
        for (const [module, contributors] of Object.entries(moduleContributors)) {
            reports.push(this.analyzeModule(module, contributors, allContributors.size));
        }
        return reports.sort((a, b) => b.riskScore - a.riskScore);
    }
    getGenerateDocsAction(module) {
        return `Generate comprehensive documentation for ${module} including architecture overview, setup guide, and common workflows.`;
    }
    getPairProgrammingAction(module) {
        return `Suggest pair-programming session for ${module} with a team member who hasn't worked on it.`;
    }
    getTopRisks(reports, threshold = 0.5) {
        return reports.filter((r) => r.riskScore >= threshold);
    }
    saveReport(reports) {
        const reportPath = path.join(this.siloPath, 'report.json');
        fs.writeFileSync(reportPath, JSON.stringify(reports, null, 2), 'utf-8');
    }
    loadReport() {
        const reportPath = path.join(this.siloPath, 'report.json');
        if (!fs.existsSync(reportPath))
            return [];
        try {
            return JSON.parse(fs.readFileSync(reportPath, 'utf-8'));
        }
        catch {
            return [];
        }
    }
    ensureDirectory(dir) {
        if (!fs.existsSync(dir)) {
            fs.mkdirSync(dir, { recursive: true });
        }
    }
}
exports.KnowledgeSiloDetector = KnowledgeSiloDetector;
//# sourceMappingURL=knowledge-silo-detector.js.map