/**
 * File Path: assets/js/blocks/block-arc-signup.js
 *
 * Block editor component: mbs/arc-signup
 *
 * Self-contained IIFE. Depends on mbs-blocks-editor (window.mbsBlocksCore).
 * No build step required.
 *
 * Sprint F-027: ARC Signup Block.
 */
( function ( wp, mbsCore ) {
	'use strict';

	var registerBlockType = wp.blocks.registerBlockType;
	var el                = wp.element.createElement;
	var __                = wp.i18n.__;
	var Fragment          = wp.element.Fragment;
	var InspectorControls = wp.blockEditor.InspectorControls;
	var useBlockProps     = wp.blockEditor.useBlockProps;
	var PanelBody         = wp.components.PanelBody;
	var TextControl       = wp.components.TextControl;
	var ToggleControl     = wp.components.ToggleControl;
	var ServerSideRender  = wp.serverSideRender;
	var useSelect         = wp.data.useSelect;

	registerBlockType( 'mbs/arc-signup', {
		apiVersion: 3,
		title:      __( 'ARC Signup (PRO)', 'my-book-showroom' ),
		category:   'mbs',
		keywords:   [ 'arc', 'review', 'advance copy', 'reviewer', 'signup', 'form' ],
		icon:       'email-alt',
		description: __( 'ARC reviewer self-signup form for a book page (PRO).', 'my-book-showroom' ),
		supports: { html: false },

		attributes: {
			bookId:           { type: 'number',  default: 0     },
			title:            { type: 'string',  default: 'Apply to Review This Book' },
			buttonLabel:      { type: 'string',  default: 'Submit Application' },
			successMessage:   { type: 'string',  default: "Thank you! We'll be in touch." },
			showMessageField: { type: 'boolean', default: true  },
			consentText:      { type: 'string',  default: ''    },
			requireConsent:   { type: 'boolean', default: true  },
		},

		edit: function ( props ) {
			var attrs       = props.attributes;
			var setAttr     = props.setAttributes;
			var postType    = useSelect( function ( select ) {
				return select( 'core/editor' ).getCurrentPostType();
			} );
			var mbsPostType = ( mbsCore && mbsCore.postType ) ? mbsCore.postType : 'mbs_book';

			var notOnBookPage = postType && postType !== mbsPostType;

			return el( Fragment, null,

				el( InspectorControls, null,
					el( PanelBody, { title: __( 'ARC Signup Settings', 'my-book-showroom' ), initialOpen: true },

						el( TextControl, {
							label:    __( 'Heading', 'my-book-showroom' ),
							value:    attrs.title,
							onChange: function ( v ) { setAttr( { title: v } ); },
						} ),

						el( TextControl, {
							label:    __( 'Button Label', 'my-book-showroom' ),
							value:    attrs.buttonLabel,
							onChange: function ( v ) { setAttr( { buttonLabel: v } ); },
						} ),

						el( TextControl, {
							label:    __( 'Success Message', 'my-book-showroom' ),
							value:    attrs.successMessage,
							onChange: function ( v ) { setAttr( { successMessage: v } ); },
						} ),

						el( ToggleControl, {
							label:    __( 'Show message / pitch field', 'my-book-showroom' ),
							checked:  attrs.showMessageField,
							onChange: function ( v ) { setAttr( { showMessageField: v } ); },
						} ),

						el( ToggleControl, {
							label:    __( 'Require consent checkbox', 'my-book-showroom' ),
							checked:  attrs.requireConsent,
							onChange: function ( v ) { setAttr( { requireConsent: v } ); },
						} ),

						el( TextControl, {
							label:    __( 'Consent text', 'my-book-showroom' ),
							value:    attrs.consentText,
							onChange: function ( v ) { setAttr( { consentText: v } ); },
						} )
					)
				),

				el( 'div', useBlockProps(),
					notOnBookPage
						? el( 'div', { className: 'mbs-block-notice', style: { padding: '12px', background: '#fff3cd', border: '1px solid #ffc107', borderRadius: '4px' } },
							__( 'This block is designed for book pages. Please place it on a book\'s single page.', 'my-book-showroom' )
						  )
						: el( ServerSideRender, {
							block:      'mbs/arc-signup',
							attributes: attrs,
						} )
				)
			);
		},

		save: function () {
			return null; // Server-side render.
		},
	} );

} )( window.wp, window.mbsBlocksCore );
