/**
 * External dependencies.
 */
import { memo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import {
	InspectorControls,
} from '@wordpress/block-editor';
import {
	__experimentalToolsPanel as ToolsPanel,
	__experimentalToolsPanelItem as ToolsPanelItem,
	TextControl,
	ToggleControl,
} from '@wordpress/components';

/**
 * Element Sub-settings: General settings.
 *
 * @param {Object} props The element props.
 * @since 2.0.0
 * @return {Element} The rendered block settings.
 */
const BlockSettings = memo( ( props ) => {

	// Destructure the required props.
	const { clientId, setAttributes, attributes } = props;

	const {
		fieldPlaceholder,
		isRequired,
	} = attributes;

	return (
		<>
			<InspectorControls group='settings'>
				<ToolsPanel
					label={ __( 'Register Username Field', 'spectra-pro' ) }
					resetAll={ () => {
						setAttributes( {
							fieldPlaceholder: '',
							isRequired: false,
						} );
					} }
					panelId={ clientId }
				>
					<ToolsPanelItem
						hasValue={ () => !! fieldPlaceholder }
						label={ __( 'Field Placeholder', 'spectra-pro' ) }
						panelId={ clientId }
						onDeselect={ () => setAttributes( { fieldPlaceholder: '' } ) }
						isShownByDefault
					>
						<TextControl
							__next40pxDefaultSize
							__nextHasNoMarginBottom
							label={ __( 'Field Placeholder', 'spectra-pro' ) }
							value={ fieldPlaceholder }
							onChange={ ( value ) => setAttributes( { fieldPlaceholder: value } ) }
							placeholder={ __( 'Enter your username or email', 'spectra-pro' ) }
							help={ __( 'Custom placeholder text for this field', 'spectra-pro' ) }
						/>
					</ToolsPanelItem>
					<ToolsPanelItem
						hasValue={ () => isRequired !== false }
						label={ __( 'Required', 'spectra-pro' ) }
						panelId={ clientId }
						onDeselect={ () => setAttributes( { isRequired: false } ) }
						isShownByDefault
					>
						<ToggleControl
							__nextHasNoMarginBottom
							checked={ isRequired }
							label={ __( 'Required Field', 'spectra-pro' ) }
							onChange={ () => setAttributes( { isRequired: ! isRequired } ) }
							help={ __( 'Mark this field as required', 'spectra-pro' ) }
						/>
					</ToolsPanelItem>
				</ToolsPanel>
			</InspectorControls>
		</>
	);
} );

/**
 * The Editor settings for this block.
 *
 * @param {Object} props The element props.
 * @since 2.0.0
 * @return {Element} The rendered settings.
 */
const Settings = ( props ) => {
	return (
		<BlockSettings { ...{ ...props } } />
	);
};

export default memo( Settings );