/**
 * Admin Notice Handler
 * @param $
 */
( function ( $ ) {
	'use strict';

	$( document ).ready( function () {
		// Ensure notice data exists and has required properties
		if (
			typeof lifestyle_blog_arcane_notice === 'undefined' ||
			! lifestyle_blog_arcane_notice.notice_class ||
			! lifestyle_blog_arcane_notice.ajax_url ||
			! lifestyle_blog_arcane_notice.action ||
			! lifestyle_blog_arcane_notice.nonce
		) {
			return;
		}

		const noticeClass = lifestyle_blog_arcane_notice.notice_class;
		const $notice = $( `.${ noticeClass }` );

		if ( $notice.length === 0 ) {
			return;
		}

		/**
		 * Handle notice dismiss click
		 *
		 * @param {Event} e
		 */
		function handleNoticeDismiss( e ) {
			e.preventDefault();

			const $thisNotice = $( this ).closest( `.${ noticeClass }` );

			// Prevent duplicate dismiss requests
			if ( $thisNotice.data( 'dismissing' ) ) {
				return;
			}

			$thisNotice.data( 'dismissing', true );

			$.ajax( {
				url: lifestyle_blog_arcane_notice.ajax_url,
				method: 'POST',
				data: {
					action: lifestyle_blog_arcane_notice.action,
					nonce: lifestyle_blog_arcane_notice.nonce,
				},
				success( response ) {
					if ( response && response.success ) {
						$thisNotice.fadeOut( 200, function () {
							$( this ).remove();
						} );
					} else {
						console.error(
							'Failed to dismiss notice:',
							response ? response.data : 'No response data'
						);
						// Optional: still hide on failure to avoid user frustration
						$thisNotice.fadeOut( 200, function () {
							$( this ).remove();
						} );
					}
				},
				error( xhr, status, error ) {
					console.error( 'AJAX error:', error );
					// Optional: still hide on AJAX error
					$thisNotice.fadeOut( 200, function () {
						$( this ).remove();
					} );
				},
				complete() {
					// Allow potential future interactions if needed
					$thisNotice.data( 'dismissing', false );
				},
			} );
		}

		// Delegated click handler for dismiss button
		$( document ).on(
			'click',
			`.${ noticeClass } .notice-dismiss`,
			handleNoticeDismiss
		);
	} );
} )( jQuery );
