import type { LogEntry } from "@codeflash/types";
import type { State } from "../root";
import { isTaskRunning } from "@codeflash/shared";

// adds a new log entry to the active task
export const handleAddLog = (state: State, log: LogEntry): State => {
  const activeTask = state.queueTasks.find(isTaskRunning);
  if (!activeTask) {
    return state;
  }

  return {
    ...state,
    queueTasks: state.queueTasks.map((t) => {
      if (t.id === activeTask.id) {
        return {
          ...t,
          logs: [...t.logs, log],
        };
      }
      return t;
    }),
  };
};
