const baseCommand = "node";
const suiteEntry = "tests/assets/suite/runner.js";
const pipelineMapping = [
  {
    id: 0,
    pipeline: [{ name: "primary" }, { name: "secondary", delay: 5000 }],
  },
];

const buildCommon = (jobRequest) => {
  const { testName } = jobRequest;
  if (!testName) throw Error("'testName' must be provided!");
  const command = [baseCommand, suiteEntry, `test=${testName}`];

  // Apply Common Flags
  command.push("isRetry=false");

  // Return new request
  return { ...jobRequest, command };
};

const buildSingle = (jobReq) => jobReq;

const buildMarker = (jobReq) => {};

const buildProject = (jobReq) => {};

const pipelineMaxLife = (testName) => {
  const pipelines = pipelineMapping
    .filter((m) => m.pipeline.find((t) => t.name === testName))
    .map((m) => m.pipeline);
  return Math.max(pipelines.map((p) => p.length)) + 1;
};

const buildCompound = (jobReq, socketId) => {
  const { testName, command } = jobReq;
  const pipelineTriggers = jobReq.pipelineTriggers;
  if (pipelineTriggers) command.push(`pipelineTriggers=${pipelineTriggers}`);
  command.push(`pipelineDashboardSocket=${socketId}`);
  return { ...jobReq, command };
};

function nextCompound(previousTest) {}

export default function jobBuilder(jobRequest, id) {
  const jobReq = buildCommon(jobRequest, id);
  switch (jobRequest.type) {
    case "single":
      return buildSingle(jobReq);
    case "marker":
      return buildMarker(jobReq);
    case "project":
      return buildProject(jobReq);
    case "compound":
      return buildCompound(jobReq, id);
    default:
      throw Error("No Job Request Type Specified!");
  }
}
