/**
 * SwapAds Banner Block — Editor Sidebar + Live Preview
 *
 * F122 (2026-07-30): inspector controls + live preview for the
 * `swapads/banner` Gutenberg block.
 *
 * No build step. Loaded via wp_register_script() in BannerBlock::register().
 * Uses wp.element + wp.blockEditor + wp.components (already provided by WP).
 *
 * @since 1.5.2
 */
( function ( wp ) {
    'use strict';

    if ( ! wp || ! wp.element || ! wp.blocks || ! wp.blockEditor || ! wp.components ) {
        // WP < 5.0 or missing dependency — bail silently. The block
        // still renders server-side; we just don't show controls.
        return;
    }

    var el = wp.element.createElement;
    var Fragment = wp.element.Fragment;
    var __ = wp.i18n ? wp.i18n.__ : function ( s ) { return s; };

    var PanelBody = wp.components.PanelBody;
    var SelectControl = wp.components.SelectControl;
    var Placeholder = wp.components ? wp.components.Placeholder : null;

    var InspectorControls = wp.blockEditor.InspectorControls;
    var useBlockProps = wp.blockEditor.useBlockProps;

    // F122: placement options for the editor sidebar. The server doesn't
    // use this for matching (verified during F201 cleanup) — it's just
    // an informational marker in the impression record. Default 'auto'.
    var PLACEMENT_OPTIONS = [
        { label: 'Auto (recommended)',  value: 'auto' },
        { label: 'Sidebar',             value: 'sidebar' },
        { label: 'In-content',          value: 'in-content' },
        { label: 'Footer',              value: 'footer' },
        { label: 'Header',              value: 'header' },
    ];

    var ALIGN_OPTIONS = [
        { label: 'Default (none)', value: '' },
        { label: 'Wide',          value: 'wide' },
        { label: 'Full',          value: 'full' },
        { label: 'Left',          value: 'left' },
        { label: 'Right',         value: 'right' },
        { label: 'Center',        value: 'center' },
    ];

    // F122: live preview — server-renders via the same shortcode output.
    // We can't call wpApiFetch here without admin URL etc., so we show a
    // faithful placeholder that matches what the operator will see.
    function PreviewBox( props ) {
        var placement = props.placement || 'auto';
        var align     = props.align || '';
        var alignClass = align !== '' ? ' align' + align : '';
        return el(
            'div',
            { className: 'swapads-block-preview' + alignClass },
            el( 'div', { className: 'swapads-block-preview__label' },
                __( 'SwapAds Banner Preview' ) + ' (' + placement + ')'
            ),
            el( 'div', { className: 'swapads-block-preview__bar' },
                el( 'span', null, 'Your banner will render here.' )
            )
        );
    }

    var Edit = function ( props ) {
        var attributes = props.attributes || {};
        var setAttributes = props.setAttributes;

        // F122: bump to api_version 2
        var blockProps = useBlockProps ? useBlockProps( { className: 'swapads-block' } ) : { className: 'swapads-block' };

        return el(
            Fragment,
            null,
            el(
                InspectorControls,
                null,
                el(
                    PanelBody,
                    { title: __( 'SwapAds Banner' ), initialOpen: true },
                    el( SelectControl, {
                        label: __( 'Placement (informational)' ),
                        value: attributes.placement || 'auto',
                        options: PLACEMENT_OPTIONS,
                        onChange: function ( value ) {
                            setAttributes( { placement: value } );
                        },
                        help: __( 'Where on the page the banner sits. Used for analytics; does not affect matching.' )
                    } ),
                    el( SelectControl, {
                        label: __( 'Alignment' ),
                        value: attributes.align || '',
                        options: ALIGN_OPTIONS,
                        onChange: function ( value ) {
                            setAttributes( { align: value } );
                        }
                    } )
                )
            ),
            el(
                'div',
                blockProps,
                el( PreviewBox, {
                    placement: attributes.placement || 'auto',
                    align: attributes.align || ''
                } )
            )
        );
    };

    // Register the block (overrides the BannerBlock::register PHP version
    // since wp.blocks.registerBlockType is the canonical client API).
    // The PHP side keeps the server render_callback so saved posts still
    // produce correct HTML even if the JS bundle is missing.
    if ( wp.blocks && typeof wp.blocks.registerBlockType === 'function' ) {
        wp.blocks.registerBlockType( 'swapads/banner', {
            edit: Edit,
            // 'save' returns null so WP uses server-side render via PHP
            // (api_version: 2 + render_callback in PHP).
            save: function () { return null; }
        } );
    }
}( window.wp ) );
