/**
 * @file assets/js/blocks/block-book-teaser.js
 * Registers the mbs/book-teaser Gutenberg block.
 * Depends on mbs-blocks-editor (wp.* globals + window.mbsBlocksCore helpers).
 * Helpers used: getBookOptions
 * @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;

    // Shared helpers provided by blocks-editor.js via window.mbsBlocksCore.
    const getBookOptions = mbsCore.getBookOptions || function() { return []; };

    // ============================================================================
    registerBlockType('mbs/book-teaser', {
        apiVersion: 3,
        title: 'Book Teaser',
        description: 'Display the marketing teaser blurb for a book, with optional cover thumbnail.',
        icon: 'text-page',
        category: 'mbs',
        keywords: [ 'teaser', 'blurb', 'excerpt', 'preview', 'description' ],

        attributes: {
            bookId:     { type: 'number',  default: 0     },
            show_cover: { type: 'boolean', default: false },
        },

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

            return el(
                Fragment,
                {},
                el(
                    InspectorControls,
                    {},
                    el(
                        PanelBody,
                        { title: 'Book Teaser Settings', initialOpen: true },
                        el(SelectControl, {
                            label: 'Select Book',
                            value: attributes.bookId,
                            options: bookOptions,
                            onChange: function (value) {
                                setAttributes({ bookId: parseInt(value, 10) });
                            },
                        }),
                        el(ToggleControl, {
                            label: 'Show cover thumbnail',
                            checked: attributes.show_cover,
                            onChange: function (value) { setAttributes({ show_cover: value }); },
                        })
                    )
                ),
                el(
                    'div',
                    useBlockProps({ className: 'mbs-block mbs-block-book-teaser' }),
                    attributes.bookId
                        ? el(ServerSideRender, {
                            block: 'mbs/book-teaser',
                            attributes: attributes,
                          })
                        : el(
                            'div',
                            { className: 'mbs-block-placeholder' },
                            'Select a book from the block settings to display its teaser.'
                          )
                )
            );
        },

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

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