"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FrontendEngine = void 0;
const cell_store_1 = require("../walls/06-memory/stores/cell-store");
class FrontendEngine {
    store;
    data;
    constructor(cellDir) {
        this.store = new cell_store_1.CellStore(cellDir);
        this.data = this.store.read('frontend.json', 'personal', 'architecture') || {
            version: 1, updated: Date.now(),
            framework: { name: 'unknown', confidence: 0 },
            stateManagement: { name: 'unknown', confidence: 0 },
            dataFetching: { name: 'unknown', confidence: 0 },
            styling: { name: 'unknown', confidence: 0 },
            componentStyle: { functional: 0, classBased: 0, hooks: 0 },
            totalComponents: 0,
        };
    }
    analyze(code) {
        if (!code || code.length < 20)
            return { componentsFound: 0 };
        let found = 0;
        // Framework
        const fw = this.detectFramework(code);
        if (fw.confidence > this.data.framework.confidence) {
            this.data.framework = fw;
            found++;
        }
        // State management
        const sm = this.detectStateMgmt(code);
        if (sm.confidence > this.data.stateManagement.confidence) {
            this.data.stateManagement = sm;
            found++;
        }
        // Data fetching
        const df = this.detectDataFetching(code);
        if (df.confidence > this.data.dataFetching.confidence) {
            this.data.dataFetching = df;
            found++;
        }
        // Styling
        const st = this.detectStyling(code);
        if (st.confidence > this.data.styling.confidence) {
            this.data.styling = st;
            found++;
        }
        // Component style
        this.data.totalComponents += (code.match(/export\s+(?:default\s+)?function\s+\w+|const\s+\w+\s*[:=]\s*(?:React\.)?(?:FC|FunctionComponent)/g) || []).length;
        const funcComps = (code.match(/export\s+(?:default\s+)?function\s+\w+|const\s+\w+\s*[:=]\s*(?:async\s*)?\(/g) || []).length;
        const classComps = (code.match(/class\s+\w+\s+extends\s+(?:React\.)?Component/g) || []).length;
        const hookCalls = (code.match(/(?:useContext|useState|useEffect|useReducer|useCallback|useMemo|useRef|useQuery|useMutation)\s*\(/g) || []).length;
        if (funcComps > 0)
            this.data.componentStyle.functional += funcComps;
        if (classComps > 0)
            this.data.componentStyle.classBased += classComps;
        if (hookCalls > 0)
            this.data.componentStyle.hooks += hookCalls;
        if (found > 0 || funcComps > 0 || classComps > 0 || hookCalls > 0) {
            this.data.updated = Date.now();
            this.store.write('frontend.json', this.data, 'personal', 'architecture');
        }
        return { componentsFound: funcComps + classComps };
    }
    getSummary() {
        const lines = [];
        if (this.data.totalComponents === 0 && this.data.framework.confidence === 0)
            return [];
        lines.push(`Framework: ${this.data.framework.name} · State: ${this.data.stateManagement.name} · Style: ${this.data.styling.name}`);
        if (this.data.totalComponents > 0) {
            lines.push(`Components: ${this.data.componentStyle.functional} functional, ${this.data.componentStyle.classBased} class-based · Hooks: ${this.data.componentStyle.hooks}`);
        }
        return lines;
    }
    detectFramework(code) {
        if (/from\s+['"]react['"]/i.test(code) || /import.*\breact\b/i.test(code))
            return { name: 'React', confidence: 0.9 };
        if (/from\s+['"]vue['"]/i.test(code) || /import.*\bvue\b/i.test(code))
            return { name: 'Vue', confidence: 0.85 };
        if (/from\s+['"]@angular/i.test(code))
            return { name: 'Angular', confidence: 0.85 };
        if (/from\s+['"]svelte['"]/i.test(code))
            return { name: 'Svelte', confidence: 0.8 };
        if (/from\s+['"]solid-js['"]/i.test(code))
            return { name: 'Solid', confidence: 0.75 };
        return { name: 'unknown', confidence: 0 };
    }
    detectStateMgmt(code) {
        if (/from\s+['"]redux['"]/i.test(code) || /from\s+['"]@reduxjs/i.test(code))
            return { name: 'Redux', confidence: 0.9 };
        if (/from\s+['"]zustand['"]/i.test(code))
            return { name: 'Zustand', confidence: 0.85 };
        if (/from\s+['"]jotai['"]/i.test(code))
            return { name: 'Jotai', confidence: 0.8 };
        if (/from\s+['"]mobx['"]/i.test(code))
            return { name: 'MobX', confidence: 0.85 };
        if (/\buseContext\s*\(/i.test(code) && /\bcreateContext\s*\(/i.test(code))
            return { name: 'Context API', confidence: 0.7 };
        if (/from\s+['"]@ngrx/i.test(code))
            return { name: 'NgRx', confidence: 0.8 };
        return { name: 'unknown', confidence: 0 };
    }
    detectDataFetching(code) {
        if (/from\s+['"]@tanstack\/react-query['"]/i.test(code) || /from\s+['"]react-query['"]/i.test(code))
            return { name: 'React Query', confidence: 0.9 };
        if (/from\s+['"]swr['"]/i.test(code))
            return { name: 'SWR', confidence: 0.85 };
        if (/from\s+['"]@apollo/i.test(code))
            return { name: 'Apollo', confidence: 0.85 };
        if (/\baxios\b/i.test(code) && /\.(?:get|post|put|delete)\s*\(/i.test(code))
            return { name: 'Axios', confidence: 0.7 };
        if (/\bfetch\s*\(/i.test(code) && !/from\s+['"]/i.test(code))
            return { name: 'fetch', confidence: 0.5 };
        return { name: 'unknown', confidence: 0 };
    }
    detectStyling(code) {
        if (/\bclassName\s*:?\s*['"]\s*(?:flex|grid|text-|bg-|p-[0-9]|m-[0-9])/.test(code) || /from\s+['"]..\/tailwind/i.test(code))
            return { name: 'Tailwind', confidence: 0.85 };
        if (/\.module\.(css|scss)/.test(code) || /from\s+['"][^'"]+\.module\.(css|scss)['"]/.test(code))
            return { name: 'CSS Modules', confidence: 0.8 };
        if (/from\s+['"]styled-components['"]/i.test(code) || /styled\.\w+`/i.test(code))
            return { name: 'Styled Components', confidence: 0.85 };
        if (/from\s+['"]@emotion/i.test(code))
            return { name: 'Emotion', confidence: 0.8 };
        return { name: 'unknown', confidence: 0 };
    }
}
exports.FrontendEngine = FrontendEngine;
//# sourceMappingURL=frontend-engine.js.map