/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
export class VisibilityObserver {
    constructor(callback) {
        this.callback = callback;
        this.handleVisibilityChange = this.handleVisibilityChange.bind(this);
        this.emitVisibilityChange = this.emitVisibilityChange.bind(this);
        this.mutationObservers = [];
    }
    isVisible(element) {
        if ('checkVisibility' in element) {
            return element.checkVisibility({
                visibilityProperty: true,
                opacityProperty: true,
            });
        }
        // In the case that checkVisibility is not available for an
        // element, just return true rather than checking the entire
        // parent structure.
        return true;
    }
    observe(element) {
        this.targetElement = element;
        this.watchElementVisibility(element);
    }
    unobserve() {
        if (this.targetElement != null) {
            this.mutationObservers.forEach((obs) => obs.disconnect());
            this.mutationObservers = [];
        }
        this.targetElement = undefined;
    }
    disconnect() {
        this.mutationObservers.forEach((obs) => obs.disconnect());
        this.mutationObservers = [];
    }
    watchElementVisibility(element) {
        if (element != null) {
            const observer = new MutationObserver(this.handleVisibilityChange);
            observer.observe(element, {
                attributes: true,
                attributeFilter: ['style', 'class', 'hidden'],
            });
            this.mutationObservers = [...this.mutationObservers, observer];
            this.watchElementVisibility(element.parentElement);
        }
    }
    handleVisibilityChange() {
        if (this.targetElement != null) {
            this.emitVisibilityChange(this.targetElement);
        }
    }
    emitVisibilityChange(element) {
        const visible = element != null ? this.isVisible(element) : true;
        console.debug(`Detected visibility change [visible={${visible}}]`);
        this.callback(visible);
    }
}
//# sourceMappingURL=visibilityObserver.js.map
