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 metadata from '../../../blocks/premium/applied-discount/block.json';

registerBlockType( metadata.name, {
    edit: ( { attributes, setAttributes } ) => (
        <>
            <InspectorControls>
                <PanelBody title="Content">
                    <ToggleControl
                        label="Show Discount Code"
                        checked={ attributes.show_code === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_code: val ? 'yes' : '' } ) }
                    />
                    <ToggleControl
                        label="Show Discount Amount"
                        checked={ attributes.show_amount === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_amount: val ? 'yes' : '' } ) }
                    />
                    <ToggleControl
                        label="Show Remove Button"
                        checked={ attributes.show_remove_button === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_remove_button: val ? 'yes' : '' } ) }
                    />
                    <TextControl
                        label="Empty State Text"
                        value={ attributes.empty_message }
                        onChange={ ( val ) => setAttributes( { empty_message: val } ) }
                        help="Text to show when no discount is applied (leave empty to hide widget)"
                    />
                    <TextControl
                        label="Discount Label"
                        value={ attributes.discount_label }
                        onChange={ ( val ) => setAttributes( { discount_label: val } ) }
                    />
                </PanelBody>
            </InspectorControls>
            <ServerSideRender block={ metadata.name } attributes={ attributes } />
        </>
    ),
    save: () => null,
} );
