/**
 * WordPress dependencies
 */
import { addFilter } from '@safe-wordpress/hooks';
import { registerPlugin } from '@safe-wordpress/plugins';

/**
 * Internal dependencies
 */
import { EditorExtensions, PopupOpenControl } from './components';
import type { Settings } from './types';

// Init
if ( hasSettings( window ) ) {
	init( window.NelioPopupsSettings );
}

// =======
// HELPERS
// =======

function hasSettings( w: unknown ): w is { NelioPopupsSettings: Settings } {
	return !! ( w as Record< string, unknown > ).NelioPopupsSettings;
}

function init( settings: Settings ) {
	registerPlugin( 'nelio-popups-external', {
		icon: () => null,
		render: () => <EditorExtensions settings={ settings } />,
	} );

	addFilter(
		'editor.BlockEdit',
		'nelio-popups/trigger-open-control',
		PopupOpenControl( settings.popupOpenBlocks )
	);
}
