import { Button as ButtonRAC } from 'react-aria-components'

import { variants } from './variants'

import type { ButtonProps } from './types'
import { Spinner } from '../Spinner'
import styles from './button.module.css'

export function Button({
  variant,
  color,
  size,
  wrapping,

  typography,
  className,
  children,
  isPending,
  ...props
}: ButtonProps) {
  const classNames = variants({
    variant,
    color,
    size,
    wrapping,
    typography,
    className,
  })

  return (
    <ButtonRAC {...props} className={classNames} isPending={isPending}>
      {({ isPending }) => {
        return (
          <>
            {children}
            {isPending && (
              <div className={styles.spinnerWrapper}>
                <Spinner size="Small" color="CurrentColor" />
              </div>
            )}
          </>
        )
      }}
    </ButtonRAC>
  )
}
