/**
 * @file assets/js/blocks/block-author.js
 * Registers the mbs/author 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/author', {
        apiVersion: 3,
        title: 'Author',
        description: 'Display an author with bio, social links, and their book grid.',
        icon: 'admin-users',
        category: 'mbs',
        keywords: [ 'author', 'bio', 'writer', 'biography', 'books' ],

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

        // FREE layouts available to all; PRO slugs are passed through to PHP
        // and fall back to 'grid' when mbs_is_pro() is false on the server.
        // Mirrors the pattern in mbs-blocks.js (LAYOUT_OPTIONS).
        // 'hero' omitted here — it is a single-book feature, not meaningful
        // for "books by author" grids.
        /* layout options defined inline in edit() below */

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

            return el(
                Fragment,
                {},
                el(
                    InspectorControls,
                    {},
                    el(
                        PanelBody,
                        { title: 'Author Settings', initialOpen: true },
                        el(SelectControl, {
                            label: 'Select Author',
                            value: attributes.slug,
                            options: termOptions,
                            onChange: function (value) {
                                setAttributes({ slug: value });
                            },
                        }),
                        el(SelectControl, {
                            label: 'Layout',
                            value: attributes.layout,
                            options: [
                                { label: 'Grid',              value: 'grid'    },
                                { label: 'List',              value: 'list'    },
                                { label: 'Masonry',           value: 'masonry' },
                                { label: 'Card',              value: 'card'    },
                                { label: 'Hero',              value: 'hero'    },
                                { label: 'Slider (PRO)',      value: 'slider'  },
                                { label: 'Flip (PRO)',        value: 'flip'    },
                                { label: 'Filter (PRO)',      value: 'filter'  },
                                { label: 'Popup (PRO)',       value: 'popup'   },
                            ],
                            onChange: function (value) {
                                setAttributes({ layout: 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-author' }),
                    attributes.slug
                        ? el(ServerSideRender, {
                            block: 'mbs/author',
                            attributes: attributes,
                          })
                        : el(
                            'div',
                            { className: 'mbs-block-placeholder' },
                            'Select a author from the block settings. Manage authors in My Book Showroom → Settings → Authors.'
                          )
                )
            );
        },

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

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