import { ButtonHTMLAttributes, FC, memo, ReactNode } from 'react'
import MoonLoader from "react-spinners/MoonLoader"
import { XOR } from '../helpers/types'
import { clx } from '../helpers/utils'

type Props = {
    className?: string;
    isLoading?: boolean,
    percentage?: number,
} & ButtonHTMLAttributes<HTMLButtonElement> &
    XOR<{ children: ReactNode }, { label: string }>;

const Button: FC<Props> = memo((props: Props) => {

    const buttonClass = clx(
        'flex rounded-xl items-center justify-center text-center h-10 text-sm  bg-primary text-white w-full',
        props.disabled && 'cursor-not-allowed opacity-60',
        props.className
    );

    return (
        <button {...props} className={`${buttonClass} ${props.className}`} disabled={props.disabled || props.isLoading}>
            {
                props.isLoading ?
                    <div className='flex gap-2 items-center'>
                        {
                            !props.percentage ?
                                <MoonLoader size={20} color='#fff' />
                                :
                                <span>{props.percentage}%</span>
                        }
                    </div>
                    :
                    props.label || props.children
            }
        </button>
    )
})

export default Button