import React, { CSSProperties, useContext, useCallback } from 'react';
import { ProductContext } from './ProductCard';

import styles from '../styles/styles.module.css';

export interface Props {
    className?: string;
    style?: CSSProperties;
}

export const ProductButtons = ( { className, style }: Props ) => {

    // TODO: macCount
    const { increaseBy, counter, maxCount } = useContext( ProductContext );

    // TODO: isMaxReached = useCallback, [ count, maxCounter ]
    // TRUE si el count === maxCounter
    // FALSE si el count !== maxCounter
    const isMaxReached = useCallback(
        () => !!maxCount && counter === maxCount,
        [counter, maxCount],
    );

    return (
        <div
            className={ `${ styles.buttonsContainer} ${ className }` }
            style={ style }
        >
            <button
                onClick={ () => increaseBy(-1) }
                className={ styles.buttonMinus }>-</button>
            <button className={ styles.countLabel }>{ counter }</button>
            <button
                onClick={ () => increaseBy(1) }
                className={ `${ styles.buttonAdd } ${ isMaxReached() && styles.disabled }` }>+</button>
        </div>
    );
}