/**
 * Donatico — Upsell prompts dismiss handler
 *
 * Catches the WordPress "dismiss" click on Donatico upsell notices and
 * persists the dismissal server-side via AJAX so it doesn't reappear on
 * subsequent page loads.
 *
 * Depends on:
 *  - jQuery (already loaded by WordPress admin)
 *  - window.donaticoUpsell localized object with { ajaxUrl, nonce }
 *
 * @package Donatico
 */

( function ( $ ) {
	'use strict';

	if ( typeof window.donaticoUpsell === 'undefined' ) {
		return;
	}

	$( document ).on( 'click', '.donatico-upsell-notice .notice-dismiss', function () {
		var $notice = $( this ).closest( '.donatico-upsell-notice' );
		var key = $notice.attr( 'data-donatico-upsell-key' );

		if ( ! key ) {
			return;
		}

		$.ajax( {
			url: window.donaticoUpsell.ajaxUrl,
			type: 'POST',
			data: {
				action: 'donatico_dismiss_upsell',
				nonce: window.donaticoUpsell.nonce,
				key: key
			}
		} );

		// WordPress's built-in dismiss handler hides the notice from view.
		// We don't need to do anything more on the client side — the AJAX
		// call persists the state for future page loads.
	} );

} )( jQuery );
