(function() {
    function init() {
        var btn = document.getElementById('am-setup-reset-btn');
        if (!btn) { return; }
        if (typeof amaiSetup === 'undefined') { return; }
        btn.addEventListener('click', function() {
            btn.disabled = true;
            btn.textContent = 'Resetting...';
            var fd = new FormData();
            fd.append('action', 'amai_nudge_reset_setup');
            fd.append('_ajax_nonce', amaiSetup.nonce);
            fetch(amaiSetup.ajaxUrl, { method: 'POST', body: fd })
                .then(function(r) { return r.json(); })
                .then(function(data) {
                    if (data.success) { window.location.reload(); }
                    else { btn.disabled = false; btn.textContent = 'Reset setup checklist'; }
                })
                .catch(function() { btn.disabled = false; btn.textContent = 'Reset setup checklist'; });
        });
    }
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();
