/**
 * Live search block — inspector + server-side preview.
 */
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import ServerSideRender from '@wordpress/server-side-render';
import { Spinner } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

import LiveSearchInspector from './live-search-inspector';
import './editor.scss';

export default function Edit({ attributes, setAttributes }) {
	const props = useBlockProps({
		className: 'movie-engine-live-search-block-editor',
	});

	return (
		<>
			<InspectorControls>
				<LiveSearchInspector attributes={attributes} setAttributes={setAttributes} />
			</InspectorControls>
			<div {...props}>
				<div className="movie-engine-live-search-block-editor__preview movie-engine-plugin">
					<ServerSideRender
						block="cine-movie-engine/live-search"
						attributes={attributes}
						LoadingResponsePlaceholder={() => (
							<p className="movie-engine-live-search-block-editor__preview-loading">
								<Spinner />
								{__('Loading preview…', 'cine-movie-engine')}
							</p>
						)}
						ErrorResponsePlaceholder={() => (
							<p className="movie-engine-live-search-block-editor__preview-loading">
								{__(
									'Preview unavailable. Check the front end or try saving the page.',
									'cine-movie-engine'
								)}
							</p>
						)}
					/>
				</div>
			</div>
		</>
	);
}
