/**
 * Normalize Form  Item List Titles
 * @param value
 * @returns {*|string}
 */
const normalizeFormListTitleValue = (value) => {
  if (value === null || value === undefined) return "";

  if (Array.isArray(value)) {
    return value
      .map((item) => normalizeFormListTitleValue(item))
      .filter(Boolean)
      .join(", ");
  }

  return String(value).trim();
};

/**
 * Get Form Listem Item Title
 * @param fallbackLabel
 * @param index
 * @param candidates
 * @returns {*|string}
 */
export function getFormListItemTitle(fallbackLabel, index, ...candidates) {
  const title = candidates.map((candidate) => normalizeFormListTitleValue(candidate)).find(Boolean);

  return title || `${fallbackLabel} ${index + 1}`;
}
