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

export interface PasswordInputIndicatorBaseProps extends PolymorphicProps<'span'> {
  /**
 * The fallback content to display when the password is not visible.
 */
  fallback?: any;
}
export interface PasswordInputIndicatorProps extends HTMLProps<
  'span'
>, PasswordInputIndicatorBaseProps {}

export component PasswordInputIndicator(props: MaybeTracked<PasswordInputIndicatorProps>) {
  const [children, fallback, localProps] = trackSplit(props, ['children', 'fallback']);
  const passwordInput = usePasswordInputContext();

  let mergedProps = track(() => mergeProps(@passwordInput.getIndicatorProps(), @localProps));

  <ark.span {...@mergedProps}>
    if (@passwordInput.visible) {
      <@children />
    } else {
      <@fallback />
    }
  </ark.span>
}
