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

export interface PasswordInputRootBaseProps extends PolymorphicProps<
  'div'
>, UsePasswordInputProps {}
export interface PasswordInputRootProps extends HTMLProps<'div'>, PasswordInputRootBaseProps {}

export component PasswordInputRoot(props: MaybeTracked<PasswordInputRootProps>) {
  const [children, rest] = trackSplit(props, ['children']);
  const [passwordInputProps, localProps] = splitPasswordInputProps(@rest);

  const passwordInput = usePasswordInput(passwordInputProps);
  let mergedProps = track(() => mergeProps(@passwordInput.getRootProps(), @localProps));

  PasswordInputApiContext.set(passwordInput);

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