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-stock/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 Format"
                        value={ attributes.displayFormat }
                        options={ [
                            { label: 'Number Only (25)', value: 'number_only' },
                            { label: 'Text + Number (In Stock: 25)', value: 'text_number' },
                            { label: 'Status Only (In Stock)', value: 'status_only' },
                        ] }
                        onChange={ ( val ) => setAttributes( { displayFormat: val } ) }
                    />
                    { attributes.displayFormat !== 'number_only' && (
                        <>
                            <TextControl
                                label="In Stock Text"
                                value={ attributes.inStockText }
                                onChange={ ( val ) => setAttributes( { inStockText: val } ) }
                            />
                            <TextControl
                                label="Low Stock Text"
                                value={ attributes.lowStockText }
                                onChange={ ( val ) => setAttributes( { lowStockText: val } ) }
                            />
                        </>
                    ) }
                    <TextControl
                        label="Out of Stock Text"
                        value={ attributes.outOfStockText }
                        onChange={ ( val ) => setAttributes( { outOfStockText: val } ) }
                    />
                    <TextControl
                        label="Low Stock Threshold"
                        type="number"
                        value={ attributes.lowStockThreshold }
                        onChange={ ( val ) => setAttributes( { lowStockThreshold: parseInt( val, 10 ) || 1 } ) }
                    />
                    <ToggleControl
                        label="Show Status Icon"
                        checked={ attributes.showIcon === 'yes' }
                        onChange={ ( val ) => setAttributes( { showIcon: val ? 'yes' : '' } ) }
                    />
                </PanelBody>
            </InspectorControls>
            <ServerSideRender
                block={ metadata.name }
                attributes={ attributes }
            />
        </>
    ),
    save: () => null,
} );
