/**
 * Sprint 11 PR 1 — Gutenberg editor JS for the Seekmodo Chat block.
 *
 * See `blocks/src/search/index.js` for the design rationale.
 */
( function ( wp ) {
	if ( ! wp || ! wp.blocks || ! wp.element || ! wp.blockEditor ) {
		return;
	}

	var el = wp.element.createElement;
	var Fragment = wp.element.Fragment;
	var registerBlockType = wp.blocks.registerBlockType;
	var useBlockProps = wp.blockEditor.useBlockProps;
	var InspectorControls = wp.blockEditor.InspectorControls;
	var Components = wp.components || {};
	var PanelBody = Components.PanelBody || function ( props ) { return el( 'div', null, props.children ); };
	var TextControl = Components.TextControl || function () { return null; };
	var TextareaControl = Components.TextareaControl || TextControl;
	var ToggleControl = Components.ToggleControl || function () { return null; };
	var __ = ( wp.i18n && wp.i18n.__ ) ? wp.i18n.__ : function ( s ) { return s; };

	registerBlockType( 'seekmodo/chat', {
		edit: function ( props ) {
			var attrs = props.attributes;
			var setAttr = function ( key ) {
				return function ( value ) {
					var update = {};
					update[ key ] = value;
					props.setAttributes( update );
				};
			};

			return el(
				Fragment,
				null,
				el(
					InspectorControls,
					null,
					el(
						PanelBody,
						{ title: __( 'Seekmodo Chat', 'seekmodo-wordpress' ), initialOpen: true },
						el( TextControl, {
							label: __( 'Placeholder', 'seekmodo-wordpress' ),
							value: attrs.placeholder || '',
							onChange: setAttr( 'placeholder' )
						} ),
						el( TextareaControl, {
							label: __( 'Intro message', 'seekmodo-wordpress' ),
							help: __( 'Optional first assistant turn shown before the shopper types.', 'seekmodo-wordpress' ),
							value: attrs.intro || '',
							onChange: setAttr( 'intro' )
						} ),
						el( TextControl, {
							type: 'number',
							label: __( 'Maximum turns', 'seekmodo-wordpress' ),
							value: attrs.maxTurns != null ? String( attrs.maxTurns ) : '12',
							onChange: function ( value ) {
								var n = parseInt( value, 10 );
								setAttr( 'maxTurns' )( isNaN( n ) ? 12 : n );
							}
						} ),
						el( ToggleControl, {
							label: __( 'Show citation chips', 'seekmodo-wordpress' ),
							checked: !! attrs.autoCitations,
							onChange: setAttr( 'autoCitations' )
						} )
					)
				),
				el(
					'div',
					useBlockProps( {
						style: {
							border: '1px dashed #c3c4c7',
							borderRadius: 6,
							padding: 12,
							background: '#fff'
						}
					} ),
					el( 'div', { style: { fontSize: 11, color: '#6c7781', marginBottom: 6 } }, __( 'Seekmodo \u00b7 Chat', 'seekmodo-wordpress' ) ),
					el( 'div', {
						style: {
							border: '1px solid #d4d4d8',
							borderRadius: 4,
							padding: 10,
							color: '#374151',
							background: '#fafafa',
							minHeight: 60
						}
					}, attrs.intro || __( 'Conversational AI assistant will appear here on the front-end.', 'seekmodo-wordpress' ) )
				)
			);
		},
		save: function () {
			return null;
		}
	} );
} )( window.wp );
