// Auto show/hide decision for the page minimap.
//
// The minimap sits next to the page iframe, so showing it narrows the page.
// Responsive host pages reflow at the narrower width, which changes the very
// content height the visibility decision is based on. On pages where the
// narrower layout is SHORTER (a breakpoint collapses content), a naive
// show/hide threshold oscillates: tall page -> show minimap -> page narrows
// and becomes short -> hide minimap -> page widens and becomes tall -> show...
//
// To break that cycle without giving up auto-hide, the resolver remembers the
// last measurement taken in each visibility state. A hide is suppressed when
// the remembered hidden-state measurement — taken at the width the page
// returns to once the minimap leaves — already exceeded the show threshold,
// because hiding would deterministically re-trigger the show. The memory is
// host-agnostic: it only uses measurements the bridge already reports.
//
// Content edits made while the minimap is shown make the hidden-state memory
// stale. They are told apart from minimap-induced reflows by the host
// viewport width: an edit changes the content height at an UNCHANGED width,
// a reflow changes both. On an edit the hidden-state memory is dropped so a
// genuinely shrunken page can still hide the minimap.
/** Content must exceed the viewport by this much before the minimap shows. */
export const MINIMAP_SHOW_HYSTERESIS_PX = 100;
/**
 * Height changes at an unchanged host width larger than this are treated as
 * content edits (invalidating the hidden-state memory) rather than layout
 * jitter from scrollbars or late-loading assets.
 */
export const MINIMAP_CONTENT_EDIT_TOLERANCE_PX = 4;
export function resolveMiniMapVisibility(visible, measurement, memory) {
    const { contentHeight, contentWidth, clientHeight } = measurement;
    const showThreshold = clientHeight + MINIMAP_SHOW_HYSTERESIS_PX;
    const hideThreshold = clientHeight;
    if (!visible) {
        const nextMemory = {
            ...memory,
            hidden: { contentHeight, contentWidth },
        };
        if (contentHeight > showThreshold) {
            // The shown-state memory belongs to the previous shown period; the
            // first shown measurement re-establishes it.
            return { visible: true, memory: { ...nextMemory, shown: undefined } };
        }
        return { visible: false, memory: nextMemory };
    }
    let hidden = memory.hidden;
    const previousShown = memory.shown;
    if (hidden &&
        previousShown &&
        previousShown.contentWidth === contentWidth &&
        Math.abs(previousShown.contentHeight - contentHeight) >
            MINIMAP_CONTENT_EDIT_TOLERANCE_PX) {
        // Content changed at an unchanged host width: a real edit, not the
        // reflow caused by the minimap itself. The hidden-state measurement no
        // longer describes the current content.
        hidden = undefined;
    }
    const nextMemory = {
        hidden,
        shown: { contentHeight, contentWidth },
    };
    if (contentHeight > hideThreshold) {
        return { visible: true, memory: nextMemory };
    }
    if (hidden && hidden.contentHeight > showThreshold) {
        // Hiding would widen the page back to a layout that is known to exceed
        // the show threshold — the toggle would oscillate. Stay shown.
        return { visible: true, memory: nextMemory };
    }
    return { visible: false, memory: nextMemory };
}
//# sourceMappingURL=minimapVisibility.js.map