import type {
  QueueTaskItem,
  RestoreStateMessage,
  Suggestion,
} from "@codeflash/types";
import type { State } from "../root";

export const handleRestoreStateFromCache = (
  oldState: State,
  message: RestoreStateMessage,
): State => {
  const payload = message.state;

  const {
    // currentStatus,
    // currentStatusMessage,
    // currentFunctionName,
    moduleRoot,
    running,
    focusedTaskId,
    queueTasks,
    configSuggestions,
    triedCodeflashDemo,
    configPath,
  } = payload as {
    running?: boolean;
    queueTasks?: QueueTaskItem[];
    focusedTaskId: string;
    moduleRoot?: string;
    configSuggestions?: Record<string, Suggestion>;
    triedCodeflashDemo?: boolean;
    configPath?: string;
  };
  // console.log(
  //   `received restore state from cache message: ${message.type}, ${JSON.stringify(payload)}`,
  // );
  return {
    ...oldState,
    optimizationRunning: running ?? false,
    triedCodeflashDemo: triedCodeflashDemo ?? false,
    queueTasks: queueTasks ?? [],
    focusedTaskId,
    moduleRoot: moduleRoot ?? "",
    projectConfigSuggestions: configSuggestions ?? {},
    configPath: configPath ?? "",
  };
};
