const levelRank = {
    debug: 10,
    info: 20,
    warn: 30,
    error: 40,
    silent: 50
};
export class Logger {
    level;
    sinks = new Set();
    lastEmitted = new Map();
    constructor(level = "info") {
        this.level = level;
    }
    setLevel(level) {
        this.level = level;
    }
    addSink(sink) {
        this.sinks.add(sink);
        return () => this.sinks.delete(sink);
    }
    debug(category, message, data) {
        this.log("debug", category, message, data);
    }
    info(category, message, data) {
        this.log("info", category, message, data);
    }
    warn(category, message, data) {
        this.log("warn", category, message, data);
    }
    error(category, message, data) {
        this.log("error", category, message, data);
    }
    log(level, category, message, data, rateLimitMs = 0) {
        if (levelRank[level] < levelRank[this.level])
            return;
        const key = `${level}:${category}:${message}`;
        const now = Date.now();
        if (rateLimitMs > 0 && now - (this.lastEmitted.get(key) ?? -Infinity) < rateLimitMs)
            return;
        this.lastEmitted.set(key, now);
        const entry = Object.freeze({ level, category, message, timestamp: now, data });
        for (const sink of [...this.sinks]) {
            try {
                sink(entry);
            }
            catch {
                this.sinks.delete(sink);
            }
        }
    }
}
//# sourceMappingURL=Logger.js.map