/**
 * @file assets/js/blocks/block-term-list.js
 * Registers the mbs/term-list 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/term-list', {
        apiVersion: 3,
        title: 'Term List',
        description: 'Display all terms from an MBS taxonomy (authors, series, genres, or tags) as a linked grid.',
        icon: 'list-view',
        category: 'mbs',
        keywords: [ 'terms', 'taxonomy', 'authors', 'genres', 'series list' ],

        attributes: {
            taxonomy:   { type: 'string',  default: 'mbs_author' },
            columns:    { type: 'number',  default: 3 },
            show_count: { type: 'boolean', default: false },
        },

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

            return el(
                Fragment,
                {},
                el(
                    InspectorControls,
                    {},
                    el(
                        PanelBody,
                        { title: 'Term List Settings', initialOpen: true },
                        el(SelectControl, {
                            label: 'Taxonomy',
                            value: attributes.taxonomy,
                            options: [
                                { label: 'Authors', value: 'mbs_author' },
                                { label: 'Series',  value: 'mbs_series' },
                                { label: 'Genres',  value: 'mbs_genre' },
                                { label: 'Tags',    value: 'mbs_tag' },
                            ],
                            onChange: function (value) {
                                setAttributes({ taxonomy: value });
                            },
                        }),
                        el(RangeControl, {
                            label: 'Columns',
                            value: attributes.columns,
                            onChange: function (value) {
                                setAttributes({ columns: value });
                            },
                            min: 1,
                            max: 6,
                        }),
                        el(ToggleControl, {
                            label: 'Show book count',
                            checked: attributes.show_count,
                            onChange: function (value) {
                                setAttributes({ show_count: value });
                            },
                        })
                    )
                ),
                el(
                    'div',
                    useBlockProps({ className: 'mbs-block mbs-block-term-list' }),
                    el(ServerSideRender, {
                        block: 'mbs/term-list',
                        attributes: attributes,
                    })
                )
            );
        },

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

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