import { deepReady, waitAfterFramePaint } from "../helpers";
import { ListenerAbstract } from "../types/listener";
export class BalVisibilityListener extends ListenerAbstract {
    constructor() {
        super(...arguments);
        this.waitAfterFramePrint = false;
        this.intersectionObserver = undefined;
        this.intersectionCallback = (records) => {
            const isIntersecting = records.some(record => record.isIntersecting);
            if (isIntersecting) {
                this.notify();
            }
        };
    }
    async connect(el) {
        super.connect(el);
        if (typeof IntersectionObserver === 'undefined') {
            return;
        }
        if (this.waitAfterFramePrint) {
            await deepReady(el);
            await waitAfterFramePaint();
        }
        this.destroyMutationObserver();
        this.intersectionObserver = new IntersectionObserver(this.intersectionCallback, {
            root: null, // Use the viewport as the container
            threshold: 0.5, // Trigger when 50% of the element is visible
        });
        this.intersectionObserver.observe(el);
    }
    disconnect() {
        super.disconnect();
        this.destroyMutationObserver();
    }
    destroyMutationObserver() {
        if (this.intersectionObserver !== undefined) {
            this.intersectionObserver.disconnect();
            this.intersectionObserver = undefined;
        }
    }
}
