/**
 * Informationsdisk frontend helpers.
 */
(function () {
    'use strict';

    document.addEventListener('click', function (event) {
        var trigger = event.target.closest('.maca-info-event-booking-trigger');
        if (!trigger) {
            return;
        }

        event.preventDefault();

        var shell = document.querySelector('.maca-info-events-booking-shell .maca-table-booking');
        if (!shell) {
            return;
        }

        if (typeof window.macaMenulistInitTableBooking === 'function') {
            window.macaMenulistInitTableBooking(shell);
        }

        var bookingDate = trigger.getAttribute('data-booking-date') || '';
        var dateInput = shell.querySelector('[name="booking_date"]');
        if (dateInput && bookingDate) {
            dateInput.value = bookingDate;
            dateInput.dispatchEvent(new Event('change', { bubbles: true }));
        }

        var modal = shell.querySelector('.maca-table-booking-modal');
        if (modal) {
            modal.hidden = false;
            document.body.classList.add('maca-booking-modal-open');
            return;
        }

        var openTrigger = shell.querySelector('.maca-table-booking-trigger');
        if (openTrigger) {
            openTrigger.click();
        }
    });

    function scrollToEventDetail() {
        var hash = window.location.hash;

        if (!hash || hash.charAt(0) !== '#') {
            return;
        }

        var target = document.querySelector(hash);

        if (target && target.classList.contains('maca-info-event-detail--collapsible')) {
            target.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
        }
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', scrollToEventDetail);
    } else {
        scrollToEventDetail();
    }

    window.addEventListener('hashchange', scrollToEventDetail);

    function restartBannerTicker(banner) {
        if (!banner || !banner.classList.contains('maca-info-news-list--scroll')) {
            return;
        }

        var inner = banner.querySelector('.maca-info-news-banner-track-inner');

        if (!inner) {
            return;
        }

        inner.style.animation = 'none';
        void inner.offsetHeight;
        inner.style.animation = '';
    }

    function applyNewsLayoutChrome() {
        var banner = document.querySelector('.maca-info-news-list--banner:not(.maca-info-news-list--preview)');

        if (banner) {
            if (!banner.querySelector('.maca-info-news-item')) {
                return;
            }

            if (banner.parentNode !== document.body) {
                document.body.insertBefore(banner, document.body.firstChild);
            }

            document.body.classList.add('maca-has-info-news-banner');

            var setBannerHeight = function () {
                var node = document.querySelector('.maca-info-news-list--banner:not(.maca-info-news-list--preview)');
                if (!node) {
                    return;
                }
                document.body.style.setProperty('--maca-info-news-banner-height', node.offsetHeight + 'px');
                restartBannerTicker(node);
            };

            setBannerHeight();
            window.addEventListener('resize', setBannerHeight);
        }

        var sidebar = document.querySelector('.maca-info-news-list--sidebar-left:not(.maca-info-news-list--preview), .maca-info-news-list--sidebar-right:not(.maca-info-news-list--preview)');

        if (sidebar) {
            if (sidebar.classList.contains('maca-info-news-list--sidebar-left')) {
                document.body.classList.add('maca-has-info-news-sidebar-left');
            }

            if (sidebar.classList.contains('maca-info-news-list--sidebar-right')) {
                document.body.classList.add('maca-has-info-news-sidebar-right');
            }

            var sidebarWidth = getComputedStyle(sidebar).getPropertyValue('--maca-info-news-sidebar-width').trim() || '17.5rem';
            document.body.style.setProperty('--maca-info-news-sidebar-width', sidebarWidth);

            var setSidebarHeight = function () {
                document.body.style.setProperty('--maca-info-news-sidebar-height', sidebar.offsetHeight + 'px');
            };

            setSidebarHeight();
            window.addEventListener('resize', setSidebarHeight);
        }
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', applyNewsLayoutChrome);
    } else {
        applyNewsLayoutChrome();
    }

    document.addEventListener('click', function (event) {
        var bannerTrigger = event.target.closest('.maca-info-news-banner-trigger');

        if (!bannerTrigger) {
            return;
        }

        var modalId = bannerTrigger.getAttribute('data-news-modal') || '';
        var dialog = modalId ? document.getElementById(modalId) : null;

        if (!dialog) {
            return;
        }

        if (typeof dialog.showModal === 'function') {
            dialog.showModal();
        } else {
            dialog.setAttribute('open', 'open');
        }
    });
})();
