import Plugin from 'src/plugin-system/plugin.class';

export default class BcAquaInstagramWidget extends Plugin {
    init() {
        this.initObserver();
    }

    initObserver() {
        const posts = this.el.querySelectorAll('video[bc-data-autoplay]');

        if (!posts.length) return;

        const observer = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                const video = entry.target;
                if (entry.isIntersecting) {
                    video.play().catch(() => {
                        video.muted = true;
                        video.play();
                    });
                } else {
                    video.pause();
                }
            });
        }, { threshold: 0.5 });

        posts.forEach(video => observer.observe(video));
    }
}