const CUVEE_FRAME_ID = 'cuvees-content';
const CUVEE_SECTION_ID = 'cuvees';

let registered = false;

function scrollToCuveeSection() {
    const section = document.getElementById(CUVEE_SECTION_ID);

    if (!section) {
        return;
    }

    section.scrollIntoView({
        block: 'start',
        behavior: 'smooth',
    });
}

export default function registerCuveeFrameScroll() {
    if (registered) {
        return;
    }

    registered = true;

    document.addEventListener('turbo:frame-load', (event) => {
        if (!(event.target instanceof HTMLElement) || event.target.id !== CUVEE_FRAME_ID) {
            return;
        }

        requestAnimationFrame(scrollToCuveeSection);
    });
}
