/**
 * Fundraiser Share Block — Editor Registration
 *
 * Registers the block in the Gutenberg editor with InspectorControls
 * to configure which platforms are shown and the display style.
 * Uses vanilla JS + wp.* globals (no build step required).
 */
( function ( wp ) {
	'use strict';

	var __              = wp.i18n.__;
	var el              = wp.element.createElement;
	var Fragment        = wp.element.Fragment;
	var useBlockProps   = wp.blockEditor.useBlockProps;
	var InspectorControls = wp.blockEditor.InspectorControls;
	var PanelBody       = wp.components.PanelBody;
	var ToggleControl   = wp.components.ToggleControl;
	var SelectControl   = wp.components.SelectControl;
	var TextControl     = wp.components.TextControl;

	wp.blocks.registerBlockType( 'ambassadorhub/fundraiser-share', {
		edit: function ( props ) {
			var attrs = props.attributes;
			var set   = props.setAttributes;

			return el(
				Fragment,
				null,

				// ── Sidebar settings ──────────────────────────────────────────
				el( InspectorControls, null,
					el( PanelBody, { title: __( 'Platforms', 'ambassadorhub' ), initialOpen: true },
						el( ToggleControl, {
							label:   __( 'Facebook', 'ambassadorhub' ),
							checked: attrs.showFacebook,
							onChange: function ( v ) { set( { showFacebook: v } ); },
						} ),
						el( ToggleControl, {
							label:   __( 'X (Twitter)', 'ambassadorhub' ),
							checked: attrs.showTwitter,
							onChange: function ( v ) { set( { showTwitter: v } ); },
						} ),
						el( ToggleControl, {
							label:   __( 'WhatsApp', 'ambassadorhub' ),
							checked: attrs.showWhatsapp,
							onChange: function ( v ) { set( { showWhatsapp: v } ); },
						} ),
						el( ToggleControl, {
							label:   __( 'LinkedIn', 'ambassadorhub' ),
							checked: attrs.showLinkedin,
							onChange: function ( v ) { set( { showLinkedin: v } ); },
						} ),
						el( ToggleControl, {
							label:   __( 'Email', 'ambassadorhub' ),
							checked: attrs.showEmail,
							onChange: function ( v ) { set( { showEmail: v } ); },
						} ),
						el( ToggleControl, {
							label:   __( 'Copy link', 'ambassadorhub' ),
							checked: attrs.showCopyLink,
							onChange: function ( v ) { set( { showCopyLink: v } ); },
						} ),
						el( ToggleControl, {
							label:   __( 'Threads', 'ambassadorhub' ),
							checked: attrs.showThreads,
							onChange: function ( v ) { set( { showThreads: v } ); },
						} ),
						el( ToggleControl, {
							label:   __( 'Mastodon', 'ambassadorhub' ),
							checked: attrs.showMastodon,
							onChange: function ( v ) { set( { showMastodon: v } ); },
						} ),
						attrs.showMastodon ? el( TextControl, {
							label:    __( 'Default Mastodon instance', 'ambassadorhub' ),
							value:    attrs.mastodonInstance,
							placeholder: 'mastodon.social',
							onChange: function ( v ) { set( { mastodonInstance: v } ); },
							help:     __( 'Pre-fill the instance field. Leave empty to always ask the visitor.', 'ambassadorhub' ),
						} ) : null,
						el( ToggleControl, {
							label:   __( 'Bluesky', 'ambassadorhub' ),
							checked: attrs.showBluesky,
							onChange: function ( v ) { set( { showBluesky: v } ); },
						} )
					),

					el( PanelBody, { title: __( 'Appearance', 'ambassadorhub' ), initialOpen: true },
						el( SelectControl, {
							label:   __( 'Display style', 'ambassadorhub' ),
							value:   attrs.displayStyle,
							options: [
								{ label: __( 'Buttons (icon + label)', 'ambassadorhub' ), value: 'buttons' },
								{ label: __( 'Icons only', 'ambassadorhub' ),             value: 'icons'   },
							],
							onChange: function ( v ) { set( { displayStyle: v } ); },
						} ),
						el( TextControl, {
							label:    __( 'Heading label', 'ambassadorhub' ),
							value:    attrs.label,
							onChange: function ( v ) { set( { label: v } ); },
							help:     __( 'Optional text shown above the share buttons.', 'ambassadorhub' ),
						} )
					),

					el( PanelBody, { title: __( 'Share message', 'ambassadorhub' ), initialOpen: false },
						el( TextControl, {
							label:    __( 'Custom share text', 'ambassadorhub' ),
							value:    attrs.shareText,
							onChange: function ( v ) { set( { shareText: v } ); },
							help:     __( 'Used as the tweet text and WhatsApp / email subject. Defaults to the fundraiser title when left empty.', 'ambassadorhub' ),
						} )
					)
				),

				// ── Editor canvas preview ─────────────────────────────────────
				el( 'div', useBlockProps(),
					el( 'div', { className: 'ambh-block-preview' },
						__( 'Fundraiser Share (renders on the frontend)', 'ambassadorhub' )
					)
				)
			);
		},

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

} )( window.wp );
