import { ResolvedOp } from '../../types';

const resolvedOps: ResolvedOp[] = [];

let usedOps: ResolvedOp[] = [];
let isSavingUsed = false;

export const pushResolvedOp = (op: ResolvedOp | ResolvedOp[]) => {
  if (Array.isArray(op)) {
    resolvedOps.push(...op);
  } else {
    resolvedOps.push(op);
  }
};

export const shiftResolvedOp = () => {
  const result = {
    isOp: resolvedOps.length > 0,
    op: resolvedOps.shift(),
  } as
    | {
        isOp: true;
        op: ResolvedOp | null;
      }
    | {
        isOp: false;
        op: undefined;
      };

  if (isSavingUsed && result.op) {
    usedOps.push(result.op);
  }

  return result;
};

export const getUsedOps = () => usedOps;

export const saveUsedOps = () => {
  usedOps = [];
  isSavingUsed = true;
};
