'use client';

import { bool, string } from 'prop-types';
import { RippleArea } from '../ripple/ripple-area';
import { ButtonLayoutProps } from './button-layout.types';
import useRippleEffect from '../ripple/hooks/useRippleEffect';
import React, { forwardRef, useId, useRef, useState } from 'react';

export const ButtonLayout = forwardRef<HTMLButtonElement, ButtonLayoutProps>(
    ({ centralRipple = false, variant, ...props }, ref) => {
        const [isActive, setIsActive] = useState<boolean>(false),
            ripplesRef = useRef(null),
            buttonId = useId(),
            events = useRippleEffect(ripplesRef, setIsActive);

        const classes = props.className
            ? `m3 ${props.className} ${variant}${isActive ? ' is-active' : ''}`
            : `m3 ${variant}${isActive ? ' is-active' : ''}`;

        return (
            <button
                {...props}
                {...events}
                className={classes}
                disabled={props.disabled}
                id={buttonId}
                ref={ref}
            >
                {props.children}
                <RippleArea
                    callback={setIsActive}
                    central={centralRipple}
                    ref={ripplesRef}
                />
            </button>
        );
    },
);

ButtonLayout.propTypes = {
    variant: string,
    centralRipple: bool,
    children: string,
};
