(function () {
  'use strict';

  var integrazoWczoDeactivateUrl = '';

  function integrazoWczoDeactivateInit() {
    if (typeof integrazo_wczo_deactivate === 'undefined') return;

    var basename = integrazo_wczo_deactivate.plugin_basename;
    var row = document.querySelector('tr[data-plugin="' + basename + '"]');
    if (!row) return;

    var link = row.querySelector('a[href*="action=deactivate"]');
    if (!link) return;

    link.addEventListener('click', function (e) {
      e.preventDefault();
      integrazoWczoDeactivateUrl = link.href;
      integrazoWczoDeactivateBuild();
      integrazoWczoDeactivateShow();
    });
  }

  function integrazoWczoDeactivateBuild() {
    if (document.getElementById('integrazo-wczo-deactivate-overlay')) return;

    var html =
      '<div id="integrazo-wczo-deactivate-overlay" class="integrazo-wczo-deactivate-overlay" role="dialog" aria-modal="true">' +
        '<div class="integrazo-wczo-deactivate-modal">' +
          '<div class="integrazo-wczo-deactivate-header">' +
            '<p class="integrazo-wczo-deactivate-title">Deactivate WooCommerce to Zoho Integration?</p>' +
            '<p class="integrazo-wczo-deactivate-subtitle">Please share why you\'re deactivating</p>' +
            '<button class="integrazo-wczo-deactivate-close" aria-label="Close">&times;</button>' +
          '</div>' +
          '<div class="integrazo-wczo-deactivate-body">' +
            '<div class="integrazo-wczo-deactivate-spinner" id="integrazo-wczo-deactivate-spinner"></div>' +
            '<iframe class="integrazo-wczo-deactivate-iframe" id="integrazo-wczo-deactivate-iframe" frameborder="0" allowtransparency="true" scrolling="auto"></iframe>' +
          '</div>' +
          '<div class="integrazo-wczo-deactivate-footer">' +
            '<button class="integrazo-wczo-deactivate-skip">Deactivate</button>' +
          '</div>' +
        '</div>' +
      '</div>';

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

    var overlay  = document.getElementById('integrazo-wczo-deactivate-overlay');
    var iframe   = document.getElementById('integrazo-wczo-deactivate-iframe');
    var spinner  = document.getElementById('integrazo-wczo-deactivate-spinner');
    var closeBtn = overlay.querySelector('.integrazo-wczo-deactivate-close');
    var skipBtn  = overlay.querySelector('.integrazo-wczo-deactivate-skip');

    // Set src after attaching load listener to avoid race condition
    iframe.addEventListener('load', function () {
      if (spinner) spinner.style.display = 'none';
    });
    iframe.src = 'https://creatorapp.zohopublic.in/plugcrux/support/form-embed/Integrazo_Deactivation_Feedback/p5gJMO9HVbGDSjHTpnrpnFWQThnQ53jSCgygr60wK3q8nwdaaPgJEMYExsT9A44GOynGXQCKRdePX3VCupA5eqFQN4P2MPb4PNHP?Plugin_Name=integrazo-wc-zoho-integration';

    closeBtn.addEventListener('click', integrazoWczoDeactivateClose);

    skipBtn.addEventListener('click', function () {
      window.location.href = integrazoWczoDeactivateUrl;
    });

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

    window.addEventListener('message', function (e) {
      if (e.data === 'integrazo_deactivated') {
        window.location.href = integrazoWczoDeactivateUrl;
      }
    });
  }

  function integrazoWczoDeactivateShow() {
    var overlay = document.getElementById('integrazo-wczo-deactivate-overlay');
    if (overlay) {
      overlay.classList.add('integrazo-wczo-deactivate-visible');
      document.addEventListener('keydown', integrazoWczoDeactivateEsc);
    }
  }

  function integrazoWczoDeactivateClose() {
    var overlay = document.getElementById('integrazo-wczo-deactivate-overlay');
    if (overlay) {
      overlay.classList.remove('integrazo-wczo-deactivate-visible');
    }
    document.removeEventListener('keydown', integrazoWczoDeactivateEsc);
  }

  function integrazoWczoDeactivateEsc(e) {
    if (e.key === 'Escape') {
      integrazoWczoDeactivateClose();
    }
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', integrazoWczoDeactivateInit);
  } else {
    integrazoWczoDeactivateInit();
  }
})();
