/**
 * @file assets/js/blocks/block-buy-links.js
 * Registers the mbs/buy-links Gutenberg block.
 * Depends on mbs-blocks-editor (wp.* globals + window.mbsBlocksCore helpers).
 * Helpers used: getBookOptions
 * @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 getBookOptions = mbsCore.getBookOptions || function() { return []; };

    // ============================================================================
    registerBlockType('mbs/buy-links', {
        apiVersion: 3,
        title: 'Buy Links',
        icon: 'cart',
        category: 'mbs',
        keywords: [ 'buy', 'purchase', 'links', 'retailer', 'amazon', 'store' ],

        attributes: {
            bookId:  { type: 'number', default: 0 },
            context: { type: 'string', default: 'single' },
        },

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

            return el(
                Fragment,
                {},
                el(
                    InspectorControls,
                    {},
                    el(
                        PanelBody,
                        { title: 'Buy Links Settings' },
                        el(SelectControl, {
                            label: 'Select Book',
                            value: attributes.bookId,
                            options: bookOptions,
                            onChange: function (value) {
                                setAttributes({ bookId: parseInt(value, 10) || 0 });
                            },
                        }),
                        el(SelectControl, {
                            label: 'Display Context',
                            value: attributes.context,
                            options: [
                                { label: 'Single book page', value: 'single' },
                                { label: 'Grid / card',      value: 'grid' },
                            ],
                            onChange: function (value) {
                                setAttributes({ context: value });
                            },
                        })
                    )
                ),
                el(
                    'div',
                    useBlockProps({ className: 'mbs-block mbs-block-buy-links' }),
                    attributes.bookId
                        ? el(ServerSideRender, {
                            block: 'mbs/buy-links',
                            attributes: attributes,
                        })
                        : el(
                            'div',
                            { className: 'mbs-block-placeholder' },
                            'Select a book in the block settings to preview buy links.'
                        )
                )
            );
        },

        save: function () {
            return null;
        },
    });

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