export function maskNotePreviewByWord(note: string) {
  const trimmed = note.trim();
  if (!trimmed) {
    return '요청 내용이 마스킹되었습니다.';
  }

  return note
    .split(/(\s+)/)
    .map((segment) => {
      if (!segment || /\s+/.test(segment)) {
        return segment;
      }

      if (segment.length === 1) {
        return '*';
      }

      const visiblePrefixLength = segment.length >= 4 ? 1 : 0;
      const visibleSuffixLength = segment.length >= 6 ? 1 : 0;
      const maxMaskLength = Math.max(1, segment.length - visiblePrefixLength - visibleSuffixLength);
      const maskLength = Math.min(maxMaskLength, Math.max(1, Math.ceil(segment.length * 0.4)));
      const maskStart = Math.max(
        visiblePrefixLength,
        Math.floor((segment.length - maskLength) / 2),
      );
      const maskEnd = Math.min(segment.length - visibleSuffixLength, maskStart + maskLength);

      return `${segment.slice(0, maskStart)}${'*'.repeat(maskEnd - maskStart)}${segment.slice(maskEnd)}`;
    })
    .join('');
}
