/**
 * Biscotti CMP — Gutenberg Block Editor Blocks
 *
 * Registers 8 blocks using wp.blocks.registerBlockType with
 * wp.serverSideRender for live preview in the editor.
 *
 * No build step required — uses standard WP block API.
 *
 * @package Biscotti_CMP
 * @since 3.1.0
 */
(function () {
    var el = wp.element.createElement;
    var registerBlockType = wp.blocks.registerBlockType;
    var ServerSideRender = wp.serverSideRender;
    var InspectorControls = wp.blockEditor.InspectorControls;
    var PanelBody = wp.components.PanelBody;
    var SelectControl = wp.components.SelectControl;
    var ToggleControl = wp.components.ToggleControl;
    var TextControl = wp.components.TextControl;
    var i18n = biscottiBlocks.i18n;
    var langOptions = biscottiBlocks.langOptions;

    // =========================================================================
    // BLOCK CATEGORY
    // =========================================================================
    wp.blocks.updateCategory && wp.blocks.getCategories().filter(function (c) {
        return c.slug === 'biscotti';
    }).length === 0 && wp.blocks.registerBlockCollection && wp.blocks.registerBlockCollection('biscotti', { title: i18n.categoryTitle });

    // Fallback: add category via filter
    if (wp.hooks && wp.hooks.addFilter) {
        wp.hooks.addFilter('blocks.registerBlockType', 'biscotti/add-category', function (settings, name) {
            if (name && name.indexOf('biscotti/') === 0) {
                if (!settings.category) settings.category = 'widgets';
            }
            return settings;
        });
    }

    // =========================================================================
    // 1. COOKIE TABLE
    // =========================================================================
    registerBlockType('biscotti/cookie-table', {
        title: i18n.cookieTable,
        description: i18n.cookieTableDesc,
        icon: 'editor-table',
        category: 'widgets',
        keywords: ['biscotti', 'cookie', 'gdpr', 'table'],
        attributes: {
            category: { type: 'string', default: 'all' },
            showDuration: { type: 'boolean', default: true },
            showProvider: { type: 'boolean', default: true },
        },
        edit: function (props) {
            return el('div', {},
                el(InspectorControls, {},
                    el(PanelBody, { title: i18n.categoryTitle, initialOpen: true },
                        el(SelectControl, {
                            label: i18n.category,
                            value: props.attributes.category,
                            options: [
                                { value: 'all', label: i18n.all },
                                { value: 'essential', label: i18n.essential },
                                { value: 'functional', label: i18n.functional },
                                { value: 'analytics', label: i18n.analytics },
                                { value: 'marketing', label: i18n.marketing },
                            ],
                            onChange: function (val) { props.setAttributes({ category: val }); },
                        }),
                        el(ToggleControl, { label: i18n.showDuration, checked: props.attributes.showDuration, onChange: function (val) { props.setAttributes({ showDuration: val }); } }),
                        el(ToggleControl, { label: i18n.showProvider, checked: props.attributes.showProvider, onChange: function (val) { props.setAttributes({ showProvider: val }); } })
                    )
                ),
                el(ServerSideRender, { block: 'biscotti/cookie-table', attributes: props.attributes })
            );
        },
        save: function () { return null; },
    });

    // =========================================================================
    // 2. CONSENT LINK
    // =========================================================================
    registerBlockType('biscotti/consent-link', {
        title: i18n.consentLink,
        description: i18n.consentLinkDesc,
        icon: 'admin-links',
        category: 'widgets',
        keywords: ['biscotti', 'consent', 'cookie', 'link'],
        attributes: {
            text: { type: 'string', default: '' },
            cssClass: { type: 'string', default: 'biscotti-consent-link' },
        },
        edit: function (props) {
            return el('div', {},
                el(InspectorControls, {},
                    el(PanelBody, { title: i18n.consentLink, initialOpen: true },
                        el(TextControl, { label: i18n.linkText, value: props.attributes.text, onChange: function (val) { props.setAttributes({ text: val }); } }),
                        el(TextControl, { label: i18n.cssClass, value: props.attributes.cssClass, onChange: function (val) { props.setAttributes({ cssClass: val }); } })
                    )
                ),
                el(ServerSideRender, { block: 'biscotti/consent-link', attributes: props.attributes })
            );
        },
        save: function () { return null; },
    });

    // =========================================================================
    // 3. SETTINGS LINK
    // =========================================================================
    registerBlockType('biscotti/settings-link', {
        title: i18n.settingsLink,
        description: i18n.settingsLinkDesc,
        icon: 'admin-generic',
        category: 'widgets',
        keywords: ['biscotti', 'settings', 'cookie'],
        attributes: {
            text: { type: 'string', default: '' },
            cssClass: { type: 'string', default: 'biscotti-settings-link' },
        },
        edit: function (props) {
            return el('div', {},
                el(InspectorControls, {},
                    el(PanelBody, { title: i18n.settingsLink, initialOpen: true },
                        el(TextControl, { label: i18n.linkText, value: props.attributes.text, onChange: function (val) { props.setAttributes({ text: val }); } }),
                        el(TextControl, { label: i18n.cssClass, value: props.attributes.cssClass, onChange: function (val) { props.setAttributes({ cssClass: val }); } })
                    )
                ),
                el(ServerSideRender, { block: 'biscotti/settings-link', attributes: props.attributes })
            );
        },
        save: function () { return null; },
    });

    // =========================================================================
    // 4. DATA EXPORT
    // =========================================================================
    registerBlockType('biscotti/data-export', {
        title: i18n.dataExport,
        description: i18n.dataExportDesc,
        icon: 'download',
        category: 'widgets',
        keywords: ['biscotti', 'gdpr', 'export', 'data'],
        edit: function (props) {
            return el(ServerSideRender, { block: 'biscotti/data-export', attributes: props.attributes });
        },
        save: function () { return null; },
    });

    // =========================================================================
    // 5. DO NOT SELL (CCPA)
    // =========================================================================
    registerBlockType('biscotti/do-not-sell', {
        title: i18n.doNotSell,
        description: i18n.doNotSellDesc,
        icon: 'shield',
        category: 'widgets',
        keywords: ['biscotti', 'ccpa', 'do not sell', 'privacy'],
        attributes: {
            text: { type: 'string', default: '' },
        },
        edit: function (props) {
            return el('div', {},
                el(InspectorControls, {},
                    el(PanelBody, { title: i18n.doNotSell, initialOpen: true },
                        el(TextControl, { label: i18n.linkText, value: props.attributes.text, onChange: function (val) { props.setAttributes({ text: val }); } })
                    )
                ),
                el(ServerSideRender, { block: 'biscotti/do-not-sell', attributes: props.attributes })
            );
        },
        save: function () { return null; },
    });

    // =========================================================================
    // LEGAL DOCUMENT BLOCKS (shared helper)
    // =========================================================================
    function registerLegalBlock(slug, title, description, icon) {
        registerBlockType('biscotti/' + slug, {
            title: title,
            description: description,
            icon: icon,
            category: 'widgets',
            keywords: ['biscotti', 'legal', 'gdpr', slug.replace('-', ' ')],
            attributes: {
                enableCache: { type: 'boolean', default: true },
                lang: { type: 'string', default: 'auto' },
            },
            edit: function (props) {
                var langOpts = langOptions.map(function (o) { return { value: o.value, label: o.label }; });
                return el('div', {},
                    el(InspectorControls, {},
                        el(PanelBody, { title: title, initialOpen: true },
                            el(ToggleControl, { label: i18n.enableCache, checked: props.attributes.enableCache, onChange: function (val) { props.setAttributes({ enableCache: val }); } }),
                            el(SelectControl, { label: i18n.language, value: props.attributes.lang, options: langOpts, onChange: function (val) { props.setAttributes({ lang: val }); } })
                        )
                    ),
                    el(ServerSideRender, { block: 'biscotti/' + slug, attributes: props.attributes })
                );
            },
            save: function () { return null; },
        });
    }

    // 6. Impressum
    registerLegalBlock('impressum', i18n.impressum, i18n.impressumDesc, 'media-document');

    // 7. Privacy Policy
    registerLegalBlock('privacy-policy', i18n.privacyPolicy, i18n.privacyPolicyDesc, 'lock');

    // 8. Cookie Policy
    registerLegalBlock('cookie-policy', i18n.cookiePolicy, i18n.cookiePolicyDesc, 'info-outline');

})();
