(function() {
    'use strict';

    var featureConfig = (window.ciboostUpgradeModal && window.ciboostUpgradeModal.features) || {};

    function openUpgradeModal(feature) {
        var modal = document.getElementById('ciboost-upgrade-modal');
        var config = featureConfig[feature];

        if (!modal || !config) {
            return;
        }

        document.getElementById('ciboost-upgrade-title').textContent = config.title;
        document.getElementById('ciboost-upgrade-description').textContent = config.description;

        var featuresList = document.getElementById('ciboost-upgrade-features');
        featuresList.innerHTML = '';
        config.features.forEach(function(featureText) {
            var li = document.createElement('li');
            var icon = document.createElement('span');
            icon.className = 'dashicons dashicons-yes-alt';
            li.appendChild(icon);
            li.appendChild(document.createTextNode(' ' + featureText));
            featuresList.appendChild(li);
        });

        modal.style.display = 'flex';
        document.body.style.overflow = 'hidden';
    }

    function closeUpgradeModal() {
        var modal = document.getElementById('ciboost-upgrade-modal');

        if (modal) {
            modal.style.display = 'none';
            document.body.style.overflow = '';
        }
    }

    document.addEventListener('DOMContentLoaded', function() {
        document.addEventListener('click', function(event) {
            var trigger = event.target.closest('.ciboost-upgrade-trigger');

            if (trigger) {
                event.preventDefault();
                openUpgradeModal(trigger.getAttribute('data-feature'));
                return;
            }

            if (event.target.classList.contains('ciboost-upgrade-overlay')) {
                closeUpgradeModal();
                return;
            }

            if (event.target.closest('.ciboost-upgrade-close')) {
                closeUpgradeModal();
            }
        });

        document.addEventListener('keydown', function(event) {
            if (event.key === 'Escape') {
                closeUpgradeModal();
            }
        });
    });
})();
