"use client"

import { Slot } from "@radix-ui/react-slot"
import { Button as ButtonRAC } from "react-aria-components"

import { buttonVariants } from "./variants"

import type { ButtonProps } from "./button"

export default function Button(props: ButtonProps) {
  const {
    className,
    clean,
    intent,
    size,
    theme,
    fullWidth,
    wrapping,
    variant,
    ...restProps
  } = props

  const classNames = buttonVariants({
    className,
    clean,
    intent,
    size,
    theme,
    fullWidth,
    wrapping,
    variant,
  })

  if (restProps.asChild) {
    const { asChild, ...slotProps } = restProps
    return <Slot className={classNames} {...slotProps} />
  }

  const { asChild, onClick, disabled, ...racProps } = restProps
  return (
    <ButtonRAC
      className={classNames}
      isDisabled={disabled}
      onPress={onClick}
      {...racProps}
    />
  )
}
