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

import metadata from '../../../blocks/tags/product-details-link/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 } ) }
                        />
                    ) }
                    <TextControl
                        label="Link Text"
                        value={ attributes.linkText }
                        onChange={ ( val ) => setAttributes( { linkText: val } ) }
                    />
                    <SelectControl
                        label="Link Style"
                        value={ attributes.linkStyle }
                        options={ [
                            { label: 'Text Link', value: 'text' },
                            { label: 'Button', value: 'button' },
                            { label: 'Outline Button', value: 'outline' },
                        ] }
                        onChange={ ( val ) => setAttributes( { linkStyle: val } ) }
                    />
                    <ToggleControl
                        label="Close Popup on Click"
                        checked={ attributes.closePopup === 'yes' }
                        onChange={ ( val ) => setAttributes( { closePopup: val ? 'yes' : '' } ) }
                    />
                    <SelectControl
                        label="Alignment"
                        value={ attributes.align }
                        options={ [
                            { label: 'Left', value: 'left' },
                            { label: 'Center', value: 'center' },
                            { label: 'Right', value: 'right' },
                        ] }
                        onChange={ ( val ) => setAttributes( { align: val } ) }
                    />
                </PanelBody>
            </InspectorControls>
            <ServerSideRender
                block={ metadata.name }
                attributes={ attributes }
            />
        </>
    ),
    save: () => null,
} );
