<?php
/**
 * The block-based widgets editor, for use in widgets.php.
 *
 * @package GeChiUI
 * @subpackage Administration
 */

// Don't load directly.
if ( ! defined( 'ABSPATH' ) ) {
	die( '-1' );
}

// Flag that we're loading the block editor.
$current_screen = get_current_screen();
$current_screen->is_block_editor( true );

$block_editor_context = new GC_Block_Editor_Context( array( 'name' => 'core/edit-widgets' ) );

$preload_paths = array(
	array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),
	'/gc/v2/widget-types?context=edit&per_page=-1',
	'/gc/v2/sidebars?context=edit&per_page=-1',
	'/gc/v2/widgets?context=edit&per_page=-1&_embed=about',
);
block_editor_rest_api_preload( $preload_paths, $block_editor_context );

$editor_settings = get_block_editor_settings(
	array_merge( get_legacy_widget_block_editor_settings(), array( 'styles' => get_block_editor_theme_styles() ) ),
	$block_editor_context
);

// The widgets editor does not support the Block Directory, so don't load any of
// its assets. This also prevents 'gc-editor' from being enqueued which we
// cannot load in the widgets screen because many widget scripts rely on `gc.editor`.
remove_action( 'enqueue_block_editor_assets', 'gc_enqueue_editor_block_directory_assets' );

gc_add_inline_script(
	'gc-edit-widgets',
	sprintf(
		'gc.domReady( function() {
			gc.editWidgets.initialize( "widgets-editor", %s );
		} );',
		gc_json_encode( $editor_settings )
	)
);

// Preload server-registered block schemas.
gc_add_inline_script(
	'gc-blocks',
	'gc.blocks.unstable__bootstrapServerSideBlockDefinitions(' . gc_json_encode( get_block_editor_server_block_settings() ) . ');'
);

gc_add_inline_script(
	'gc-blocks',
	sprintf( 'gc.blocks.setCategories( %s );', gc_json_encode( get_block_categories( $block_editor_context ) ) ),
	'after'
);

gc_enqueue_script( 'gc-edit-widgets' );
gc_enqueue_script( 'admin-widgets' );
gc_enqueue_style( 'gc-edit-widgets' );

/** This action is documented in gc-admin/edit-form-blocks.php */
do_action( 'enqueue_block_editor_assets' );

/** This action is documented in gc-admin/widgets-form.php */
do_action( 'sidebar_admin_setup' );

require_once ABSPATH . 'gc-admin/admin-header.php';

/** This action is documented in gc-admin/widgets-form.php */
do_action( 'widgets_admin_page' );
?>

<div id="widgets-editor" class="blocks-widgets-container"></div>

<?php
/** This action is documented in gc-admin/widgets-form.php */
do_action( 'sidebar_admin_page' );

require_once ABSPATH . 'gc-admin/admin-footer.php';
