/* ==================================================
   Nuclear Fancy Pants Edition™ -- NUCLEAR+++
   File: /assets/js/amai-blog-nudge.js
   Description: Blog nudge admin notice -- title pick and dismiss handlers
   Last Edited: 2026-04-25
   ================================================== */
document.addEventListener('DOMContentLoaded', function () {
    var notice   = document.getElementById('amai-blog-nudge-notice');
    var dismiss  = document.getElementById('amai-blog-nudge-dismiss');
    var ajaxurl  = window.amaiBlogNudge ? window.amaiBlogNudge.ajaxurl : window.ajaxurl;
    var nonce    = window.amaiBlogNudge ? window.amaiBlogNudge.nonce   : '';

    if (!notice) { return; }

    var slotId = notice.getAttribute('data-slot-id') || '0';

    // Title pick
    notice.addEventListener('click', function (e) {
        var btn = e.target.closest('.amai-blog-nudge-title-btn');
        if (!btn) { return; }
        if (btn.disabled) { return; }

        var title = btn.getAttribute('data-title') || '';
        if (!title) { return; }

        // Mark selected
        notice.querySelectorAll('.amai-blog-nudge-title-btn').forEach(function (b) {
            b.classList.remove('selected');
            b.disabled = true;
        });
        btn.classList.add('selected');
        btn.textContent = 'Starting...';

        var body = new URLSearchParams();
        body.append('action',   'amai_blog_nudge_pick');
        body.append('nonce',    nonce);
        body.append('title',    title);
        body.append('slot_id',  slotId);

        fetch(ajaxurl, {
            method:  'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body:    body.toString(),
        })
        .then(function (r) { return r.json(); })
        .then(function (data) {
            if (data.success && data.data && data.data.edit_url) {
                window.location.href = data.data.edit_url;
            } else {
                notice.querySelectorAll('.amai-blog-nudge-title-btn').forEach(function (b) {
                    b.disabled = false;
                });
                btn.classList.remove('selected');
                btn.textContent = title;
            }
        })
        .catch(function () {
            notice.querySelectorAll('.amai-blog-nudge-title-btn').forEach(function (b) {
                b.disabled = false;
            });
            btn.classList.remove('selected');
            btn.textContent = title;
        });
    });

    // Dismiss
    if (dismiss) {
        dismiss.addEventListener('click', function () {
            var body = new URLSearchParams();
            body.append('action',  'amai_blog_nudge_dismiss');
            body.append('nonce',   nonce);
            body.append('slot_id', slotId);

            fetch(ajaxurl, {
                method:  'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body:    body.toString(),
            })
            .catch(function () {});

            notice.style.display = 'none';
        });
    }
});