/**
 * @file assets/js/adminjs/upgrade-nudge.js
 * Dismissal handler for FREE-user upgrade nudge notices (.mbs-upgrade-nudge).
 * Fires AJAX on both the "Remind me later" button and the WP core X-button.
 * @package MyBookShowroom
 * @since   1.1.0
 */

/* global ajaxurl, jQuery */

document.addEventListener( 'DOMContentLoaded', function () {
	'use strict';

	document.querySelectorAll( '.mbs-upgrade-nudge' ).forEach( function ( notice ) {
		var nonce     = notice.dataset.nonce;
		var milestone = notice.dataset.milestone;

		function dismiss() {
			jQuery.post( ajaxurl, {
				action    : 'mbs_dismiss_upgrade_nudge',
				nonce     : nonce,
				milestone : milestone,
			} );
			notice.remove();
		}

		// "Remind me later" button — explicit dismiss trigger.
		var remindBtn = notice.querySelector( '.mbs-nudge-dismiss-btn' );
		if ( remindBtn ) {
			remindBtn.addEventListener( 'click', dismiss );
		}

		// WP core X-button (.notice-dismiss) — also fires AJAX so the notice
		// does not reappear on the next page load.
		var xBtn = notice.querySelector( '.notice-dismiss' );
		if ( xBtn ) {
			xBtn.addEventListener( 'click', dismiss );
		}
	} );
} );
