/**
 * Number 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, __experimentalNumberControl } = components;
	const { __ } = i18n;

	// Fallback for NumberControl if not available
	const NumberControl = __experimentalNumberControl || TextControl;

	registerBlockType('giraforms/number-field', {
		icon: {
			src: 'editor-ol',
			foreground: '#3858e9'
		},
		edit: function (props) {
			const { attributes, setAttributes, clientId } = props;
			const {
				fieldId,
				label,
				placeholder,
				required,
				requiredMessage,
				helpText,
				min,
				max,
				step,
				defaultValue,
			} = attributes;

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

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

			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: __('Number Settings', 'giraforms'), initialOpen: true },
						el(NumberControl, {
							label: __('Minimum Value', 'giraforms'),
							value: min !== null ? min : '',
							onChange: (value) => setAttributes({ min: value !== '' ? parseFloat(value) : null }),
							help: __('Leave empty for no minimum', 'giraforms'),
						}),
						el(NumberControl, {
							label: __('Maximum Value', 'giraforms'),
							value: max !== null ? max : '',
							onChange: (value) => setAttributes({ max: value !== '' ? parseFloat(value) : null }),
							help: __('Leave empty for no maximum', 'giraforms'),
						}),
						el(NumberControl, {
							label: __('Step', 'giraforms'),
							value: step,
							onChange: (value) => setAttributes({ step: value !== '' ? parseFloat(value) : 1 }),
							help: __('Increment value (e.g., 1 for integers, 0.01 for decimals)', 'giraforms'),
							min: 0.01,
							step: 0.01,
						}),
						el(NumberControl, {
							label: __('Default Value', 'giraforms'),
							value: defaultValue !== null ? defaultValue : '',
							onChange: (value) => setAttributes({ defaultValue: value !== '' ? parseFloat(value) : null }),
							help: __('Leave empty for no default value', 'giraforms'),
						})
					),
					// Conditional Logic Panel
					
				),
				// Block Content
				el(
					'div',
					blockProps,
					el(
						'label',
						{ htmlFor: fieldId, className: required ? 'required' : '' },
						label
					),
					el('input', {
						type: 'number',
						id: fieldId,
						placeholder: placeholder,
						required: required,
						min: min !== null ? min : undefined,
						max: max !== null ? max : undefined,
						step: step,
						defaultValue: defaultValue !== null ? defaultValue : undefined,
						disabled: true,
					}),
					helpText &&
						el('div', { className: 'giraforms-field-description' }, helpText)
				)
			);
		},

		save: function (props) {
			const { attributes } = props;
			const {
				fieldId,
				label,
				placeholder,
				required,
				requiredMessage,
				helpText,
				min,
				max,
				step,
				defaultValue,
			} = attributes;

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

			return el(
				'div',
				blockProps,
				el(
					'label',
					{ htmlFor: fieldId, className: required ? 'required' : '' },
					label
				),
				el('input', {
					type: 'number',
					id: fieldId,
					name: fieldId,
					placeholder: placeholder,
					required: required,
					'data-required-message': requiredMessage || undefined,
					min: min !== null ? min : undefined,
					max: max !== null ? max : undefined,
					step: step,
					defaultValue: defaultValue !== null ? defaultValue : undefined,
				}),
				helpText &&
					el('div', { className: 'giraforms-field-description' }, helpText)
			);
		},
	});
})(
	window.wp.blocks,
	window.wp.element,
	window.wp.blockEditor,
	window.wp.components,
	window.wp.i18n
);
