(function () {
  'use strict';

  function integrazoWzInitPopup() {
    if (document.getElementById('integrazo-wz-popup-overlay')) return;

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

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

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

    closeBtn.addEventListener('click', integrazoWzCloseProPopup);

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

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

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

  function integrazoWzShowProPopup(pluginName) {
    integrazoWzInitPopup();
    var overlay = document.getElementById('integrazo-wz-popup-overlay');
    if (overlay) {
      var descEl = overlay.querySelector('.integrazo-wz-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 Zoho Apps Pro. Try the full Pro experience before you buy.'));
        } else {
          strong.textContent = 'Zoho Apps 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-wz-popup-visible');
      document.addEventListener('keydown', integrazoWzHandleEsc);
    }
  }

  function integrazoWzCloseProPopup() {
    var overlay = document.getElementById('integrazo-wz-popup-overlay');
    if (overlay) {
      overlay.classList.remove('integrazo-wz-popup-visible');
      document.removeEventListener('keydown', integrazoWzHandleEsc);
    }
  }

  function integrazoWzHandleEsc(e) {
    if (e.key === 'Escape') {
      integrazoWzCloseProPopup();
    }
  }

  window.integrazoWzShowProPopup  = integrazoWzShowProPopup;
  window.integrazoWzCloseProPopup = integrazoWzCloseProPopup;
})();
