let remainingImages;
export async function waitUntilImagesDone() {
    let tries = 1000;
    while (tries > 0) {
        tries--;
        if (remainingImages === 0) {
            return;
        }
        else {
            await new Promise(resolve => setTimeout(resolve, 100));
        }
    }
    throw Error('Images not loaded');
}
export function updateLargeImageWarnings(answer) {
    answer === null || answer === void 0 ? void 0 : answer.querySelectorAll('.full-size-image').forEach(el => el.remove());
    const images = (answer === null || answer === void 0 ? void 0 : answer.querySelectorAll('img')) || [];
    remainingImages = images.length;
    images.forEach(img => {
        const measure = () => requestAnimationFrame(() => requestAnimationFrame(() => updateImageStatus(img)));
        if (img.complete && img.naturalWidth > 0) {
            measure();
        }
        else {
            img.addEventListener('load', measure, { once: true });
        }
    });
}
function updateImageStatus(img) {
    remainingImages--;
    const wrapper = img.parentElement;
    const nextSibling = wrapper === null || wrapper === void 0 ? void 0 : wrapper.nextSibling;
    const hasFullSizeLink = nextSibling instanceof HTMLElement && nextSibling.classList.contains('full-size-image');
    const isLargeImage = img.naturalWidth > img.width + 1; // allow for rounding errors
    wrapper === null || wrapper === void 0 ? void 0 : wrapper.classList.toggle('e-large-image', isLargeImage);
    if (hasFullSizeLink) {
        nextSibling.remove();
    }
    if (isLargeImage) {
        wrapper === null || wrapper === void 0 ? void 0 : wrapper.insertAdjacentHTML('afterend', `<div class="full-size-image"><a target="_blank" href="${img.src}"></a></div>`);
    }
}
//# sourceMappingURL=largeImageDetector.js.map