import { registerBlockType } from '@wordpress/blocks';
import { InspectorControls } from '@wordpress/block-editor';
import ServerSideRender from '@wordpress/server-side-render';
import { PanelBody, SelectControl, ToggleControl, TextControl } from '@wordpress/components';
import ProductSearchControl from '../../components/ProductSearchControl';

import metadata from '../../../blocks/tags/product-name/block.json';

registerBlockType( metadata.name, {
    edit: ( { attributes, setAttributes } ) => (
        <>
            <InspectorControls>
                <PanelBody title="Content">
                    <SelectControl
                        label="Product Source"
                        value={ attributes.productSource }
                        options={ [
                            { label: 'Current Product (Auto-detect)', value: 'current' },
                            { label: 'Select Product', value: 'custom' },
                        ] }
                        onChange={ ( val ) => setAttributes( { productSource: val } ) }
                    />
                    { attributes.productSource === 'custom' && (
                        <ProductSearchControl
                            value={ attributes.productId }
                            onChange={ ( val ) => setAttributes( { productId: val } ) }
                        />
                    ) }
                    <SelectControl
                        label="HTML Tag"
                        value={ attributes.htmlTag }
                        options={ [
                            { label: 'H1', value: 'h1' },
                            { label: 'H2', value: 'h2' },
                            { label: 'H3', value: 'h3' },
                            { label: 'H4', value: 'h4' },
                            { label: 'H5', value: 'h5' },
                            { label: 'H6', value: 'h6' },
                            { label: 'div', value: 'div' },
                            { label: 'span', value: 'span' },
                            { label: 'p', value: 'p' },
                        ] }
                        onChange={ ( val ) => setAttributes( { htmlTag: val } ) }
                    />
                    <ToggleControl
                        label="Enable Link"
                        checked={ attributes.enableLink === 'yes' }
                        onChange={ ( val ) => setAttributes( { enableLink: val ? 'yes' : '' } ) }
                    />
                    { attributes.enableLink === 'yes' && (
                        <>
                            <TextControl
                                label="Link URL"
                                value={ attributes.link.url }
                                onChange={ ( val ) =>
                                    setAttributes( { link: { ...attributes.link, url: val } } )
                                }
                            />
                            <ToggleControl
                                label="Open in New Tab"
                                checked={ attributes.link.is_external }
                                onChange={ ( val ) =>
                                    setAttributes( { link: { ...attributes.link, is_external: val } } )
                                }
                            />
                            <ToggleControl
                                label="Nofollow"
                                checked={ attributes.link.nofollow }
                                onChange={ ( val ) =>
                                    setAttributes( { link: { ...attributes.link, nofollow: val } } )
                                }
                            />
                        </>
                    ) }
                    <SelectControl
                        label="Alignment"
                        value={ attributes.alignment }
                        options={ [
                            { label: 'Default', value: '' },
                            { label: 'Left', value: 'left' },
                            { label: 'Center', value: 'center' },
                            { label: 'Right', value: 'right' },
                            { label: 'Justify', value: 'justify' },
                        ] }
                        onChange={ ( val ) => setAttributes( { alignment: val } ) }
                    />
                </PanelBody>
            </InspectorControls>
            <ServerSideRender
                block={ metadata.name }
                attributes={ attributes }
            />
        </>
    ),
    save: () => null,
} );
