export function conversationDisplayTitle({ session, transition, turns = [], fallback = "New chat" } = {}) {
  return cleanTitle(session?.conversationTitle)
    ?? cleanTitle(transition?.label)
    ?? firstUserTitle(turns)
    ?? fallback;
}

function firstUserTitle(turns) {
  for (const turn of turns) {
    for (const message of turn?.userMessages ?? []) {
      const title = cleanTitle(message?.text);
      if (title) return title;
    }
    const title = cleanTitle(turn?.userText);
    if (title) return title;
  }
  return undefined;
}

function cleanTitle(value) {
  return typeof value === "string" && value.trim() ? value.trim() : undefined;
}
