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/stripe-gateway/block.json';

registerBlockType( metadata.name, {
    edit: ( { attributes, setAttributes } ) => (
        <>
            <InspectorControls>
                <PanelBody title="Content">
                    <ToggleControl
                        label="Show Title"
                        checked={ attributes.show_title === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_title: val ? 'yes' : '' } ) }
                    />
                    { attributes.show_title === 'yes' && (
                        <TextControl
                            label="Title"
                            value={ attributes.title_text }
                            onChange={ ( val ) => setAttributes( { title_text: val } ) }
                        />
                    ) }
                    <ToggleControl
                        label="Show Payment Icons"
                        checked={ attributes.show_icons === 'yes' }
                        onChange={ ( val ) => setAttributes( { show_icons: val ? 'yes' : '' } ) }
                    />
                </PanelBody>
            </InspectorControls>
            <ServerSideRender block={ metadata.name } attributes={ attributes } />
        </>
    ),
    save: () => null,
} );
