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

import metadata from '../../../blocks/premium/product-barcode/block.json';

registerBlockType( metadata.name, {
    edit: ( { attributes, setAttributes } ) => (
        <>
            <InspectorControls>
                <PanelBody title="Content">
                    <ProductSearchControl
                        value={ attributes.productId }
                        onChange={ ( val ) => setAttributes( { productId: val } ) }
                    />
                    <SelectControl
                        label="Display"
                        value={ attributes.barcode_display }
                        options={ [
                            { label: 'Primary Barcode Only', value: 'primary' },
                            { label: 'All Barcodes', value: 'all' },
                        ] }
                        onChange={ ( val ) => setAttributes( { barcode_display: val } ) }
                    />
                    <ToggleControl
                        label="Show Barcode Type"
                        checked={ attributes.show_barcode_type === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_barcode_type: val ? 'yes' : '' } ) }
                    />
                    <ToggleControl
                        label="Show Barcode Image"
                        checked={ attributes.show_barcode_image === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_barcode_image: val ? 'yes' : '' } ) }
                    />
                    <SelectControl
                        label="Layout"
                        value={ attributes.layout }
                        options={ [
                            { label: 'Inline', value: 'inline' },
                            { label: 'Stacked', value: 'stacked' },
                            { label: 'Table', value: 'table' },
                        ] }
                        onChange={ ( val ) => setAttributes( { layout: val } ) }
                    />
                    <TextControl
                        label="Label Text"
                        value={ attributes.label_text }
                        onChange={ ( val ) => setAttributes( { label_text: val } ) }
                    />
                    <ToggleControl
                        label="Hide if No Barcode"
                        checked={ attributes.hide_if_empty === 'yes' }
                        onChange={ ( val ) => setAttributes( { hide_if_empty: val ? 'yes' : '' } ) }
                    />
                </PanelBody>
            </InspectorControls>
            <ServerSideRender block={ metadata.name } attributes={ attributes } />
        </>
    ),
    save: () => null,
} );
