/**
 * Rentals & Bookings for WooCommerce & Dokan - Product Rental Fields Toggle
 *
 * Handles show/hide logic for rental configuration fields
 * on Dokan product edit/create pages.
 *
 * @package WahraRentalsBookings
 */

(function () {
    'use strict';

    /**
     * Initialize rental fields toggle behavior.
     * Uses vanilla JS with event delegation for compatibility
     * with both classic Dokan and React-based Dokan dashboard.
     */
    function initRentalFields() {
        var container = document.querySelector('.wahrb-rental-fields');
        if (!container) {
            return;
        }

        var checkbox = container.querySelector('input[name="_wahrb_rental_enabled"]');
        var optionsPanel = container.querySelector('.wahrb-rental-options');
        var wizardBtn = container.querySelector('#wahrb-wizard-btn');
        var modeSelect = container.querySelector('#_wahrb_booking_mode');

        if (!checkbox || !optionsPanel) {
            return;
        }

        /**
         * Toggle rental options and wizard button visibility
         */
        function toggleRentalOptions() {
            if (checkbox.checked) {
                optionsPanel.style.display = '';
                slideDown(optionsPanel);
                if (wizardBtn) {
                    wizardBtn.classList.remove('dokan-btn-default');
                    wizardBtn.classList.add('dokan-btn-theme');
                    wizardBtn.removeAttribute('aria-disabled');
                }
            } else {
                slideUp(optionsPanel);
                if (wizardBtn) {
                    wizardBtn.classList.remove('dokan-btn-theme');
                    wizardBtn.classList.add('dokan-btn-default');
                }
            }
        }

        /**
         * Toggle booking mode fields visibility
         */
        function toggleModeFields() {
            if (!modeSelect) {
                return;
            }

            var mode = modeSelect.value;
            var modes = ['day', 'slot', 'service'];

            modes.forEach(function (m) {
                // Mode description elements
                var modeElements = container.querySelectorAll('.wahrb-mode-' + m);
                modeElements.forEach(function (el) {
                    el.style.display = (m === mode) ? '' : 'none';
                });

                // Mode-specific field groups
                var fieldGroup = container.querySelector('.wahrb-' + m + '-fields');
                if (fieldGroup) {
                    fieldGroup.style.display = (m === mode) ? '' : 'none';
                }
            });
        }

        /**
         * Simple slideDown animation (vanilla JS)
         */
        function slideDown(el) {
            el.style.display = '';
            el.style.overflow = 'hidden';
            var height = el.scrollHeight;
            el.style.height = '0px';
            el.style.transition = 'height 0.3s ease';

            // Force reflow
            el.offsetHeight; // eslint-disable-line no-unused-expressions

            el.style.height = height + 'px';

            setTimeout(function () {
                el.style.height = '';
                el.style.overflow = '';
                el.style.transition = '';
            }, 300);
        }

        /**
         * Simple slideUp animation (vanilla JS)
         */
        function slideUp(el) {
            el.style.overflow = 'hidden';
            el.style.height = el.scrollHeight + 'px';
            el.style.transition = 'height 0.3s ease';

            // Force reflow
            el.offsetHeight; // eslint-disable-line no-unused-expressions

            el.style.height = '0px';

            setTimeout(function () {
                el.style.display = 'none';
                el.style.height = '';
                el.style.overflow = '';
                el.style.transition = '';
            }, 300);
        }

        // Bind events
        checkbox.addEventListener('change', toggleRentalOptions);

        if (modeSelect) {
            modeSelect.addEventListener('change', toggleModeFields);
        }

        // Set initial state (ensure correct visibility on page load)
        if (!checkbox.checked) {
            optionsPanel.style.display = 'none';
        }
    }

    // Initialize when DOM is ready
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initRentalFields);
    } else {
        initRentalFields();
    }

    // Also re-initialize on Dokan SPA navigation (React dashboard)
    // Dokan 4.2+ uses a React SPA that may load content dynamically
    var observer = new MutationObserver(function (mutations) {
        for (var i = 0; i < mutations.length; i++) {
            var added = mutations[i].addedNodes;
            for (var j = 0; j < added.length; j++) {
                if (added[j].nodeType === 1 && added[j].querySelector && added[j].querySelector('.wahrb-rental-fields')) {
                    initRentalFields();
                    return;
                }
            }
        }
    });

    observer.observe(document.body || document.documentElement, {
        childList: true,
        subtree: true
    });
})();
