/**
 * Text Field Block
 */

(function (blocks, element, blockEditor, components, i18n) {
	const { registerBlockType } = blocks;
	const { createElement: el, Fragment, useEffect } = element;
	const { InspectorControls, useBlockProps } = blockEditor;
	const { PanelBody, TextControl, ToggleControl, RangeControl } = components;
	const { __ } = i18n;

	registerBlockType('giraforms/text-field', {
		icon: {
			src: 'text',
			foreground: '#3858e9'
		},
		edit: function (props) {
			const { attributes, setAttributes, clientId } = props;
			const {
				fieldId,
				label,
				placeholder,
				required,
				requiredMessage,
				helpText,
				minLength,
				maxLength,
			} = attributes;

			// Generate field ID if not set
			useEffect(() => {
				if (!fieldId) {
					setAttributes({
						fieldId: 'text_' + Math.random().toString(36).substring(2, 11),
					});
				}
			}, []);

			const blockProps = useBlockProps({
				className: 'giraforms-field giraforms-field-text',
			});

			return el(
				Fragment,
				null,
				// Inspector Controls
				el(
					InspectorControls,
					null,
					el(
						PanelBody,
						{ title: __('Field Settings', 'giraforms'), initialOpen: true },
						el(TextControl, {
							label: __('Label', 'giraforms'),
							value: label,
							onChange: (value) => setAttributes({ label: value }),
						}),
						el(TextControl, {
							label: __('Placeholder', 'giraforms'),
							value: placeholder,
							onChange: (value) => setAttributes({ placeholder: value }),
						}),
						el(ToggleControl, {
							label: __('Required field', 'giraforms'),
							checked: required,
							onChange: (value) => setAttributes({ required: value }),
						}),
						required && el(TextControl, {
							label: __('Required message', 'giraforms'),
							value: requiredMessage,
							onChange: (val) => setAttributes({ requiredMessage: val }),
							placeholder: __('This field is required.', 'giraforms'),
							help: __('Overrides the default required error message for this field.', 'giraforms'),
						}),
						el(TextControl, {
							label: __('Help text', 'giraforms'),
							value: helpText,
							onChange: (value) => setAttributes({ helpText: value }),
						})
					),
					el(
						PanelBody,
						{ title: __('Validation', 'giraforms'), initialOpen: false },
						el(RangeControl, {
							label: __('Minimum length', 'giraforms'),
							value: minLength,
							onChange: (value) => setAttributes({ minLength: value }),
							min: 0,
							max: 1000,
						}),
						el(RangeControl, {
							label: __('Maximum length', 'giraforms'),
							value: maxLength,
							onChange: (value) => setAttributes({ maxLength: value }),
							min: 0,
							max: 1000,
						})
					),
					// Conditional Logic Panel
					
				),
				// Block Content
				el(
					'div',
					blockProps,
					el(
						'label',
						{ htmlFor: fieldId, className: required ? 'required' : '' },
						label
					),
					el('input', {
						type: 'text',
						id: fieldId,
						placeholder: placeholder,
						required: required,
						disabled: true,
					}),
					helpText &&
						el('div', { className: 'giraforms-field-description' }, helpText)
				)
			);
		},

		save: function (props) {
			const { attributes } = props;
			const {
				fieldId,
				label,
				placeholder,
				required,
				requiredMessage,
				helpText,
				minLength,
				maxLength,
			} = attributes;

			const blockProps = useBlockProps.save({
				className: 'giraforms-field giraforms-field-text',
				'data-field-id': fieldId,
			});

			return el(
				'div',
				blockProps,
				el(
					'label',
					{ htmlFor: fieldId, className: required ? 'required' : '' },
					label
				),
				el('input', {
					type: 'text',
					id: fieldId,
					name: fieldId,
					placeholder: placeholder,
					required: required,
					'data-required-message': requiredMessage || undefined,
					minLength: minLength > 0 ? minLength : null,
					maxLength: maxLength > 0 ? maxLength : null,
				}),
				helpText &&
					el('div', { className: 'giraforms-field-description' }, helpText)
			);
		},
	});
})(
	window.wp.blocks,
	window.wp.element,
	window.wp.blockEditor,
	window.wp.components,
	window.wp.i18n
);
