import type {
  LegacyRecord,
  LegacySummaryEntry,
  LegacyTalentChoice,
  LegacyTrajectoryStep,
} from "../legacy/interop.js";
import type { LegacySessionActionSetters } from "./legacySessionActionTypes";
import type { AllocationState } from "./legacySessionTypes";
import { createEmptyRecord, createRecordSnapshot } from "./legacySessionUtils";

export function transitionLegacySessionToDraft(
  setters: LegacySessionActionSetters,
  availableTalents: LegacyTalentChoice[],
): void {
  setters.setAvailableTalents(availableTalents);
  setters.setSelectedTalentIds([]);
  setters.setExtendTalentId(null);
  setters.setAllocation(null);
  setters.setTrajectory([]);
  setters.setSummary([]);
  setters.setCurrentRecord(createEmptyRecord());
  setters.setStage("talent");
  setters.setAutoPlaySpeed(null);
}

export function transitionLegacySessionToProperty(
  setters: Pick<LegacySessionActionSetters, "setAllocation" | "setStage">,
  allocation: AllocationState,
): void {
  setters.setAllocation(allocation);
  setters.setStage("property");
}

export function transitionLegacySessionToTrajectory(
  setters: Pick<
    LegacySessionActionSetters,
    | "setAutoPlaySpeed"
    | "setCurrentRecord"
    | "setStage"
    | "setSummary"
    | "setTrajectory"
  >,
  record: LegacyRecord,
): void {
  setters.setStage("trajectory");
  setters.setTrajectory([]);
  setters.setCurrentRecord(createRecordSnapshot(record));
  setters.setSummary([]);
  setters.setAutoPlaySpeed(null);
}

export function appendLegacyTrajectoryStep(
  setters: Pick<LegacySessionActionSetters, "setCurrentRecord" | "setTrajectory">,
  step: LegacyTrajectoryStep,
  record: LegacyRecord,
): void {
  setters.setTrajectory((current) => [...current, step]);
  setters.setCurrentRecord(createRecordSnapshot(record));
}

export function transitionLegacySessionToSummary(
  setters: Pick<
    LegacySessionActionSetters,
    | "setAutoPlaySpeed"
    | "setExtendTalentId"
    | "setStage"
    | "setSummary"
  >,
  summary: LegacySummaryEntry[],
  extendTalentId: number | null,
): void {
  setters.setSummary(summary);
  setters.setExtendTalentId(extendTalentId);
  setters.setStage("summary");
  setters.setAutoPlaySpeed(null);
}
