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

export interface SwitchLabelBaseProps extends PolymorphicProps<'span'> {}
export interface SwitchLabelProps extends HTMLProps<'span'>, SwitchLabelBaseProps {}

export component SwitchLabel(props: MaybeTracked<SwitchLabelProps>) {
  const [children, localProps] = trackSplit(props, ['children']);
  const api = useSwitchContext();

  let mergedProps = track(() => mergeProps(@api.getLabelProps(), @localProps));

  <ark.span {...@mergedProps}>
    <@children />
  </ark.span>
}
