/**
 * Compile the agent-supplied watches. Validation lives at the
 * tool boundary (see tool.ts); this function trusts shape +
 * regex validity and just normalises defaults.
 */
export function compileWatches(input) {
    if (input === undefined)
        return [];
    return input.map((w, i) => ({
        index: i,
        pattern: w.pattern,
        stream: w.stream ?? "both",
        repeat: w.repeat === true,
        regex: new RegExp(w.pattern),
        fired: false,
    }));
}
/**
 * Run a freshly-emitted line against the compiled set. Returns
 * the watches that matched AND are eligible to fire (skipping
 * single-fire watches that already fired). Mutates `fired` for
 * matched single-fire watches so the same line stream of repeats
 * doesn't keep alerting.
 */
export function evaluateWatches(watches, source, line) {
    const hits = [];
    for (const w of watches) {
        if (w.stream !== "both" && w.stream !== source)
            continue;
        if (!w.repeat && w.fired)
            continue;
        if (!w.regex.test(line))
            continue;
        if (!w.repeat)
            w.fired = true;
        hits.push(w);
    }
    return hits;
}
/**
 * Build the wire shape for a single match. Used by the manager
 * when it fans out a `process_watch_matched` event.
 */
export function buildMatchEvent(watch, processId, processName, processCommand, source, line) {
    return {
        processId,
        processName,
        processCommand,
        source,
        line,
        watch: {
            index: watch.index,
            pattern: watch.pattern,
            stream: watch.stream,
            repeat: watch.repeat,
        },
    };
}
//# sourceMappingURL=watches.js.map