/**
 * @file assets/js/blocks/block-tags.js
 * Registers the mbs/tags 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, RangeControl } = 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/tags', {
        apiVersion: 3,
        title: 'Tag',
        description: 'Display all books carrying a specific tag.',
        icon: 'tag',
        category: 'mbs',
        keywords: [ 'tag', 'trope', 'character', 'setting', 'books', 'filter' ],

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

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

            return el(
                Fragment,
                {},
                el(
                    InspectorControls,
                    {},
                    el(
                        PanelBody,
                        { title: 'Tag Settings', initialOpen: true },
                        el( SelectControl, {
                            label: 'Select Tag',
                            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-tags' } ),
                    attributes.slug
                        ? el( ServerSideRender, {
                            block: 'mbs/tags',
                            attributes: attributes,
                          } )
                        : el(
                            'div',
                            { className: 'mbs-block-placeholder' },
                            'Select a tag from the block settings. Manage tags in My Book Showroom → Settings → Tags.'
                          )
                )
            );
        },

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

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