/** Pick the wizard dropdown option that best matches an engine-detected label. */
export function matchWizardOption(detected: string | null | undefined, options: string[]): string {
  if (!detected?.trim() || !options.length) return '';
  const d = detected.trim().toLowerCase();
  const exact = options.find((o) => o.toLowerCase() === d);
  if (exact) return exact;
  const contains = options.find(
    (o) => o.toLowerCase().includes(d) || d.includes(o.toLowerCase().split(/\s+/)[0] ?? ''),
  );
  if (contains) return contains;
  const word = d.split(/\s+/)[0];
  const prefix = options.find((o) => o.toLowerCase().startsWith(word));
  return prefix ?? '';
}
