var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Timers_intervals, _Timers_definitions, _Timers_dispatcher;
import { UIEventScope } from '@devvit/protos';
import { WrappedEvent } from './mappers.js';
export class Timers {
    constructor(dispatcher) {
        _Timers_intervals.set(this, new Map());
        _Timers_definitions.set(this, new Map());
        _Timers_dispatcher.set(this, void 0);
        if (devvit?.debug.runtime)
            console.debug('[runtime] timers: constructor');
        __classPrivateFieldSet(this, _Timers_dispatcher, dispatcher, "f");
    }
    add(definition) {
        if (devvit?.debug.runtime)
            console.debug('[runtime] timers: add', definition);
        __classPrivateFieldGet(this, _Timers_definitions, "f").set(definition.id, definition);
        __classPrivateFieldGet(this, _Timers_intervals, "f").set(definition.id, window.setInterval(() => {
            __classPrivateFieldGet(this, _Timers_dispatcher, "f").submit([
                new WrappedEvent({
                    scope: UIEventScope.ALL,
                    timer: {},
                    hook: definition.id,
                }),
            ]);
        }, definition.duration));
    }
    apply(snapshot) {
        if (devvit?.debug.runtime)
            console.debug('[runtime] timers: apply', snapshot);
        const unknown = new Map(__classPrivateFieldGet(this, _Timers_definitions, "f"));
        snapshot.forEach((definition) => {
            unknown.delete(definition.id);
            if (!__classPrivateFieldGet(this, _Timers_definitions, "f").has(definition.id)) {
                if (devvit?.debug.runtime) {
                    console.debug('[runtime] timers: apply - adding definition', definition);
                }
                this.add(definition);
            }
        });
        if (devvit?.debug.runtime) {
            console.debug('[runtime] timers: apply - removing the following definitions', unknown);
        }
        for (const [id] of unknown) {
            this.remove(id);
        }
    }
    // My assumption is that we want clearAll here because we're not
    // given an argument in the function to clear just one thing.
    clear() {
        if (devvit?.debug.runtime)
            console.debug('[runtime] timers: clear');
        for (const interval of __classPrivateFieldGet(this, _Timers_intervals, "f").values()) {
            clearInterval(interval);
        }
        __classPrivateFieldGet(this, _Timers_intervals, "f").clear();
        __classPrivateFieldGet(this, _Timers_definitions, "f").clear();
    }
    list() {
        if (devvit?.debug.runtime)
            console.debug('[runtime] timers: list');
        return [...__classPrivateFieldGet(this, _Timers_definitions, "f").values()];
    }
    pauseAll() {
        if (devvit?.debug.runtime)
            console.debug('[runtime] timers: pauseAll');
        // There is no such thing as pause in web setInterval as far as I know.
        for (const interval of __classPrivateFieldGet(this, _Timers_intervals, "f").values()) {
            clearInterval(interval);
        }
        return this.list();
    }
    remove(id) {
        if (devvit?.debug.runtime)
            console.debug('[runtime] timers: remove');
        __classPrivateFieldGet(this, _Timers_definitions, "f").delete(id);
        clearInterval(__classPrivateFieldGet(this, _Timers_intervals, "f").get(id));
        __classPrivateFieldGet(this, _Timers_intervals, "f").delete(id);
    }
    resumeAll(snapshot) {
        if (devvit?.debug.runtime)
            console.debug('[runtime] timers: resumeAll');
        this.apply(snapshot);
    }
}
_Timers_intervals = new WeakMap(), _Timers_definitions = new WeakMap(), _Timers_dispatcher = new WeakMap();
//# sourceMappingURL=timers.js.map