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 _StateSynchronizer_observers;
import { BehaviorSubject } from 'rxjs';
/**
 * State synchronizer singleton
 *
 * Used to synchronize changes across atoms of the same family
 */
export class StateSynchronizer {
    constructor() {
        _StateSynchronizer_observers.set(this, void 0);
        __classPrivateFieldSet(this, _StateSynchronizer_observers, new Map(), "f");
    }
    /**
     * Register a key in the state synchronizer
     *
     * @param key key to register
     * @param defaultValue value to register
     */
    register(key, defaultValue) {
        if (!__classPrivateFieldGet(this, _StateSynchronizer_observers, "f").has(key)) {
            __classPrivateFieldGet(this, _StateSynchronizer_observers, "f").set(key, new BehaviorSubject({ type: 'initial', value: defaultValue }));
        }
    }
    /**
     * Check if a given key is registered in the state synchronizer
     *
     * @param key key to check if registered
     */
    isRegistered(key) {
        return __classPrivateFieldGet(this, _StateSynchronizer_observers, "f").has(key);
    }
    /**
     * Get the current state for a given key
     *
     * @param key key to get the current value for
     */
    getCurrentState(key) {
        if (!this.isRegistered(key)) {
            return null;
        }
        return __classPrivateFieldGet(this, _StateSynchronizer_observers, "f").get(key).getValue();
    }
    /**
     * Subscribe to changes on a given key
     *
     * @param key key to subscribe to
     */
    subscribe(key, subscription) {
        // If somehow the ended up with no listener here, register it with null value
        if (!this.isRegistered(key)) {
            this.register(key, null);
        }
        const sub = __classPrivateFieldGet(this, _StateSynchronizer_observers, "f").get(key).subscribe(subscription);
        return () => {
            sub.unsubscribe();
            // if no more observers, remove the listener
            if (__classPrivateFieldGet(this, _StateSynchronizer_observers, "f").get(key).observers.length === 0) {
                __classPrivateFieldGet(this, _StateSynchronizer_observers, "f").delete(key);
            }
        };
    }
    /**
     * Notify listeners for a given key
     *
     * @param key key to notify listeners for
     * @param update update to notify about
     */
    notify(key, update) {
        // If somehow the ended up with no listener here, register it with null value
        if (!this.isRegistered(key)) {
            this.register(key, null);
        }
        __classPrivateFieldGet(this, _StateSynchronizer_observers, "f").get(key).next(update);
    }
}
_StateSynchronizer_observers = new WeakMap();
//# sourceMappingURL=state-synchronizer.js.map