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

import metadata from '../../../blocks/premium/wishlist-icon/block.json';

registerBlockType( metadata.name, {
    edit: ( { attributes, setAttributes } ) => (
        <>
            <InspectorControls>
                <PanelBody title="Content">
                    <ProductSearchControl
                        value={ attributes.productId }
                        onChange={ ( val ) => setAttributes( { productId: val } ) }
                    />
                    <ToggleControl
                        label="Show Count"
                        checked={ attributes.showCount === 'yes' }
                        onChange={ ( val ) => setAttributes( { showCount: val ? 'yes' : '' } ) }
                    />
                    <ToggleControl
                        label="Link to Wishlist Page"
                        checked={ attributes.linkToWishlist === 'yes' }
                        onChange={ ( val ) => setAttributes( { linkToWishlist: val ? 'yes' : '' } ) }
                    />
                    { attributes.linkToWishlist === 'yes' && (
                        <TextControl
                            label="Wishlist Page URL"
                            value={ attributes.wishlistPageUrl }
                            onChange={ ( val ) => setAttributes( { wishlistPageUrl: val } ) }
                        />
                    ) }
                </PanelBody>
            </InspectorControls>
            <ServerSideRender block={ metadata.name } attributes={ attributes } />
        </>
    ),
    save: () => null,
} );
