
( function( wp ) {
    const { __ } = wp.i18n;
    const { registerPlugin } = wp.plugins;
    const { PluginDocumentSettingPanel } = wp.editPost || {};
    const { PanelBody, TextareaControl, Notice } = wp.components;
    const { useSelect, dispatch } = wp.data;
    const { createElement: el, Fragment, useState, useMemo } = wp.element;

    const Panel = () => {
        const postType = useSelect( sel => sel('core/editor').getCurrentPostType(), [] );
        const meta = useSelect( sel => sel('core/editor').getEditedPostAttribute('meta') || {}, [] );
        const sameas = meta && meta.sameas ? meta.sameas : '';

        const setSameAs = ( value ) => {
            dispatch('core/editor').editPost({ meta: { sameas: value } });
        };

        const help = __('Inserisci uno o più URL, uno per riga. Verranno emessi come "sameAs" nello JSON-LD.', 'iron-seo3');

        const Body = ( props ) => el( PanelBody,
            { title: __('SameAs (JSON-LD)', 'iron-seo3'), initialOpen: true },
            el( TextareaControl, {
                label: __('URL SameAs', 'iron-seo3'),
                help,
                value: sameas,
                onChange: setSameAs,
                rows: 6,
                placeholder: 'https://example.com/...\nhttps://twitter.com/...\nhttps://www.wikidata.org/wiki/Q...'
            } )
        );

        // Prefer PluginDocumentSettingPanel when available
        if ( PluginDocumentSettingPanel ) {
            return el( PluginDocumentSettingPanel,
                { name: 'iron-seo3-sameas', title: __('SameAs (JSON-LD)', 'iron-seo3'), className: 'iron-seo3-sameas-panel' },
                el( TextareaControl, {
                    label: __('URL SameAs', 'iron-seo3'),
                    help,
                    value: sameas,
                    onChange: setSameAs,
                    rows: 6,
                    placeholder: 'https://example.com/...\nhttps://twitter.com/...\nhttps://www.wikidata.org/wiki/Q...'
                } )
            );
        }
        return el( Body );
    };

    registerPlugin( 'iron-seo3-sameas-panel', { render: Panel, icon: null } );
} )( window.wp );
