(function () {
    'use strict';

    var iafwzdeskDeactivateUrl = '';

    function iafwzdeskDeactivateInit() {
        if (typeof iafwzdesk_deactivate === 'undefined') return;

        var basename = iafwzdesk_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();
            iafwzdeskDeactivateUrl = link.href;
            iafwzdeskDeactivateBuild();
            iafwzdeskDeactivateShow();
        });
    }

    function iafwzdeskDeactivateBuild() {
        if (document.getElementById('iafwzdesk-deactivate-overlay')) return;

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

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

        var overlay  = document.getElementById('iafwzdesk-deactivate-overlay');
        var iframe   = document.getElementById('iafwzdesk-deactivate-iframe');
        var spinner  = document.getElementById('iafwzdesk-deactivate-spinner');
        var closeBtn = overlay.querySelector('.iafwzdesk-deactivate-close');
        var skipBtn  = overlay.querySelector('.iafwzdesk-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-with-zoho-desk';

        closeBtn.addEventListener('click', iafwzdeskDeactivateClose);

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

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

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

    function iafwzdeskDeactivateShow() {
        var overlay = document.getElementById('iafwzdesk-deactivate-overlay');
        if (overlay) {
            overlay.classList.add('iafwzdesk-deactivate-visible');
            document.addEventListener('keydown', iafwzdeskDeactivateEsc);
        }
    }

    function iafwzdeskDeactivateClose() {
        var overlay = document.getElementById('iafwzdesk-deactivate-overlay');
        if (overlay) {
            overlay.classList.remove('iafwzdesk-deactivate-visible');
        }
        document.removeEventListener('keydown', iafwzdeskDeactivateEsc);
    }

    function iafwzdeskDeactivateEsc(e) {
        if (e.key === 'Escape') {
            iafwzdeskDeactivateClose();
        }
    }

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