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

interface RootProviderProps {
  value: UsePasswordInputReturn;
}

export interface PasswordInputRootProviderBaseProps extends RootProviderProps, PolymorphicProps<
  'div'
> {}
export interface PasswordInputRootProviderProps extends HTMLProps<
  'div'
>, PasswordInputRootProviderBaseProps {}

export component PasswordInputRootProvider(props: MaybeTracked<PasswordInputRootProviderProps>) {
  const [children, value, localProps] = trackSplit(props, ['children', 'value']);
  let mergedProps = track(() => mergeProps(@value.getRootProps(), @localProps));

  PasswordInputApiContext.set(value);

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