import type { ConfigVersionSummary } from "@/types/api/admin-config";

/** Prefer newest draft, then active, otherwise highest numeric id — matches config doc UX. */
export function pickDefaultConfigVersionId(list: ConfigVersionSummary[]): string | null {
  if (list.length === 0) {
    return null;
  }
  const drafts = list.filter((x) => x.status === "draft").sort((a, b) => b.id - a.id);
  const active = list.find((x) => x.status === "active");
  const pick = drafts[0] ?? active ?? [...list].sort((a, b) => b.id - a.id)[0];
  return pick ? String(pick.id) : null;
}
