import { PreparedOp, Task } from '../../../types';

export const splitTasks = (awaitedTasks: Task[], isFullyExecuted: boolean) => {
  if (awaitedTasks.length === 0) {
    return { inThreadOps: [], outThreadOps: [] };
  }

  const inThreadOps: PreparedOp[] = [];
  let outThreadOps: PreparedOp[] = [];

  if (!isFullyExecuted) {
    const latestTaskBatchId = awaitedTasks.at(-1)!.batchId ?? -1;

    awaitedTasks.forEach((task) => {
      if (task.batchId === latestTaskBatchId) {
        inThreadOps.push(task.op);
      } else {
        outThreadOps.push(task.op);
      }
    });
  } else {
    outThreadOps = awaitedTasks.map((task) => task.op);
  }

  return { inThreadOps, outThreadOps };
};
