import { useCallback } from 'react';

const useAbbreviation = () => {
  const createAbbreviation = useCallback((text) => {
    if (!text || text.length <= 20) return text;

    const words = text.split(' ');
    if (words.length > 1) {
      return words.map((word) => word.charAt(0).toUpperCase()).join('');
    } else {
      const matches = text.match(/[А-ЯA-Z][а-яa-z]*/g);
      if (matches && matches.length > 1) {
        return matches
          .map((m) => m.charAt(0).toUpperCase())
          .join('')
          .slice(0, 5);
      }
      return text.substring(0, 3).toUpperCase();
    }
  }, []);

  const shouldAbbreviate = useCallback((text) => {
    return text && text.length > 20;
  }, []);

  return {
    createAbbreviation,
    shouldAbbreviate,
  };
};

export default useAbbreviation;
