(function () {
  'use strict';

  function integrazoWczoInitPopup() {
    if (document.getElementById('integrazo-wczo-popup-overlay')) return;

    var html =
      '<div id="integrazo-wczo-popup-overlay" class="integrazo-wczo-popup-overlay" role="dialog" aria-modal="true">' +
        '<div class="integrazo-wczo-popup-modal">' +
          '<button class="integrazo-wczo-popup-close" aria-label="Close">&times;</button>' +
          '<span class="integrazo-wczo-popup-lock-icon">🔒</span><br>' +
          '<p class="integrazo-wczo-popup-description"><strong class="integrazo-wczo-popup-plugin-name-inline"></strong> is available in Integrazo Pro. Try the full Pro experience before you buy.</p>' +
          '<span class="integrazo-wczo-popup-trial-badge">⏱ 24-Hour Free Trial</span>' +
          '<div class="integrazo-wczo-popup-buttons">' +
            '<button class="integrazo-wczo-popup-btn-demo">Try Demo</button>' +
            '<button class="integrazo-wczo-popup-btn-upgrade">Upgrade to Pro &rarr;</button>' +
          '</div>' +
        '</div>' +
      '</div>';

    document.body.insertAdjacentHTML('beforeend', html);

    var overlay    = document.getElementById('integrazo-wczo-popup-overlay');
    var closeBtn   = overlay.querySelector('.integrazo-wczo-popup-close');
    var demoBtn    = overlay.querySelector('.integrazo-wczo-popup-btn-demo');
    var upgradeBtn = overlay.querySelector('.integrazo-wczo-popup-btn-upgrade');

    closeBtn.addEventListener('click', integrazoWczoCloseProPopup);

    overlay.addEventListener('click', function (e) {
      if (e.target === overlay) {
        integrazoWczoCloseProPopup();
      }
    });

    demoBtn.addEventListener('click', function () {
      window.open('https://demo.integrazo.com/?plugin=woozoho', '_blank');
    });

    upgradeBtn.addEventListener('click', function () {
      window.open('https://integrazo.com/products/woocommerce-zoho-integration', '_blank');
    });
  }

  function integrazoWczoShowProPopup(pluginName) {
    integrazoWczoInitPopup();
    var overlay = document.getElementById('integrazo-wczo-popup-overlay');
    if (overlay) {
      var descEl = overlay.querySelector('.integrazo-wczo-popup-description');
      if (descEl) {
        var strong = document.createElement('strong');
        descEl.textContent = '';
        if (pluginName) {
          strong.textContent = pluginName;
          descEl.appendChild(strong);
          descEl.appendChild(document.createTextNode(' is available in Integrazo Pro. Try the full Pro experience before you buy.'));
        } else {
          strong.textContent = 'Integrazo Pro';
          descEl.appendChild(document.createTextNode('Unlock all Pro features with '));
          descEl.appendChild(strong);
          descEl.appendChild(document.createTextNode('. Try the full Pro experience before you buy.'));
        }
      }
      overlay.classList.add('integrazo-wczo-popup-visible');
      document.addEventListener('keydown', integrazoWczoHandleEsc);
    }
  }

  function integrazoWczoCloseProPopup() {
    var overlay = document.getElementById('integrazo-wczo-popup-overlay');
    if (overlay) {
      overlay.classList.remove('integrazo-wczo-popup-visible');
      document.removeEventListener('keydown', integrazoWczoHandleEsc);
    }
  }

  function integrazoWczoHandleEsc(e) {
    if (e.key === 'Escape') {
      integrazoWczoCloseProPopup();
    }
  }

  window.integrazoWczoShowProPopup  = integrazoWczoShowProPopup;
  window.integrazoWczoCloseProPopup = integrazoWczoCloseProPopup;
})();
