(function () {
    'use strict';

    function ready(fn) {
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', fn);
            return;
        }
        fn();
    }

    function setStatus(node, message, isError) {
        if (!node) {
            return;
        }

        node.textContent = message || '';
        node.classList.toggle('is-error', !!isError);
    }

    ready(function () {
        var config = window.nxtEventPlannerAdminBanner || {};
        var banner = document.querySelector('.nxt-event-planner-admin-banner');
        var button = document.querySelector('.nxt-event-planner-admin-banner__btn--install');
        var dismissButton = document.querySelector('.nxt-event-planner-admin-banner__dismiss');
        var status = document.querySelector('.nxt-event-planner-admin-banner__status');

        if (!banner || !config.ajaxUrl) {
            return;
        }

        if (dismissButton && config.dismissNonce) {
            dismissButton.addEventListener('click', function (event) {
                event.preventDefault();

                banner.remove();

                var dismissPayload = new URLSearchParams();
                dismissPayload.append('action', 'nxt_event_planner_dismiss_admin_banner');
                dismissPayload.append('nonce', config.dismissNonce);
                dismissPayload.append('notice', config.noticeKey || '');

                fetch(config.ajaxUrl, {
                    method: 'POST',
                    credentials: 'same-origin',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                    },
                    body: dismissPayload.toString()
                }).catch(function () {
                    // Intentionally ignore dismiss persistence failures.
                });
            });
        }

        if (!button || !config.nonce) {
            return;
        }

        button.addEventListener('click', function (event) {
            event.preventDefault();

            if (button.classList.contains('is-loading') || button.classList.contains('is-complete')) {
                return;
            }

            button.classList.add('is-loading');
            button.textContent = config.installingText || 'Installing...';
            setStatus(status, config.workingText || 'Working...', false);

            var payload = new URLSearchParams();
            payload.append('action', 'nxt_event_planner_install_activate_novex');
            payload.append('nonce', config.nonce);

            fetch(config.ajaxUrl, {
                method: 'POST',
                credentials: 'same-origin',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                },
                body: payload.toString()
            })
                .then(function (response) {
                    return response.json();
                })
                .then(function (result) {
                    if (!result || !result.success) {
                        throw new Error((result && result.data && result.data.message) ? result.data.message : (config.errorText || 'Could not install plugin.'));
                    }

                    button.classList.remove('is-loading');
                    button.classList.add('is-complete');
                    button.textContent = config.activatedText || 'Plugin Activated';
                    button.setAttribute('aria-disabled', 'true');
                    setStatus(status, (result.data && result.data.message) ? result.data.message : (config.successText || 'Plugin installed and activated.'), false);

                    if (result.data && result.data.redirect) {
                        window.location.href = result.data.redirect;
                    }
                })
                .catch(function (error) {
                    button.classList.remove('is-loading');
                    button.textContent = config.installText || 'Install Plugin';
                    setStatus(status, error.message || (config.errorText || 'Could not install plugin.'), true);
                });
        });
    });
})();
