"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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileGenerator = void 0;
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const BrainClient_1 = require("../core/brain/BrainClient");
const chalk_1 = __importDefault(require("chalk"));
class FileGenerator {
    constructor(aiCore, projectRoot = process.cwd()) {
        this.aiCore = aiCore;
        this.projectRoot = projectRoot;
        // Walk up to find workspace root (directory containing .mycontext/)
        this.brainClient = BrainClient_1.BrainClient.getInstance(this.findWorkspaceRoot(projectRoot));
    }
    /**
     * Walk up directory tree to find the workspace root (contains .mycontext/)
     */
    findWorkspaceRoot(startDir) {
        let dir = startDir;
        for (let i = 0; i < 10; i++) { // max 10 levels up
            if (fs.existsSync(path.join(dir, '.mycontext'))) {
                return dir;
            }
            const parent = path.dirname(dir);
            if (parent === dir)
                break; // reached filesystem root
            dir = parent;
        }
        return startDir; // fallback to project root
    }
    async generateFile(relativePath, prompt, context) {
        const filePath = path.join(this.projectRoot, relativePath);
        const exists = await fs.pathExists(filePath);
        let fullPrompt = prompt;
        // Phase 11: Lego Assembly - Search Registry
        const registry = await this.brainClient.getRegistry();
        const relevantPieces = registry.components.filter((c) => {
            const nameMatch = prompt.toLowerCase().includes(c.name.toLowerCase());
            const descMatch = c.description.toLowerCase().split(' ').some((word) => word.length > 3 && prompt.toLowerCase().includes(word.toLowerCase()));
            return nameMatch || descMatch;
        });
        if (relevantPieces.length > 0) {
            console.log(chalk_1.default.cyan(`🧩 Lego Pieces Found: ${relevantPieces.map((p) => p.name).join(', ')}`));
            let piecesContext = "\n\nRELEVANT LEGO PIECES (Reuse patterns/styles from these):\n";
            for (const piece of relevantPieces) {
                try {
                    // Find full path to piece (registry stores relative or absolute, we need to be careful)
                    // Assuming registry.path is relative to workspace root or project root.
                    // For now, let's try to read it.
                    const pieceContent = await fs.readFile(path.join(this.projectRoot, piece.path), 'utf-8');
                    piecesContext += `File: ${piece.path}\nContent:\n\`\`\`tsx\n${pieceContent}\n\`\`\`\n`;
                }
                catch (e) {
                    // Ignore if file missing
                }
            }
            fullPrompt += piecesContext;
        }
        if (exists) {
            const existingContent = await fs.readFile(filePath, 'utf-8');
            console.log(chalk_1.default.yellow(`📝 Updating existing file: ${relativePath}`));
            fullPrompt = `
        TASK: Update the following file based on the instructions.
        
        EXISTING CONTENT:
        \`\`\`tsx
        ${existingContent}
        \`\`\`
        
        INSTRUCTIONS:
        ${prompt}
        
        ${relevantPieces.length > 0 ? 'REUSE PATTERNS FROM LEGO PIECES ABOVE.' : ''}
        
        IMPORTANT: Return the FULL updated file content. Do not return a diff.
      `;
        }
        else {
            console.log(chalk_1.default.green(`✨ Creating new file: ${relativePath}`));
        }
        if (context) {
            fullPrompt += `\n\nADDITIONAL CONTEXT:\n${context}`;
        }
        fullPrompt += `\n\nReturn ONLY the raw code. No markdown fences.`;
        let code = await this.aiCore.generateText(fullPrompt);
        // Clean up
        code = code.replace(/^```[a-z]*\n?/gmi, "").replace(/^```\n?/gmi, "").replace(/\n?```$/gmi, "").trim();
        // Client Component Guard
        if (this.needsUseClient(code) && !code.includes('"use client"') && !code.includes("'use client'")) {
            console.log(chalk_1.default.magenta(`🛡️  Auto-injecting "use client" directive`));
            code = `"use client";\n\n` + code;
        }
        await fs.ensureDir(path.dirname(filePath));
        await fs.writeFile(filePath, code);
        // Phase 11: Auto-Register in Living DB
        const fileName = path.basename(relativePath);
        await this.brainClient.registerComponent(fileName, `Generated component for: ${prompt.substring(0, 100)}...`, relativePath);
        return code;
    }
    needsUseClient(code) {
        const hooks = [
            'useState', 'useEffect', 'useContext', 'useReducer', 'useCallback',
            'useMemo', 'useRef', 'useLayoutEffect', 'useImperativeHandle',
            'useDebugValue', 'useDeferredValue', 'useTransition', 'useId',
            'useSyncExternalStore', 'useInsertionEffect',
            'createContext'
        ];
        const routerHooks = [
            'useRouter', 'usePathname', 'useSearchParams',
            'useSelectedLayoutSegment', 'useSelectedLayoutSegments', 'useParams'
        ];
        // Regex to match usage like: useTheme(), useState(null), etc. to avoid matching string literals
        const hookRegex = new RegExp(`\\b(${[...hooks, ...routerHooks].join('|')})\\b`);
        return hookRegex.test(code);
    }
}
exports.FileGenerator = FileGenerator;
//# sourceMappingURL=FileGenerator.js.map