/**
 * Dismiss handling for the FlexiBlog admin promotional banner.
 *
 * No external dependencies, no remote requests other than admin-ajax.php.
 */
( function () {
	'use strict';

	var settings = window.flexiBlogPromoBanner || {};

	function dismiss( banner ) {
		banner.classList.add( 'is-dismissing' );

		window.setTimeout( function () {
			if ( banner.parentNode ) {
				banner.parentNode.removeChild( banner );
			}
		}, 200 );

		if ( ! settings.ajaxUrl || ! settings.action || ! settings.nonce || ! window.fetch ) {
			return;
		}

		var body = new window.FormData();
		body.append( 'action', settings.action );
		body.append( 'nonce', settings.nonce );

		window.fetch( settings.ajaxUrl, {
			method: 'POST',
			credentials: 'same-origin',
			body: body
		} );
	}

	function init() {
		var banner = document.getElementById( 'flexiblog-promo-banner' );

		if ( ! banner ) {
			return;
		}

		banner.addEventListener( 'click', function ( event ) {
			var target = event.target;

			if ( ! target || ! target.closest ) {
				return;
			}

			if ( ! target.closest( '[data-flexiblog-promo-dismiss]' ) ) {
				return;
			}

			event.preventDefault();
			dismiss( banner );
		} );
	}

	if ( 'loading' === document.readyState ) {
		document.addEventListener( 'DOMContentLoaded', init );
	} else {
		init();
	}
}() );
