import { EventEmitter } from 'eventemitter3';
import { unstable_batchedUpdates } from 'react-dom';
var emitterCache;
function getEmitter() {
    if (!emitterCache) {
        emitterCache = new EventEmitter();
    }
    return emitterCache;
}
function listenBrowserEvents(globalObject) {
    var _a, _b;
    (_a = globalObject.addEventListener) === null || _a === void 0 ? void 0 : _a.call(globalObject, 'scroll', emit, { capture: true });
    (_b = globalObject.addEventListener) === null || _b === void 0 ? void 0 : _b.call(globalObject, 'resize', emit, { capture: true });
}
function unlistenBrowserEvents(globalObject) {
    var _a, _b;
    (_a = globalObject.removeEventListener) === null || _a === void 0 ? void 0 : _a.call(globalObject, 'scroll', emit, { capture: true });
    (_b = globalObject.removeEventListener) === null || _b === void 0 ? void 0 : _b.call(globalObject, 'resize', emit, { capture: true });
}
export function addListener(callback, globalObject) {
    var emitter = getEmitter();
    if (emitter.listeners('layout').length === 0) {
        listenBrowserEvents(globalObject);
    }
    emitter.addListener('layout', callback);
    return {
        remove: function () {
            emitter.removeListener('layout', callback);
            if (emitter.listeners('layout').length === 0) {
                unlistenBrowserEvents(globalObject);
            }
        },
    };
}
export function emit() {
    unstable_batchedUpdates(function () {
        getEmitter().emit('layout');
    });
}
//# sourceMappingURL=LayoutEvents.js.map