/**
 * External dependencies.
 */
import { useEffect } from '@wordpress/element';

/**
 * Internal dependencies.
 */
// import './editor.scss';
import RenderBlockPreview from '@spectra-components/render-block-preview';
import Render from './render';

/**
 * The edit function describes the structure of your block in the context of the
 * editor. This represents what the editor will render when the block is used.
 *
 * @param {Object} props The element props.
 * @since 2.0.0
 * @return {Element} Element to render.
 */
const Edit = ( props ) => {
	// Destructure the required props.
	const {
		attributes: {
			isPreview,
			isRequired,
		},
		setAttributes,
		context
	} = props;

	// Get field information from context
	const { 
		'spectra-pro/form-input/isRequired': contextRequired 
	} = context;

	// Set requirements from context (but not label text - let it stay empty to show placeholder)
	useEffect( () => {
		if ( contextRequired !== undefined && contextRequired !== isRequired ) {
			setAttributes( { isRequired: contextRequired } );
		}
	}, [ contextRequired, isRequired, setAttributes ] );

	// If this is an example, return the preview image.
	if ( isPreview ) {
		return <RenderBlockPreview blockName="form-input-label"/>;
	}

	return ( <Render { ...{ ...props } } /> );
};

export default Edit;