/**
 * @file assets/js/blocks/block-collection.js
 * Registers the mbs/collection 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/collection', {
        apiVersion: 3,
        title: 'Collection',
        description: 'Display a collection with its type, description, and book grid.',
        icon: 'category',
        category: 'mbs',
        keywords: [ 'collection', 'books', 'bundle', 'group', 'set' ],

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

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

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

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

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