export class LiveStatusTracker {
    base = null;
    overrides = new Map();
    subscribers = new Set();
    subscribe(subscriber) {
        this.subscribers.add(subscriber);
        const current = this.currentState();
        if (current) {
            subscriber(current);
        }
        return () => {
            this.subscribers.delete(subscriber);
        };
    }
    setBase(text, options) {
        const keepStart = this.base && this.base.text === text ? this.base.startedAt : Date.now();
        this.base = { text, detail: options?.detail, startedAt: keepStart };
        this.emit();
    }
    pushOverride(id, text, options) {
        const existing = this.overrides.get(id);
        const startedAt = existing && existing.text === text ? existing.startedAt : Date.now();
        this.overrides.set(id, { text, detail: options?.detail, tone: options?.tone, startedAt });
        this.emit();
        return () => this.clearOverride(id);
    }
    clearOverride(id) {
        this.overrides.delete(id);
        this.emit();
    }
    currentState() {
        if (this.overrides.size) {
            const last = Array.from(this.overrides.values()).pop();
            if (last)
                return last;
        }
        return this.base;
    }
    emit() {
        const state = this.currentState();
        if (!state)
            return;
        for (const sub of this.subscribers) {
            try {
                sub(state);
            }
            catch {
                // ignore subscriber errors
            }
        }
    }
}
//# sourceMappingURL=liveStatus.js.map