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

export interface SelectValueTextBaseProps extends PolymorphicProps<'span'> {
  placeholder?: string | undefined;
}
export interface SelectValueTextProps extends HTMLProps<'span'>, SelectValueTextBaseProps {}

export component SelectValueText(props: MaybeTracked<SelectValueTextProps>) {
  const [placeholder, children, localProps] = trackSplit(props, ['placeholder', 'children']);
  const select = useSelectContext();

  let mergedProps = track(() => mergeProps(@select.getValueTextProps(), @localProps));

  <ark.span {...@mergedProps}>
    if (@children) {
      <@children />
    } else {
      {@select.valueAsString || @placeholder}
    }
  </ark.span>
}
