"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.doWatch = doWatch;
exports.doBridge = doBridge;
exports.doWatchEditor = doWatchEditor;
const utils_1 = require("../utils");
const project = (0, utils_1.getProjectName)();
function doWatch(args) {
    const sub = args[1] || '';
    if (sub === 'start') {
        const dir = args[2] || process.cwd();
        (0, utils_1.httpPost)('/watch/start', { project, dir }).then((result) => {
            (0, utils_1.printSuccess)(`Watcher started for ${project}`);
            (0, utils_1.printDim)(`Watching: ${dir}`);
        }).catch(() => (0, utils_1.printWarning)('Failed to start watcher'));
        return;
    }
    if (sub === 'stop') {
        (0, utils_1.httpPost)('/watch/stop', { project }).then(() => {
            (0, utils_1.printSuccess)(`Watcher stopped for ${project}`);
        }).catch(() => (0, utils_1.printWarning)('Failed to stop watcher'));
        return;
    }
    if (sub === 'events') {
        const limit = parseInt(args[2] || '20');
        (0, utils_1.httpGet)(`/watch/events?project=${project}&limit=${limit}`).then((result) => {
            if (result.events && result.events.length > 0) {
                for (const e of result.events) {
                    console.log(`  [${e.eventType}] ${e.filePath}`);
                    (0, utils_1.printDim)(`    ${e.created_at}`);
                }
            }
            else {
                (0, utils_1.printDim)('No events recorded');
            }
        }).catch(() => (0, utils_1.printWarning)('Failed to get events'));
        return;
    }
    if (sub === 'status') {
        (0, utils_1.httpGet)(`/watch/status?project=${project}`).then((result) => {
            if (result.active) {
                (0, utils_1.printSuccess)(`Watcher active: ${result.dirPath}`);
                (0, utils_1.printDim)(`Events: ${result.eventCount} | Last: ${result.lastEventAt || 'never'}`);
            }
            else {
                (0, utils_1.printDim)('No active watcher');
            }
        }).catch(() => (0, utils_1.printWarning)('Failed to get watcher status'));
        return;
    }
    // Default: show status
    (0, utils_1.httpGet)(`/watch/status?project=${project}`).then((result) => {
        if (result.active) {
            (0, utils_1.printSuccess)(`Watcher active: ${result.dirPath}`);
        }
        else {
            (0, utils_1.printDim)('No active watcher. Start with: cell watch start');
        }
    }).catch(() => (0, utils_1.printWarning)('Failed to get watcher status'));
}
function doBridge(args) {
    const sub = args[1] || '';
    if (sub === 'start') {
        (0, utils_1.printHeader)('Cell Bridge');
        try {
            const { startBridge } = require('../../watcher/socket-bridge');
            startBridge();
            (0, utils_1.printSuccess)('Bridge started on port 9878');
        }
        catch (e) {
            (0, utils_1.printWarning)('Failed to start bridge: ' + String(e));
        }
        return;
    }
    if (sub === 'stop') {
        try {
            const { stopBridge } = require('../../watcher/socket-bridge');
            stopBridge();
            (0, utils_1.printSuccess)('Bridge stopped');
        }
        catch (e) {
            (0, utils_1.printWarning)('Failed to stop bridge: ' + String(e));
        }
        return;
    }
    if (sub === 'status') {
        try {
            const { discoverBridgePort } = require('../../watcher/socket-bridge');
            const port = discoverBridgePort();
            if (port) {
                (0, utils_1.printSuccess)(`Bridge running on port ${port}`);
            }
            else {
                (0, utils_1.printDim)('Bridge not running');
            }
        }
        catch {
            (0, utils_1.printDim)('Bridge status unknown');
        }
        return;
    }
    // Default: show status
    try {
        const { discoverBridgePort } = require('../../watcher/socket-bridge');
        const port = discoverBridgePort();
        if (port) {
            (0, utils_1.printSuccess)(`Bridge running on port ${port}`);
        }
        else {
            (0, utils_1.printDim)('Bridge not running. Start with: cell bridge start');
        }
    }
    catch {
        (0, utils_1.printDim)('Bridge status unknown');
    }
}
function doWatchEditor(args) {
    (0, utils_1.printHeader)('Cell Watch Editor');
    try {
        const { startEditorMonitor } = require('../../watcher/socket-bridge');
        startEditorMonitor();
        (0, utils_1.printSuccess)('Editor monitor started');
        (0, utils_1.printDim)('Press Ctrl+C to stop');
    }
    catch (e) {
        (0, utils_1.printWarning)('Failed to start editor monitor: ' + String(e));
    }
}
//# sourceMappingURL=watch.js.map