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

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

/**
 * Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
 */
import './editor.scss';

/**
 * The 'Edit' function for Login block.
 *
 * @param {Object} props The element props.
 * @since 2.0.0
 * @return {Element} Element to render.
 */
const Edit = ( props ) => {
	// Destructure the required props.
	const {
		clientId,
		isSelected,
		attributes: {
			isPreview,
			block_id,
			formId
		},
		setAttributes
	} = props;

	// Generate unique block ID if not set
	useEffect( () => {
		if ( ! block_id ) {
			const uniqueId = `login-${clientId}`;
			setAttributes( { block_id: uniqueId } );
		}
		if ( ! formId ) {
			setAttributes( { formId: `spectra-pro-login-${clientId}` } );
		}
	}, [ block_id, formId, clientId ] );

	// If this is an example, return the preview image.
	if ( isPreview ) {
		return <RenderBlockPreview blockName="login" pluginName="spectra-pro" />;
	}

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

export default Edit;
