import { mergeProps } from 'zag-ripple';
import { trackSplit, track } from 'ripple';
import { useSliderContext } from './use-slider-context';
import { useSliderThumbPropsContext } from './use-slider-thumb-props-context';
import { ark } from '../factory';
import type { HTMLProps, MaybeTracked, PolymorphicProps } from '../../types';

export interface SliderDraggingIndicatorBaseProps extends PolymorphicProps<'span'> {}
export interface SliderDraggingIndicatorProps extends HTMLProps<
  'span'
>, SliderDraggingIndicatorBaseProps {}

export component SliderDraggingIndicator(props: MaybeTracked<SliderDraggingIndicatorProps>) {
  const [children, localProps] = trackSplit(props, ['children']);
  const slider = useSliderContext();
  const thumbProps = useSliderThumbPropsContext();

  let mergedProps = track(
    () => mergeProps(@slider.getDraggingIndicatorProps({ index: @thumbProps.index }), @localProps),
  );

  <ark.span {...@mergedProps}>{@children ?? @slider.getThumbValue(@thumbProps.index)}</ark.span>
}
