/** A master choice as `/choices/` returns it. */
/** Re-exported so existing importers keep working off one definition. */
export type { ChoiceItem } from "@/lib/choices";
import type { ChoiceItem } from "@/lib/choices";

export interface TypeOption {
  label: string;
  value: string;
  value_en: string;
}

/** A react-select group heading with its own options under it. */
export interface TypeOptionGroup {
  label: string;
  options: TypeOption[];
}

/**
 * The order the client asked for, which is not alphabetical and not the API's.
 */
const TYPE_ORDER = [
  "Volunteer",
  "Class/Workshop",
  "Course",
  "Consultation",
  "Internship",
];

/** Development-only lists filter by `learning_type`; mixed lists by `filter-type`. */
export function developmentTypeCatalog({
  showVolunteerFields,
  showLearnServeFields,
}: {
  showVolunteerFields?: boolean;
  showLearnServeFields?: boolean;
}): "learning_type" | "filter-type" {
  return !showVolunteerFields && showLearnServeFields ? "learning_type" : "filter-type";
}

/** Ordered flat options; volunteering is hidden on development-only lists. */
export function buildOpportunityTypeOptions({
  choices,
  selectedLanguage,
  showVolunteerFields,
  showLearnServeFields,
}: {
  choices: ChoiceItem[] | undefined;
  selectedLanguage: string;
  showVolunteerFields?: boolean;
  showLearnServeFields?: boolean;
}): Array<TypeOption | TypeOptionGroup> {
  const raw: TypeOption[] = (choices ?? [])
    .map((item) => ({
      label: selectedLanguage === "ar" ? item.value_ar : item.value_en,
      value: String(item.id),
      value_en: item.value_en,
    }))
    .sort(
      (a, b) => TYPE_ORDER.indexOf(a.value_en) - TYPE_ORDER.indexOf(b.value_en)
    );

  const visible =
    !showVolunteerFields && showLearnServeFields
      ? raw.filter((option) => option.value_en !== "Volunteer")
      : raw;

  return visible;
}

/** `filter-type` names (mixed lists) that are a development `learning_type`. */
const FILTER_TYPE_TO_LEARNING_TYPE: Record<string, string> = {
  Course: "Course",
  Class: "Class/Workshop",
  "Class/Workshop": "Class/Workshop",
  Consultation: "Consultation",
  Internship: "Internship",
};

/**
 * The `learning_type` id behind a mixed list's `filter-type` choice. The
 * development endpoint ignores `type` unless it is «Volunteer»
 * (`LearnServeOpportunityController::index`), so the development section of a
 * mixed list must send `learning_type_id` to filter at all.
 */
export function learningTypeIdForFilterType(
  filterTypeId: string | number | undefined | null,
  filterTypes: ChoiceItem[] | undefined,
  learningTypes: ChoiceItem[] | undefined
): string | undefined {
  if (filterTypeId == null || filterTypeId === "") return undefined;
  const chosen = filterTypes?.find((item) => String(item.id) === String(filterTypeId));
  const learningName = chosen ? FILTER_TYPE_TO_LEARNING_TYPE[chosen.value_en] : undefined;
  if (!learningName) return undefined;
  const match = learningTypes?.find((item) => item.value_en === learningName);
  return match ? String(match.id) : undefined;
}
