/**
 * Time 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/time-field', {
		icon: {
			src: 'clock',
			foreground: '#3858e9'
		},
		edit: function (props) {
			const { attributes, setAttributes } = props;
			const { fieldId, label, required, requiredMessage, helpText, step, minTime, maxTime } = attributes;

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

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

			return el(
				Fragment,
				null,
				el(
					InspectorControls,
					null,
					el(
						PanelBody,
						{ title: __('Field Settings', 'giraforms'), initialOpen: true },
						el(TextControl, {
							label: __('Label', 'giraforms'),
							value: label,
							onChange: (value) => setAttributes({ label: 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(RangeControl, {
							label: __('Interval (seconds)', 'giraforms'),
							value: step,
							onChange: (value) => setAttributes({ step: value }),
							min: 60,
							max: 3600,
							step: 60,
							help: __('60 = 1 minute, 300 = 5 minutes, 900 = 15 minutes', 'giraforms'),
						}),
						el(TextControl, {
							label: __('Min time', 'giraforms'),
							type: 'time',
							value: minTime,
							onChange: (value) => setAttributes({ minTime: value }),
						}),
						el(TextControl, {
							label: __('Max time', 'giraforms'),
							type: 'time',
							value: maxTime,
							onChange: (value) => setAttributes({ maxTime: value }),
						})
					)
				),
				el(
					'div',
					blockProps,
					el(
						'label',
						{ htmlFor: fieldId, className: required ? 'required' : '' },
						label
					),
					el('input', {
						type: 'time',
						id: fieldId,
						disabled: true,
					}),
					helpText && el('div', { className: 'giraforms-field-description' }, helpText)
				)
			);
		},

		save: function (props) {
			const { attributes } = props;
			const { fieldId, label, required, requiredMessage, helpText, step, minTime, maxTime } = attributes;

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

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