import { useCallback } from 'react';

/**
 * Custom hook for managing character count display and updates
 * @param {Function} formatMessage - i18n format message function
 * @param {Object} messages - Message definitions
 * @returns {Object} Character count utilities
 */
export const useCharacterCount = (formatMessage, messages) => {
  /**
   * Updates the character count display via ref
   * @param {Object} labelRef - React ref to the label element
   * @param {number} currentLength - Current character count
   * @param {number} maxLength - Maximum allowed characters
   */
  const updateCharacterCount = useCallback(
    (labelRef, currentLength, maxLength) => {
      if (labelRef?.current) {
        labelRef.current.textContent = formatMessage(messages.characterCount, {
          currentLength,
          maxLength,
        });
      }
    },
    [formatMessage, messages],
  );

  return { updateCharacterCount };
};

