/**
 * GiraForms - Editor JavaScript
 *
 * This file contains shared utilities for block editor.
 * Individual blocks will have their own index.js files.
 */

(function () {
	'use strict';

	// Shared utilities for blocks
	window.giraFormsEditor = {
		

		/**
		 * Generate unique field ID
		 */
		generateFieldId: function (prefix) {
			return prefix + '_' + Math.random().toString(36).substr(2, 9);
		},

		/**
		 * Common field attributes
		 */
		commonAttributes: {
			fieldId: {
				type: 'string',
				default: '',
			},
			label: {
				type: 'string',
				default: '',
			},
			placeholder: {
				type: 'string',
				default: '',
			},
			required: {
				type: 'boolean',
				default: false,
			},
			helpText: {
				type: 'string',
				default: '',
			},
			className: {
				type: 'string',
				default: '',
			},
		},

		/**
		 * Common inspector controls
		 */
		getCommonInspectorControls: function (props) {
			const { attributes, setAttributes } = props;
			const { InspectorControls } = wp.blockEditor;
			const { PanelBody, TextControl, ToggleControl } = wp.components;

			return wp.element.createElement(
				InspectorControls,
				null,
				wp.element.createElement(
					PanelBody,
					{ title: 'Field Settings', initialOpen: true },
					wp.element.createElement(TextControl, {
						label: 'Label',
						value: attributes.label,
						onChange: (value) => setAttributes({ label: value }),
					}),
					wp.element.createElement(TextControl, {
						label: 'Placeholder',
						value: attributes.placeholder,
						onChange: (value) => setAttributes({ placeholder: value }),
					}),
					wp.element.createElement(ToggleControl, {
						label: 'Required field',
						checked: attributes.required,
						onChange: (value) => setAttributes({ required: value }),
					}),
					wp.element.createElement(TextControl, {
						label: 'Help text',
						value: attributes.helpText,
						onChange: (value) => setAttributes({ helpText: value }),
					})
				)
			);
		},
	};

	// Auto-insert form container on new page if requested
	if (typeof giraFormsData !== 'undefined' && giraFormsData.autoInsertForm) {
		// Subscribe to editor store ready event
		wp.data.subscribe(function () {
			const editor = wp.data.select('core/block-editor');
			const dispatch = wp.data.dispatch('core/block-editor');

			if (!editor || !dispatch) {
				return;
			}

			// Check if editor is ready by checking if getBlocks is available
			const blocks = editor.getBlocks();

			if (blocks === undefined) {
				return;
			}

			// Only insert if the page is empty and we haven't inserted yet
			if (blocks.length === 0 && !window.giraFormsInserted) {
				window.giraFormsInserted = true;

				// Wait a bit for editor to fully initialize
				setTimeout(function() {
					// Create the form container block
					const formBlock = wp.blocks.createBlock('giraforms/form-container', {
						formName: 'Contact Form',
					});

					// Insert at the beginning
					dispatch.insertBlocks(formBlock);
				}, 500);
			}
		});
	}
})();
