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/discount-code-input/block.json';

registerBlockType( metadata.name, {
    edit: ( { attributes, setAttributes } ) => (
        <>
            <InspectorControls>
                <PanelBody title="Content">
                    <TextControl
                        label="Input Placeholder"
                        value={ attributes.input_placeholder }
                        onChange={ ( val ) => setAttributes( { input_placeholder: val } ) }
                    />
                    <TextControl
                        label="Button Text"
                        value={ attributes.button_text }
                        onChange={ ( val ) => setAttributes( { button_text: val } ) }
                    />
                    <ToggleControl
                        label="Show Label"
                        checked={ attributes.show_label === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_label: val ? 'yes' : '' } ) }
                    />
                    { attributes.show_label === 'yes' && (
                        <TextControl
                            label="Label Text"
                            value={ attributes.label_text }
                            onChange={ ( val ) => setAttributes( { label_text: val } ) }
                        />
                    ) }
                    <ToggleControl
                        label="Collapsible"
                        checked={ attributes.collapsible === 'yes' }
                        onChange={ ( val ) => setAttributes( { collapsible: val ? 'yes' : '' } ) }
                    />
                </PanelBody>
            </InspectorControls>
            <ServerSideRender block={ metadata.name } attributes={ attributes } />
        </>
    ),
    save: () => null,
} );
