import { useBaseTextFieldContext } from '../../form/BaseTextField/contexts/BaseTextFieldContext';
import { CommonProps } from '../../types';
import { assertEmptyObject } from '../../utils/assertEmptyObject';
import { ButtonProps } from '../Button/Button';
import { ButtonAppearance } from '../Button/constants';

import { TunedButton } from './styled';

/** Props for {@link FieldSuffixTextButton} */
export interface FieldSuffixTextButtonProps
  extends CommonProps,
    Pick<ButtonProps, 'onClick' | 'onMouseDown' | 'ariaLabel'> {
  /** Text of button */
  children: string;
}

/**
 * An text button that can be added as a suffix to fields that have the property `suffix`
 *
 * ```tsx
 * <TextField
 *   suffix={<FieldSuffixTextButton onClick={handleClick}>Reset password</FieldSuffixTextButton>}
 * />
 * ```
 */
export function FieldSuffixTextButton(props: FieldSuffixTextButtonProps) {
  const { className, children, onClick, onMouseDown, ariaLabel, testId, ariaDescribedBy, ...rest } = props;
  assertEmptyObject(rest);

  const { disabled } = useBaseTextFieldContext();

  return (
    <TunedButton
      appearance={ButtonAppearance.SubtlePrimary}
      ariaDescribedBy={ariaDescribedBy}
      ariaLabel={ariaLabel}
      className={className}
      disabled={disabled}
      onClick={onClick}
      onMouseDown={onMouseDown}
      testId={testId}
    >
      {children}
    </TunedButton>
  );
}
