import type { LearnServeFormValues } from "./learnServeFormTypes";
import type { LearnServeOptions } from "./useLearnServeOptions";

/** What the current values mean for which fields apply. */
export interface LearnServeValueFlags {
  /** The two types that carry a certificate. */
  isInternshipOrCourse: boolean;
  /** No physical location to ask for. */
  isOnline: boolean;
  isInPerson: boolean;
  /** This account may charge *and* this type is chargeable. */
  canBePaid: boolean;
  isPaid: boolean;
}

/**
 * Derives the handful of booleans the field sections switch on.
 *
 * They are all comparisons of the current selection against a master-choice id,
 * which is why they cannot live in the option layer: the ids are stable, the
 * selection is not. Gathered here so the rules are stated once and the sections
 * stay presentation.
 */
export function learnServeValueFlags(
  values: LearnServeFormValues,
  options: Pick<
    LearnServeOptions,
    "internshipTypeId" | "courseTypeId" | "onlineFormatId" | "inPersonFormatId"
  >,
  developmentPaid: boolean
): LearnServeValueFlags {
  const { internshipTypeId, courseTypeId, onlineFormatId, inPersonFormatId } =
    options;

  const isInternship =
    !!internshipTypeId && values.learningType === internshipTypeId;

  // An internship is always free, whatever the account is allowed to charge.
  const canBePaid = developmentPaid && !isInternship;

  return {
    isInternshipOrCourse:
      isInternship ||
      (!!courseTypeId && values.learningType === courseTypeId),
    isOnline: !!onlineFormatId && values.learnServeFormat === onlineFormatId,
    isInPerson: !!inPersonFormatId && values.learnServeFormat === inPersonFormatId,
    canBePaid,
    isPaid: canBePaid && values.is_paid === "true",
  };
}
