import { mergeProps } from 'zag-ripple';
import { trackSplit, track } from 'ripple';
import { useFieldContext } from './use-field-context';
import { ark } from '../factory';
import type { HTMLProps, MaybeTracked, PolymorphicProps } from '../../types';

export interface FieldErrorTextBaseProps extends PolymorphicProps<'span'> {}
export interface FieldErrorTextProps extends HTMLProps<'span'>, FieldErrorTextBaseProps {}

export component FieldErrorText(props: MaybeTracked<FieldErrorTextProps>) {
  const [children, localProps] = trackSplit(props, ['children']);
  const field = useFieldContext();

  const mergedProps = track(() => mergeProps(@field?.getErrorTextProps(), @localProps));

  if (@field?.invalid) {
    <ark.span {...@mergedProps}>
      <@children />
    </ark.span>
  }
}
