/**
 * @file assets/js/blocks/block-series.js
 * Registers the mbs/series Gutenberg block.
 * Depends on mbs-blocks-editor (wp.* globals + window.mbsBlocksCore helpers).
 * Helpers used: getTermOptions
 * @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 getTermOptions  = mbsCore.getTermOptions  || function() { return []; };

    // ============================================================================
    registerBlockType('mbs/series', {
        apiVersion: 3,
        title: 'Series',
        description: 'Display a series with its description, reading order, and book grid.',
        icon: 'book-alt',
        category: 'mbs',
        keywords: [ 'series', 'books', 'sequence', 'collection', 'reading order' ],

        attributes: {
            slug:     { type: 'string', default: '' },
            per_page: { type: 'number', default: 20 },
        },

        edit: function (props) {
            const { attributes, setAttributes } = props;
            const termOptions = getTermOptions('mbs_series');

            return el(
                Fragment,
                {},
                el(
                    InspectorControls,
                    {},
                    el(
                        PanelBody,
                        { title: 'Series Settings', initialOpen: true },
                        el(SelectControl, {
                            label: 'Select Series',
                            value: attributes.slug,
                            options: termOptions,
                            onChange: function (value) {
                                setAttributes({ slug: value });
                            },
                        }),
                        el(RangeControl, {
                            label: 'Books per page (-1 = all)',
                            value: attributes.per_page,
                            onChange: function (value) {
                                setAttributes({ per_page: value });
                            },
                            min: -1,
                            max: 100,
                        })
                    )
                ),
                el(
                    'div',
                    useBlockProps({ className: 'mbs-block mbs-block-series' }),
                    attributes.slug
                        ? el(ServerSideRender, {
                            block: 'mbs/series',
                            attributes: attributes,
                          })
                        : el(
                            'div',
                            { className: 'mbs-block-placeholder' },
                            'Select a series from the block settings. Manage seriess in My Book Showroom → Settings → Series.'
                          )
                )
            );
        },

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

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