/**
 * @file assets/js/blocks/block-coming-soon.js
 * Registers the mbs/coming-soon Gutenberg block.
 * Depends on mbs-blocks-editor (wp.* globals + window.mbsBlocksCore helpers).
 * Helpers used: none
 * @package MyBookShowroom
 * @since   1.0.0
 */

( function ( wp, mbsCore ) {
    'use strict';

    const { registerBlockType } = wp.blocks;
    const { InspectorControls, useBlockProps } = wp.blockEditor;
    const { ServerSideRender } = wp.serverSideRender;
    const { PanelBody, SelectControl, TextControl, RangeControl, ToggleControl } = wp.components;
    const { createElement: el, Fragment } = wp.element;

    // ============================================================================
    registerBlockType('mbs/coming-soon', {
        apiVersion: 3,
        title: 'Coming Soon (PRO)',
        description: 'Display upcoming books marked as Coming Soon. Requires PRO license.',
        icon: 'visibility',
        category: 'mbs',
        keywords: [ 'coming soon', 'upcoming', 'future', 'releases', 'pre-order' ],

        attributes: {
            limit: { type: 'number', default: 6 },
        },

        edit: function (props) {
            const { attributes, setAttributes } = props;

            return el(
                Fragment,
                {},
                el(
                    InspectorControls,
                    {},
                    el(
                        PanelBody,
                        { title: 'Coming Soon Settings', initialOpen: true },
                        el(RangeControl, {
                            label: 'Number of books',
                            value: attributes.limit,
                            onChange: function (value) { setAttributes({ limit: value }); },
                            min: 1,
                            max: 24,
                        })
                    )
                ),
                el(
                    'div',
                    useBlockProps({ className: 'mbs-block mbs-block-coming-soon' }),
                    el(ServerSideRender, {
                        block: 'mbs/coming-soon',
                        attributes: attributes,
                    })
                )
            );
        },

        save: function () {
            return null; // Dynamic block - rendered by PHP via render_callback
        },
    });

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