import { __ } from '@wordpress/i18n';
import { InspectorControls } from '@wordpress/block-editor';
import { PanelBody, TextControl, SelectControl, ToggleControl } from '@wordpress/components';

export default function Edit( { attributes, setAttributes } ) {
	const { fieldPath, linkType, linkFieldPath, hideIfEmpty } = attributes;

	return (
		<>
			<InspectorControls>
				<PanelBody title={ __( 'Result Title', 'wlc-search-integration-with-algolia' ) } initialOpen={ true }>
					<TextControl
						label={ __( 'Title field path', 'wlc-search-integration-with-algolia' ) }
						value={ fieldPath || '' }
						onChange={ ( v ) => setAttributes( { fieldPath: v } ) }
					/>
					<SelectControl
						label={ __( 'Link', 'wlc-search-integration-with-algolia' ) }
						value={ linkType || 'permalink' }
						options={ [
							{ label: __( 'Permalink', 'wlc-search-integration-with-algolia' ), value: 'permalink' },
							{ label: __( 'None', 'wlc-search-integration-with-algolia' ), value: 'none' },
							{ label: __( 'Custom URL (field)', 'wlc-search-integration-with-algolia' ), value: 'field' },
						] }
						onChange={ ( v ) => setAttributes( { linkType: v } ) }
					/>
					{ linkType === 'field' ? (
						<TextControl
							label={ __( 'Link field path', 'wlc-search-integration-with-algolia' ) }
							value={ linkFieldPath || '' }
							onChange={ ( v ) => setAttributes( { linkFieldPath: v } ) }
						/>
					) : null }
					<ToggleControl
						label={ __( 'Hide if empty', 'wlc-search-integration-with-algolia' ) }
						checked={ hideIfEmpty !== false }
						onChange={ ( v ) => setAttributes( { hideIfEmpty: !! v } ) }
					/>
				</PanelBody>
			</InspectorControls>

			<div style={ { fontWeight: 700, fontSize: 16 } }>
				{ __( 'Title', 'wlc-search-integration-with-algolia' ) } <span style={ { opacity: 0.6 } }>{ fieldPath ? `(${ fieldPath })` : '' }</span>
			</div>
		</>
	);
}


