import readline from "node:readline";
const MULTILINE_DELIM = `"""`;
export async function runRepl(handlers) {
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stderr,
        terminal: process.stdin.isTTY,
    });
    rl.setPrompt("you> ");
    rl.prompt();
    let buffer = null;
    rl.on("line", async (raw) => {
        if (buffer) {
            if (raw.trim() === MULTILINE_DELIM) {
                const text = buffer.join("\n").trim();
                buffer = null;
                if (text)
                    await processInput(text, handlers, rl);
                rl.prompt();
                return;
            }
            buffer.push(raw);
            return;
        }
        const line = raw.trim();
        if (!line) {
            rl.prompt();
            return;
        }
        if (line === MULTILINE_DELIM) {
            buffer = [];
            process.stderr.write(`(multi-line mode — finish with ${MULTILINE_DELIM} on its own line)\n`);
            return;
        }
        await processInput(line, handlers, rl);
        rl.prompt();
    });
    await new Promise((resolve) => rl.on("close", resolve));
    await handlers.onClose();
}
async function processInput(line, handlers, rl) {
    if (line.startsWith("/")) {
        const space = line.indexOf(" ");
        const cmd = space > 0 ? line.slice(1, space) : line.slice(1);
        const args = space > 0 ? line.slice(space + 1) : "";
        rl.pause();
        try {
            const handled = await handlers.onSlash(cmd, args);
            if (!handled) {
                process.stderr.write(`unknown command: /${cmd}\n`);
            }
        }
        catch (err) {
            process.stderr.write(`[error] ${err.message}\n`);
        }
        rl.resume();
        return;
    }
    rl.pause();
    try {
        await handlers.onLine(line);
    }
    catch (err) {
        process.stderr.write(`[error] ${err.message}\n`);
    }
    rl.resume();
}
//# sourceMappingURL=repl.js.map