import { useState } from 'react';

export const useQuantityControl = (
  initialQuantity: number = 1,
  min: number = 1,
  max: number = 99
) => {
  const [quantity, setQuantity] = useState(initialQuantity);

  const handleQuantityChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const value = parseInt(e.target.value) || min;
    setQuantity(Math.min(Math.max(min, value), max));
  };

  const handleBlur = () => {
    if (quantity < min) setQuantity(min);
    if (quantity > max) setQuantity(max);
  };

  const incrementQuantity = () => {
    setQuantity(prev => Math.min(prev + 1, max));
  };

  const decrementQuantity = () => {
    setQuantity(prev => Math.max(min, prev - 1));
  };

  const resetQuantity = () => {
    setQuantity(initialQuantity);
  };

  return {
    quantity,
    setQuantity,
    handleQuantityChange,
    handleBlur,
    incrementQuantity,
    decrementQuantity,
    resetQuantity,
  };
};
