/**
 * Admin booking availability tab — load overview via AJAX.
 */
(function () {
    'use strict';

    function initAvailabilityLoader() {
        var config = window.macaMenulistAdminBookingAvailability || {};
        var strings = config.strings || {};
        var root = document.getElementById('maca-bookings-availability');
        var loadingEl = document.getElementById('maca-bookings-availability-loading');
        var contentEl = document.getElementById('maca-bookings-availability-content');
        var messageEl = document.getElementById('maca-bookings-availability-message');

        if (!root || !contentEl) {
            return;
        }

        function setMessage(text) {
            if (!messageEl) {
                return;
            }

            if (!text) {
                messageEl.textContent = '';
                messageEl.hidden = true;
                return;
            }

            messageEl.textContent = text;
            messageEl.hidden = false;
        }

        function hideLoading() {
            root.classList.add('is-loaded');
            root.setAttribute('aria-busy', 'false');

            if (loadingEl) {
                loadingEl.hidden = true;
                loadingEl.style.display = 'none';
                loadingEl.setAttribute('aria-hidden', 'true');

                var spinner = loadingEl.querySelector('.spinner');
                if (spinner) {
                    spinner.classList.remove('is-active');
                }
            }
        }

        function showLoading(show) {
            if (show) {
                root.classList.remove('is-loaded');
                root.setAttribute('aria-busy', 'true');

                if (loadingEl) {
                    loadingEl.hidden = false;
                    loadingEl.style.display = '';
                    loadingEl.removeAttribute('aria-hidden');

                    var spinner = loadingEl.querySelector('.spinner');
                    if (spinner) {
                        spinner.classList.add('is-active');
                    }
                }

                contentEl.hidden = true;
                return;
            }

            hideLoading();
        }

        function loadAvailability() {
            if (!config.ajaxUrl) {
                hideLoading();
                setMessage(strings.error || 'Kunde inte ladda lediga platser. Försök igen.');
                return;
            }

            showLoading(true);
            setMessage('');

            var body = new FormData();
            body.append('action', 'maca_admin_booking_availability');
            body.append('nonce', config.nonce || '');

            fetch(config.ajaxUrl, {
                method: 'POST',
                credentials: 'same-origin',
                body: body
            })
                .then(function (response) {
                    if (!response.ok) {
                        throw new Error('http_error');
                    }

                    return response.json();
                })
                .then(function (payload) {
                    if (!payload || !payload.success || !payload.data || typeof payload.data.html !== 'string') {
                        throw new Error('invalid_response');
                    }

                    contentEl.innerHTML = payload.data.html;
                    contentEl.hidden = false;
                })
                .catch(function () {
                    contentEl.hidden = true;
                    setMessage(strings.error || 'Kunde inte ladda lediga platser. Försök igen.');
                })
                .finally(function () {
                    hideLoading();
                });
        }

        loadAvailability();
    }

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