import { childrenIterator } from "./core/childrenIterator.js"; const waited = new Map(); const observer = new window.IntersectionObserver((entries) => { for (const entry of entries) { if (entry.isIntersecting && entry.intersectionRatio > 0.01) { const w = waited.get(entry.target); if (w) { try { w.resolve(); } catch {} } observer.unobserve(entry.target); } } }, { root: document.body }); export default class ScreenApi { static get isMobile() { const isMobile = /android|iOS|iPhone/i.test(navigator.userAgent) && !/tablet/i.test(navigator.userAgent); Object.defineProperty(ScreenApi, "isMobile", { value: isMobile }); return isMobile; } static async waitForVisible(target) { return new Promise((resolve, reject) => { waited.set(target, { resolve, reject }); observer.observe(target); }); } static getMostVisibleElement(containerDiv: HTMLElement): HTMLElement { let mostVisibleElement = null; const containerRect = containerDiv.getBoundingClientRect(); const { left, top , width, height } = containerRect; // Iterate through all direct children of the containerDiv for (const child of childrenIterator(containerDiv)) { const childRect = child.getBoundingClientRect(); const mpX = (childRect.left + childRect.width) / 2; const mpY = (childRect.top + childRect.height) / 2; if( mpX >= left && mpX <= (left + width) && mpY >= top && mpY <= (top + height) ) { return child; } } return mostVisibleElement; } }