export function mergeThreadPage(current, incoming) {
  const byId = new Map(current.map((thread) => [threadKey(thread), thread]));
  for (const thread of incoming) byId.set(threadKey(thread), thread);
  return [...byId.values()].sort((left, right) =>
    String(right.updatedAt ?? "").localeCompare(String(left.updatedAt ?? ""))
  );
}

function threadKey(thread) {
  return `${thread.ownerId ?? ""}:${thread.id}`;
}

export function threadsForAccount(previousAccountId, nextAccountId, threads) {
  return previousAccountId === nextAccountId ? threads : [];
}

export function threadTitle(thread) {
  return thread.name?.trim() || thread.preview?.trim() || "New chat";
}
