/**
 * Editor for aieo/competition — a competition picker (dropdown of published /
 * draft competitions) plus a live server-side-rendered preview. Leave the
 * picker on "On this competition page" (id 0) to auto-bind when the block is
 * placed on a competition's own page (the roosterx Competition pattern does
 * exactly this). Plain wp.* (no JSX/build); deps declared in edit.asset.php.
 */
( function ( wp ) {
	if ( !wp || !wp.blocks || !wp.element ) { return; }

	var el                = wp.element.createElement;
	var Fragment          = wp.element.Fragment;
	var registerBlockType = wp.blocks.registerBlockType;
	var getBlockType      = wp.blocks.getBlockType;
	var InspectorControls = wp.blockEditor && wp.blockEditor.InspectorControls;
	var useBlockProps     = wp.blockEditor && wp.blockEditor.useBlockProps;
	var PanelBody         = wp.components && wp.components.PanelBody;
	var SelectControl     = wp.components && wp.components.SelectControl;
	var Notice            = wp.components && wp.components.Notice;
	var ServerSideRender  = wp.serverSideRender || ( wp.editor && wp.editor.ServerSideRender );
	var useSelect         = wp.data && wp.data.useSelect;
	var __                = ( wp.i18n && wp.i18n.__ ) || function ( s ) { return s; };

	if ( getBlockType && getBlockType( 'aieo/competition' ) ) { return; } // already registered

	registerBlockType( 'aieo/competition', {
		edit: function ( props ) {
			var attrs = props.attributes || {};

			var comps = useSelect ? useSelect( function ( select ) {
				return select( 'core' ).getEntityRecords( 'postType', 'aieo_competition', {
					per_page: 100,
					orderby: 'title',
					order: 'asc',
					status: [ 'publish', 'draft', 'pending', 'future' ]
				} );
			}, [] ) : null;

			var options = [ { label: __( '— On this competition page —', 'ai-eshop-optimizer' ), value: 0 } ];
			if ( comps && comps.length ) {
				comps.forEach( function ( c ) {
					var t = ( c.title && c.title.rendered ) ? c.title.rendered : ( '#' + c.id );
					options.push( { label: t, value: c.id } );
				} );
			}

			var bp = useBlockProps ? useBlockProps() : {};

			var inspector = InspectorControls && PanelBody && SelectControl ? el(
				InspectorControls, {},
				el( PanelBody, { title: __( 'Competition', 'ai-eshop-optimizer' ), initialOpen: true },
					el( SelectControl, {
						label: __( 'Which competition?', 'ai-eshop-optimizer' ),
						help: __( 'Leave on “On this competition page” to auto-bind to the competition this block is placed on.', 'ai-eshop-optimizer' ),
						value: attrs.competitionId || 0,
						options: options,
						onChange: function ( v ) { props.setAttributes( { competitionId: parseInt( v, 10 ) || 0 } ); }
					} ),
					( comps && !comps.length ) ? el( Notice, { status: 'info', isDismissible: false },
						__( 'No competitions yet — create one under Customer Profiling → Competitions.', 'ai-eshop-optimizer' )
					) : null
				)
			) : null;

			var preview = ServerSideRender
				? el( ServerSideRender, { block: 'aieo/competition', attributes: attrs } )
				: el( 'div', { style: { padding: '16px', background: '#f6f7f7', color: '#646970', borderRadius: '8px' } },
					__( 'Competition — preview renders on the front end.', 'ai-eshop-optimizer' ) );

			return el( Fragment, {}, inspector, el( 'div', bp, preview ) );
		},
		save: function () { return null; } // server-rendered
	} );
} )( window.wp );
