"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProductivityEngine = void 0;
const cell_store_1 = require("../walls/06-memory/stores/cell-store");
class ProductivityEngine {
    store;
    data;
    constructor(cellDir) {
        this.store = new cell_store_1.CellStore(cellDir);
        this.data = this.store.read('speed.json', 'personal', 'productivity') || {
            version: 1, updated: Date.now(),
            speed: { totalEdits: 0, editsPerFile: 0, avgEditSize: 0, fastestPattern: '', slowestPattern: '' },
            decisions: { accepted: 0, rejected: 0, modified: 0, acceptRate: 0, topAccepted: [], topRejected: [] },
            reverts: { total: 0, files: [] },
            contextSwitching: { sessionCount: 0, filesPerSession: [], avgFilesPerSession: 0, peakFiles: 0 },
            engineering: { overEngineering: 0, underEngineering: 0, trend: 'balanced', recentChanges: [] },
        };
    }
    recordEdit(prevContent, newContent) {
        const prevLen = prevContent.length;
        const newLen = newContent.length;
        const editSize = Math.abs(newLen - prevLen);
        const prevTotal = this.data.speed.avgEditSize * this.data.speed.totalEdits;
        this.data.speed.totalEdits++;
        this.data.speed.avgEditSize = Math.round((prevTotal + editSize) / this.data.speed.totalEdits);
        this.save();
        return { editSize };
    }
    recordDecision(outcome, patternName) {
        if (outcome === 'accepted') {
            this.data.decisions.accepted++;
            if (!this.data.decisions.topAccepted.includes(patternName))
                this.data.decisions.topAccepted.push(patternName);
        }
        else if (outcome === 'rejected') {
            this.data.decisions.rejected++;
            if (!this.data.decisions.topRejected.includes(patternName))
                this.data.decisions.topRejected.push(patternName);
        }
        else {
            this.data.decisions.modified++;
        }
        const total = this.data.decisions.accepted + this.data.decisions.rejected + this.data.decisions.modified;
        this.data.decisions.acceptRate = Math.round(this.data.decisions.accepted / total * 100);
        this.save();
    }
    recordRevert(filePath) {
        this.data.reverts.total++;
        const existing = this.data.reverts.files.find(f => f.path === filePath);
        if (existing)
            existing.count++;
        else
            this.data.reverts.files.push({ path: filePath, count: 1 });
        this.save();
    }
    recordContextSwitch(filesInSession) {
        this.data.contextSwitching.sessionCount++;
        this.data.contextSwitching.filesPerSession.push(filesInSession);
        const total = this.data.contextSwitching.filesPerSession.reduce((a, b) => a + b, 0);
        this.data.contextSwitching.avgFilesPerSession = Math.round(total / this.data.contextSwitching.sessionCount);
        if (filesInSession > this.data.contextSwitching.peakFiles)
            this.data.contextSwitching.peakFiles = filesInSession;
        this.save();
    }
    analyzeEngineering(code, filePath) {
        if (!code || code.length < 30)
            return;
        const lines = code.split('\n').length;
        const ifCount = (code.match(/\bif\b/g) || []).length;
        const classCount = (code.match(/\bclass\b/g) || []).length;
        const interfaceCount = (code.match(/\binterface\b|\btype\b/g) || []).length;
        const genericCount = (code.match(/<[A-Z]\w*>/g) || []).length;
        const abstractionCount = classCount + interfaceCount + genericCount;
        let score = 0;
        let type = 'balanced';
        if (lines < 10 && abstractionCount > 2) {
            score = 70;
            type = 'complex'; /* small file, too many abstractions = over */
        }
        else if (lines > 50 && ifCount > 10 && abstractionCount === 0) {
            score = 70;
            type = 'simple'; /* big file, no abstractions = under */
        }
        else if (abstractionCount > lines * 0.3) {
            score = 50;
            type = 'complex';
        }
        else if (lines > 80 && abstractionCount < 2) {
            score = 40;
            type = 'simple';
        }
        else {
            score = 10;
            type = 'balanced';
        }
        if (type === 'complex')
            this.data.engineering.overEngineering = Math.min(100, this.data.engineering.overEngineering + 10);
        if (type === 'simple')
            this.data.engineering.underEngineering = Math.min(100, this.data.engineering.underEngineering + 10);
        if (type === 'balanced') {
            this.data.engineering.overEngineering = Math.max(0, this.data.engineering.overEngineering - 5);
            this.data.engineering.underEngineering = Math.max(0, this.data.engineering.underEngineering - 5);
        }
        this.data.engineering.recentChanges.push({
            file: filePath || 'unknown', type, score,
        });
        if (this.data.engineering.recentChanges.length > 20)
            this.data.engineering.recentChanges = this.data.engineering.recentChanges.slice(-20);
        const over = this.data.engineering.overEngineering;
        const under = this.data.engineering.underEngineering;
        this.data.engineering.trend = over > under + 20 ? 'over' : under > over + 20 ? 'under' : 'balanced';
        this.save();
    }
    getSummary() {
        const lines = [];
        if (this.data.speed.totalEdits === 0)
            return [];
        lines.push(`Edits: ${this.data.speed.totalEdits} total, avg ${this.data.speed.avgEditSize} chars/file`);
        lines.push(`Accept rate: ${this.data.decisions.acceptRate}% (${this.data.decisions.accepted} accepted, ${this.data.decisions.rejected} rejected)`);
        if (this.data.reverts.total > 0)
            lines.push(`Reverts: ${this.data.reverts.total} total (${this.data.reverts.files.length} files)`);
        const ctx = this.data.contextSwitching;
        if (ctx.sessionCount > 0)
            lines.push(`Context switches: avg ${ctx.avgFilesPerSession} files/session, peak ${ctx.peakFiles}`);
        const eng = this.data.engineering;
        if (eng.overEngineering > 20 || eng.underEngineering > 20) {
            const prefix = eng.trend === 'over' ? '⚠️  Over-engineering' : eng.trend === 'under' ? '⚠️  Under-engineering' : '✅ Balanced';
            lines.push(`${prefix}: ${eng.overEngineering}% over, ${eng.underEngineering}% under`);
        }
        return lines;
    }
    save() {
        this.data.updated = Date.now();
        this.store.write('speed.json', this.data, 'personal', 'productivity');
    }
}
exports.ProductivityEngine = ProductivityEngine;
//# sourceMappingURL=productivity-engine.js.map