"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NamingEngine = void 0;
const cell_store_1 = require("../walls/06-memory/stores/cell-store");
const GENERIC_WORDS = ['data', 'temp', 'tmp', 'val', 'item', 'result', 'info', 'obj', 'thing', 'stuff', 'args'];
const BOOLEAN_PREFIXES = /^(is|has|should|can|will|must|did|does)\b/;
class NamingEngine {
    store;
    data;
    constructor(cellDir) {
        this.store = new cell_store_1.CellStore(cellDir);
        this.data = this.store.read('naming.json', 'personal', 'style') || {
            version: 1,
            updated: Date.now(),
            camelCase: { confidence: 0, total: 0 },
            snakeCase: { confidence: 0, total: 0 },
            PascalCase: { confidence: 0, total: 0 },
            booleanNaming: { confidence: 0, total: 0, examples: [] },
            abbreviations: { score: 0, total: 0, examples: [] },
            genericNames: { score: 0, total: 0, examples: [] },
        };
    }
    analyze(code) {
        if (!code || code.length < 10)
            return { namesFound: 0 };
        const identifiers = this.extractIdentifiers(code);
        let namesFound = 0;
        for (const name of identifiers) {
            if (/^[a-z]+[A-Z][a-z]/.test(name)) {
                this.data.camelCase.total++;
                this.data.camelCase.confidence = this.smooth('camelCase', 0.9);
                namesFound++;
            }
            if (/^[a-z]+_[a-z]/.test(name)) {
                this.data.snakeCase.total++;
                this.data.snakeCase.confidence = this.smooth('snakeCase', 0.9);
                namesFound++;
            }
            if (/^[A-Z][a-z]+[A-Z]/.test(name)) {
                this.data.PascalCase.total++;
                this.data.PascalCase.confidence = this.smooth('PascalCase', 0.9);
                namesFound++;
            }
            if (BOOLEAN_PREFIXES.test(name)) {
                this.data.booleanNaming.total++;
                this.data.booleanNaming.confidence = this.smooth('booleanNaming', 0.85);
                if (this.data.booleanNaming.examples.length < 5 && !this.data.booleanNaming.examples.includes(name)) {
                    this.data.booleanNaming.examples.push(name);
                }
                namesFound++;
            }
            if (name.length >= 2 && name.length <= 4 && /^[a-z]+$/.test(name) && !['is', 'to', 'in', 'on', 'at', 'by', 'of', 'be', 'it', 'as'].includes(name)) {
                this.data.abbreviations.total++;
                this.data.abbreviations.score = this.smooth('abbreviations', 0.3);
                if (this.data.abbreviations.examples.length < 5 && !this.data.abbreviations.examples.includes(name)) {
                    this.data.abbreviations.examples.push(name);
                }
                namesFound++;
            }
            if (GENERIC_WORDS.includes(name)) {
                this.data.genericNames.total++;
                this.data.genericNames.score = this.smooth('genericNames', 0.2);
                if (this.data.genericNames.examples.length < 5 && !this.data.genericNames.examples.includes(name)) {
                    this.data.genericNames.examples.push(name);
                }
                namesFound++;
            }
        }
        this.data.updated = Date.now();
        this.store.write('naming.json', this.data, 'personal', 'style');
        return { namesFound };
    }
    getSummary() {
        const lines = [];
        const total = this.data.camelCase.total + this.data.snakeCase.total + this.data.PascalCase.total;
        if (total === 0)
            return [];
        const camelPct = total > 0 ? Math.round(this.data.camelCase.total / total * 100) : 0;
        const snakePct = total > 0 ? Math.round(this.data.snakeCase.total / total * 100) : 0;
        const pascalPct = total > 0 ? Math.round(this.data.PascalCase.total / total * 100) : 0;
        lines.push(`Naming: ${camelPct}% camelCase · ${snakePct}% snake_case · ${pascalPct}% PascalCase`);
        if (this.data.abbreviations.total > 3) {
            lines.push(`⚠️  Abbreviations: ${this.data.abbreviations.examples.slice(0, 3).join(', ')}`);
        }
        if (this.data.genericNames.total > 3) {
            lines.push(`⚠️  Generic names: ${this.data.genericNames.examples.slice(0, 3).join(', ')}`);
        }
        return lines;
    }
    extractIdentifiers(code) {
        const identifiers = [];
        const patterns = [
            /\b(const|let|var)\s+(\w+)/g,
            /\bfunction\s+(\w+)/g,
            /\b(?:async\s+)?function\s*\w+\s*\(\s*(\w+)/g,
            /\b(?:const|let|var)\s+\w+\s*=\s*(?:async\s*)?\([^)]*\)\s*=>/g,
            /export\s+(?:default\s+)?(?:const|let|var|function|class)\s+(\w+)/g,
            /\.(\w+)\s*[:=]/g,
            /interface\s+(\w+)/g,
            /type\s+(\w+)\s*=/g,
        ];
        for (const re of patterns) {
            let m;
            while ((m = re.exec(code)) !== null) {
                if (m[1] && !identifiers.includes(m[1]) && !['if', 'for', 'while', 'return', 'new', 'this', 'true', 'false', 'null', 'undefined', 'async', 'function', 'const', 'let', 'var'].includes(m[1])) {
                    identifiers.push(m[1]);
                }
            }
        }
        return identifiers;
    }
    smooth(field, target) {
        const current = this.data[field]?.confidence || this.data[field]?.score || 0;
        const total = this.data[field]?.total || 0;
        const weight = Math.min(total, 100) / 100;
        return current * (1 - weight) + target * weight;
    }
}
exports.NamingEngine = NamingEngine;
//# sourceMappingURL=naming-engine.js.map