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 _EventBus_events$, _EventBus_parentBus;
import { jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import { Subject } from 'rxjs';
import {} from '../../types/core';
/**
 * Global event bus that allows to publish and subscribe to events.
 */
export class EventBus {
    constructor(parentBus) {
        _EventBus_events$.set(this, new Subject());
        _EventBus_parentBus.set(this, null);
        __classPrivateFieldSet(this, _EventBus_parentBus, parentBus !== null && parentBus !== void 0 ? parentBus : null, "f");
    }
    publish(type, data) {
        __classPrivateFieldGet(this, _EventBus_events$, "f").next({ type, data });
        // bubble up the event
        if (__classPrivateFieldGet(this, _EventBus_parentBus, "f")) {
            __classPrivateFieldGet(this, _EventBus_parentBus, "f").publish(type, data);
        }
    }
    subscribe(callback) {
        // pipe just to make a copy
        const sub = __classPrivateFieldGet(this, _EventBus_events$, "f").pipe().subscribe(callback);
        return () => sub.unsubscribe();
    }
}
_EventBus_events$ = new WeakMap(), _EventBus_parentBus = new WeakMap();
/** Default top-level event bus */
export const DEFAULT_BUS = new EventBus();
const EventBusContext = React.createContext(DEFAULT_BUS);
/** Hook to get the event bus instance */
export function useEventBus() {
    return React.useContext(EventBusContext);
}
/**
 * Component that captures events of a given type fired in its children
 * and calls the provided callback with the event data.
 */
export function EventCapturer({ children, onEvent }) {
    const parentBus = useEventBus();
    const bus = React.useMemo(() => new EventBus(parentBus), [parentBus]);
    React.useEffect(() => {
        return bus.subscribe(onEvent);
    }, [bus, onEvent]);
    return _jsx(EventBusContext.Provider, { value: bus, children: children });
}
//# sourceMappingURL=event-bus.js.map