"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSyslogTopicHandler = createSyslogTopicHandler;
const KEY_ALL = "syslog:all";
function createSyslogTopicHandler(deps, getAllClients) {
    return {
        topic: "syslog",
        async sub(ctx, msg) {
            const tail = Math.max(0, Number(msg?.tail ?? 0));
            ctx.addSub("syslog", KEY_ALL);
            if (tail > 0) {
                const entries = await deps.getSyslogTail(tail);
                const m = { op: "syslog.tail", entries: entries ?? [] };
                ctx.send(m);
            }
        },
        unsub(ctx) {
            ctx.delSub("syslog", KEY_ALL);
        },
        push(entry) {
            const m = { op: "syslog.append", entry };
            for (const c of getAllClients()) {
                if (c.hasSub("syslog", KEY_ALL))
                    c.send(m);
            }
        },
    };
}
