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

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

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

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

			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(TextControl, {
							label: __('Minimum Length', 'giraforms'),
							type: 'number',
							value: minLength,
							onChange: (value) => setAttributes({ minLength: parseInt(value) || 0 }),
							min: 0,
						}),
						el(TextControl, {
							label: __('Maximum Length', 'giraforms'),
							type: 'number',
							value: maxLength,
							onChange: (value) => setAttributes({ maxLength: parseInt(value) || 0 }),
							min: 0,
						})
					),
					el(
						PanelBody,
						{ title: __('Advanced', 'giraforms'), initialOpen: false },
						el(ToggleControl, {
							label: __('Show Password Toggle', 'giraforms'),
							checked: showPasswordToggle,
							onChange: (value) => setAttributes({ showPasswordToggle: value }),
							help: __('Allow users to toggle password visibility', 'giraforms'),
						}),
						el(SelectControl, {
							label: __('Autocomplete', 'giraforms'),
							value: autocomplete,
							options: [
								{ label: __('Current Password', 'giraforms'), value: 'current-password' },
								{ label: __('New Password', 'giraforms'), value: 'new-password' },
								{ label: __('Off', 'giraforms'), value: 'off' }
							],
							onChange: (value) => setAttributes({ autocomplete: value }),
						})
					),
					// Conditional Logic Panel
					
				),
				// Block Content
				el(
					'div',
					blockProps,
					el(
						'label',
						{ className: required ? 'required' : '' },
						label
					),
					el(
						'div',
						{ className: 'giraforms-password-wrapper' },
						el('input', {
							type: 'password',
							placeholder: placeholder,
							disabled: true,
						}),
						showPasswordToggle && el(
							'button',
							{ type: 'button', className: 'giraforms-password-toggle', disabled: true },
							el('span', { className: 'dashicons dashicons-visibility' })
						)
					),
					helpText && el('p', { className: 'giraforms-help-text' }, helpText)
				)
			);
		},

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

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

			return el(
				'div',
				blockProps,
				el(
					'label',
					{ htmlFor: fieldId, className: required ? 'required' : '' },
					label
				),
				el(
					'div',
					{ className: 'giraforms-password-wrapper' },
					el('input', {
						type: 'password',
						id: fieldId,
						name: fieldId,
						placeholder: placeholder,
						required: required,
						'data-required-message': requiredMessage || undefined,
						minLength: minLength > 0 ? minLength : undefined,
						maxLength: maxLength > 0 ? maxLength : undefined,
						autoComplete: autocomplete,
						className: 'giraforms-input',
					}),
					showPasswordToggle && el(
						'button',
						{ type: 'button', className: 'giraforms-password-toggle', 'aria-label': __('Toggle password visibility', 'giraforms') },
						el('span', { className: 'dashicons dashicons-visibility' })
					)
				),
				helpText && el('div', { className: 'giraforms-field-description' }, helpText)
			);
		},
	});
})(
	window.wp.blocks,
	window.wp.element,
	window.wp.blockEditor,
	window.wp.components,
	window.wp.i18n
);
