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/premium/product-rating/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="Display Style"
                        value={ attributes.displayStyle }
                        options={ [
                            { label: 'Stars Only', value: 'stars_only' },
                            { label: 'Stars + Review Count', value: 'stars_count' },
                            { label: 'Stars + Average Rating', value: 'stars_average' },
                            { label: 'Stars + Average + Count', value: 'stars_full' },
                        ] }
                        onChange={ ( val ) => setAttributes( { displayStyle: val } ) }
                    />
                    <SelectControl
                        label="Star Style"
                        value={ attributes.starStyle }
                        options={ [
                            { label: 'Filled Stars', value: 'filled' },
                            { label: 'Outline Stars', value: 'outline' },
                        ] }
                        onChange={ ( val ) => setAttributes( { starStyle: val } ) }
                    />
                    <ToggleControl
                        label="Show Empty Stars"
                        checked={ attributes.showEmptyStars === 'yes' }
                        onChange={ ( val ) => setAttributes( { showEmptyStars: val ? 'yes' : '' } ) }
                    />
                    <ToggleControl
                        label="Link to Reviews"
                        checked={ attributes.linkToReviews === 'yes' }
                        onChange={ ( val ) => setAttributes( { linkToReviews: val ? 'yes' : '' } ) }
                    />
                    { attributes.linkToReviews === 'yes' && (
                        <TextControl
                            label="Reviews Section ID"
                            value={ attributes.reviewsAnchor }
                            onChange={ ( val ) => setAttributes( { reviewsAnchor: val } ) }
                            help="The ID of the reviews section to scroll to."
                        />
                    ) }
                    <TextControl
                        label="No Reviews Text"
                        value={ attributes.noReviewsText }
                        onChange={ ( val ) => setAttributes( { noReviewsText: val } ) }
                    />
                    <ToggleControl
                        label="Hide if No Reviews"
                        checked={ attributes.hideIfNoReviews === 'yes' }
                        onChange={ ( val ) => setAttributes( { hideIfNoReviews: val ? 'yes' : '' } ) }
                    />
                </PanelBody>
            </InspectorControls>
            <ServerSideRender block={ metadata.name } attributes={ attributes } />
        </>
    ),
    save: () => null,
} );
