/**
 * Audace — Scroll-jack long text inside a pinned `.is-style-slide`.
 * On wheel, find the slide currently pinned at the top and, if one of
 * its columns overflows, translate that column instead of scrolling.
 */
(function () {
    'use strict';

    var colsToTranslate = [];

    function refresh() {
        colsToTranslate = [];
        if (window.innerWidth < 782) return;

        const cols = document.querySelectorAll('.is-style-slide .wp-block-column');

        // Compute max height
        const adminBar = document.getElementById('wpadminbar');
        const header = document.querySelector('.wp-block-template-part.site-header, header.wp-block-template-part, #masthead');
        const adminBarH = adminBar ? adminBar.getBoundingClientRect().height : 0;
        const headerH = header ? header.getBoundingClientRect().height : 0;
        const topH = adminBarH + headerH;
        const maxH = window.innerHeight - topH;

        for (let i = 0; i < cols.length; i++) {
            const col = cols[i];
            if (col.scrollHeight > maxH) {
                colsToTranslate.push(col);
            }
        }
    }

    window.addEventListener('load', refresh);
    window.addEventListener('resize', refresh);
    window.addEventListener('scroll', refresh, { passive: true });
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', refresh);
    } else {
        refresh();
    }
})();
