import onFind from './onFind.js';
const KEY = Symbol.for('brightspot.onVisible');
let _state;
function getState() {
    if (_state)
        return _state;
    _state = globalThis[KEY];
    if (!_state) {
        _state = { calledIndex: 0 };
        globalThis[KEY] = _state;
    }
    return _state;
}
export function onVisible(selectors, callback) {
    const calledId = `_ovc${getState().calledIndex++}`;
    const observer = new IntersectionObserver(changes => {
        for (const change of changes) {
            if (change.isIntersecting) {
                const element = change.target;
                const elementClasses = element.classList;
                if (!elementClasses.contains(calledId)) {
                    elementClasses.add(calledId);
                    observer.unobserve(element);
                    callback(element);
                }
            }
        }
    });
    onFind(selectors, element => {
        observer.observe(element);
    });
}
//# sourceMappingURL=onVisible.js.map