import { ReactNode } from 'react';

/**
 * Helper to extract string from ReactNode for announcements.
 * Complex ReactNodes (JSX elements) return empty string.
 */
export const getStringValue = (node: ReactNode): string => {
  if (typeof node === 'string' || typeof node === 'number') {
    return String(node);
  }
  return '';
};

/**
 * Builds the announcement string from visible content only.
 * Content visibility depends on expanded state and presence of title.
 */
export const buildAnnouncementString = ({
  title,
  description,
  expanded,
  actionLabel,
  actionSecondaryLabel,
}: {
  title?: ReactNode;
  description?: ReactNode;
  expanded: boolean;
  actionLabel?: ReactNode;
  actionSecondaryLabel?: ReactNode;
}): string => {
  return [
    title ? getStringValue(title) : undefined,
    description && (expanded || !title) ? getStringValue(description) : undefined,
    expanded && actionLabel ? getStringValue(actionLabel) : undefined,
    expanded && actionSecondaryLabel ? getStringValue(actionSecondaryLabel) : undefined,
  ]
    .filter(Boolean)
    .join('|');
};
