import { operatingCarrier, type ISimpleFlight } from "../../../types.js";
import type { AirlineConfig } from "../airlines.js";

/**
 * Registration button is visible when:
 * - the operating carrier has a registrationUrl
 * - the first leg's registration transition status is "InProgress"
 */
export function canRegister(
  flight: ISimpleFlight,
  airlineConfig: Record<string, AirlineConfig>,
): boolean {
  const carrier = operatingCarrier(flight.operatingBy);
  if (!carrier) return false;
  const config = airlineConfig[carrier];
  if (!config?.registrationUrl) return false;

  const leg = flight.routeType === "Direct" ? flight.leg : flight.legs[0];
  if (!leg) return false;

  return leg.transition?.registration?.status === "InProgress";
}
