/**
 * External dependencies.
 */
import { addFilter } from '@wordpress/hooks';
import domReady from '@wordpress/dom-ready';

/**
 * Internal dependencies.
 */
import './index.scss';
import { addGlobalStylesMenuItem, RenderGlobalStylesRoute } from './helpers';

// Add filters to extend the menu items, and the admin route in the Spectra dashboard.
addFilter( 'spectra.adminMenuItems', 'spectra/ext-global-styles', addGlobalStylesMenuItem );
addFilter( 'spectra.adminRoute', 'spectra/ext-global-styles', RenderGlobalStylesRoute );

// Ensure block types are available when DOM is ready
domReady( () => {
	// Initialize global temporary storage for cross-navigation persistence
	if ( ! window.spectraGSTemporaryStorage ) {
		window.spectraGSTemporaryStorage = {};
	}

	// Add beforeunload event listener to warn about unsaved changes
	window.addEventListener( 'beforeunload', ( event ) => {
		if ( window.spectraGSTemporaryStorage && Object.keys( window.spectraGSTemporaryStorage ).length > 0 ) {
			event.preventDefault();
			event.returnValue = 'You have unsaved changes. Are you sure you want to leave?';
			return 'You have unsaved changes. Are you sure you want to leave?';
		}
	} );
} );
