(function() {
    'use strict';

    // Get list of auto-applied coupon codes from PHP
    const autoAppliedCoupons = window.couponsPlus?.autoAppliedCoupons || [];

    function hideRemoveButtons() {
        // Find all coupon items
        const couponItems = document.querySelectorAll('.wc-block-components-totals-discount__coupon-list-item');
        
        couponItems.forEach((item) => {
            // Get the coupon code
            const codeElement = item.querySelector('.wc-block-components-chip__text');
            if (!codeElement) {
                return;
            }
            
            const couponCode = codeElement.textContent.trim();
            
            // If this is an auto-applied coupon, hide the remove button
            if (autoAppliedCoupons.includes(couponCode)) {
                const removeButton = item.querySelector('.wc-block-components-chip__remove');
                if (removeButton) {
                    removeButton.style.display = 'none';
                }
                
                // Remove the "is-removable" class
                item.classList.remove('is-removable');
                
                // Get the current padding-left and apply it to padding-right for even padding
                const computedStyle = window.getComputedStyle(item);
                const paddingLeft = computedStyle.paddingLeft;
                item.style.paddingRight = paddingLeft;
            }
        });
    }

    // Run initially
    const init = () => {
        hideRemoveButtons();
        
        // Set up observer for dynamic changes
        const observer = new MutationObserver(hideRemoveButtons);
        
        const cartElement = document.querySelector('.wc-block-cart') || 
                           document.querySelector('.wc-block-checkout') ||
                           document.body;
        
        observer.observe(cartElement, {
            childList: true,
            subtree: true
        });
    };

    // Wait for DOM and WP data to be ready
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }

    // Also listen to cart updates if wp.data is available
    if (window.wp?.data) {
        window.wp.data.subscribe(() => {
            setTimeout(hideRemoveButtons, 100);
        });
    }

})();
