import { useState } from '@wordpress/element';
import type { StoryObj, Meta } from '@storybook/react-vite';
import { formDecorator } from './story-utils';
import { ValidatedContentEditableControl } from '../content-editable-control';

const meta: Meta< typeof ValidatedContentEditableControl > = {
	parameters: {
		// FIXME: Contenteditable control is unlabeled (label).
		// See: https://github.com/WordPress/gutenberg/issues/81596
		a11y: { test: 'todo' },
	},

	title: 'Components/@wordpress-components/Selection & Input/Validated Form Controls/ValidatedContentEditableControl',
	id: 'components-validatedcontenteditablecontrol',
	component: ValidatedContentEditableControl,
	tags: [ 'status-private' ],
	decorators: formDecorator,
	argTypes: { value: { control: false } },
};
export default meta;

export const Default: StoryObj< typeof ValidatedContentEditableControl > = {
	render: function Template( args ) {
		// The control is presentational: the editable manages its own
		// contents, and the consumer mirrors the plain text into `value`
		// for the validity delegate.
		const [ value, setValue ] = useState( '' );

		return (
			<ValidatedContentEditableControl
				{ ...args }
				value={ value }
				onInput={ ( e ) =>
					setValue( e.currentTarget.textContent ?? '' )
				}
				customValidity={
					value.toLowerCase().includes( 'error' )
						? {
								type: 'invalid',
								message: 'The word "error" is not allowed.',
							}
						: undefined
				}
			/>
		);
	},
};
Default.args = {
	required: true,
	label: 'Content',
	help: 'The word "error" will trigger an error.',
};
