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

export interface SwapRootProviderBaseProps extends PolymorphicProps<'span'> {
  value: UseSwapReturn;
}
export interface SwapRootProviderProps extends HTMLProps<'span'>, SwapRootProviderBaseProps {}

export component SwapRootProvider(props: MaybeTracked<SwapRootProviderProps>) {
  const [swapApi, children, localProps] = trackSplit(props, ['value', 'children']);

  SwapApiContext.set(swapApi);

  let mergedProps = track(() => mergeProps(@swapApi.getRootProps(), @localProps));

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