import { helper } from '@ember/component/helper';

const MAX_INITIALS = 2;
function toInitials(name, initialsLength, initials) {
  // Calculate the number of initials to show, maxing out at MAX_INITIALS
  let calculatedInitialsLength = (initials ? initials?.split(' ')?.length : name?.split(' ')?.length) || 1;
  calculatedInitialsLength = calculatedInitialsLength > MAX_INITIALS ? MAX_INITIALS : calculatedInitialsLength;

  // Check if initialsLength was passed and set to calculated, unless greater than MAX_INITIALS
  if (initialsLength) {
    calculatedInitialsLength = initialsLength <= MAX_INITIALS ? initialsLength : MAX_INITIALS;
  }
  let calculatedInitials;

  // A. Set to initials prop if exists (but truncate to 2 characters max unless length is supplied)
  if (initials) {
    calculatedInitials = initials.substring(0, calculatedInitialsLength);
  } else {
    if (name.trim() && name.split(' ').length > 1) {
      // B. If there are any spaces in the name, set to first letter of each word
      calculatedInitials = name.match(/\b(\w)/g);
      calculatedInitials = calculatedInitials && calculatedInitials.join('').substring(0, calculatedInitialsLength);
    } else {
      // C. Set to the name's initials truncated based on calculated length
      calculatedInitials = name.substring(0, calculatedInitialsLength);
    }
  }
  return calculatedInitials;
}
var toInitials$1 = helper(function ([name, initialsLength, initials]) {
  if (name === undefined && initials === undefined) {
    return '';
  }
  return toInitials(name, initialsLength, initials);
});

export { MAX_INITIALS, toInitials$1 as default, toInitials };
//# sourceMappingURL=to-initials.js.map
