(function () {
    'use strict';

    var iafwzcDeactivateUrl = '';

    function iafwzcDeactivateInit() {
        if (typeof iafwzc_deactivate === 'undefined') return;

        var basename = iafwzc_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();
            iafwzcDeactivateUrl = link.href;
            iafwzcDeactivateBuild();
            iafwzcDeactivateShow();
        });
    }

    function iafwzcDeactivateBuild() {
        if (document.getElementById('iafwzc-deactivate-overlay')) return;

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

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

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

        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=integrate-any-form-with-zoho-crm';

        closeBtn.addEventListener('click', iafwzcDeactivateClose);

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

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

        window.addEventListener('message', function (e) {
            if (e.data === 'integrazo_deactivated') {
                var activeOverlay = document.getElementById('iafwzc-deactivate-overlay');
                if (activeOverlay && activeOverlay.classList.contains('iafwzc-deactivate-visible')) {
                    window.location.href = iafwzcDeactivateUrl;
                }
            }
        });
    }

    function iafwzcDeactivateShow() {
        var overlay = document.getElementById('iafwzc-deactivate-overlay');
        if (overlay) {
            overlay.classList.add('iafwzc-deactivate-visible');
            document.addEventListener('keydown', iafwzcDeactivateEsc);
        }
    }

    function iafwzcDeactivateClose() {
        var overlay = document.getElementById('iafwzc-deactivate-overlay');
        if (overlay) {
            overlay.classList.remove('iafwzc-deactivate-visible');
        }
        document.removeEventListener('keydown', iafwzcDeactivateEsc);
    }

    function iafwzcDeactivateEsc(e) {
        if (e.key === 'Escape') {
            iafwzcDeactivateClose();
        }
    }

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