"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.doRepl = doRepl;
const utils_1 = require("../utils");
function doRepl() {
    const readline = require('readline');
    const { REPL_COMMANDS, replCompleter, stripSlash, isKnownCommand } = require('../cli-repl');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
        completer: replCompleter,
        prompt: utils_1.C.primary('cell> '),
    });
    console.log();
    console.log(utils_1.C.bold('  Cell REPL — Interactive Mode'));
    console.log(utils_1.C.dim('  Type / for commands, or enter commands directly'));
    console.log(utils_1.C.dim('  Type /help for available commands'));
    console.log(utils_1.C.dim('  Press Ctrl+C to exit'));
    console.log();
    rl.prompt();
    rl.on('line', (line) => {
        const trimmed = stripSlash(line.trim());
        if (!trimmed) {
            rl.prompt();
            return;
        }
        const parts = trimmed.split(/\s+/);
        const cmd = parts[0];
        const cmdArgs = parts.slice(1);
        if (cmd === 'exit' || cmd === 'quit') {
            console.log(utils_1.C.dim('  Goodbye!'));
            process.exit(0);
        }
        if (cmd === 'help') {
            console.log();
            console.log(utils_1.C.bold('  Available commands:'));
            for (const [name, desc] of Object.entries(REPL_COMMANDS)) {
                console.log(`    ${utils_1.C.primary(name)}  ${desc}`);
            }
            console.log();
            rl.prompt();
            return;
        }
        // Forward to main CLI
        try {
            const { execSync } = require('child_process');
            const binPath = require('path').join(__dirname, '..', '..', '..', '..', '..', 'bin', 'cell.js');
            const result = execSync(`node "${binPath}" ${trimmed}`, {
                encoding: 'utf-8',
                timeout: 30000,
            });
            console.log(result);
        }
        catch (e) {
            console.log(utils_1.C.warn(`  Error: ${e.message}`));
        }
        rl.prompt();
    });
    rl.on('close', () => {
        console.log(utils_1.C.dim('  Goodbye!'));
        process.exit(0);
    });
}
//# sourceMappingURL=repl.js.map