/**
 * @file assets/js/blocks/block-series-directory.js
 * Registers the mbs/series-directory 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/series-directory', {
        apiVersion: 3,
        title: 'Series Directory (PRO)',
        description: 'Accordion listing every series with books in reading order. Requires PRO license.',
        icon: 'list-view',
        category: 'mbs',
        keywords: [ 'series', 'directory', 'list', 'browse', 'reading order' ],

        attributes: {
            sort: { type: 'string', default: 'name' },
        },

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

            return el(
                Fragment,
                {},
                el(
                    InspectorControls,
                    {},
                    el(
                        PanelBody,
                        { title: 'Series Directory Settings', initialOpen: true },
                        el(SelectControl, {
                            label: 'Sort series by',
                            value: attributes.sort,
                            options: [
                                { label: 'Name',                        value: 'name'      },
                                { label: 'Sort name (article-stripped)', value: 'name_sort' },
                            ],
                            onChange: function (value) { setAttributes({ sort: value }); },
                        })
                    )
                ),
                el(
                    'div',
                    useBlockProps({ className: 'mbs-block mbs-block-series-directory' }),
                    el(ServerSideRender, {
                        block: 'mbs/series-directory',
                        attributes: attributes,
                    })
                )
            );
        },

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

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