import type { JobRun } from "../api/haixun";

export const ACTIVE_JOB_STATUSES = new Set([
  "pending",
  "queued",
  "running",
  "waiting_worker",
  "cancel_requested"
]);

// Statuses where the backend still accepts a new cancel request.
// "cancel_requested" is intentionally excluded: the backend has already
// accepted the cancel and calling it again always fails with
// "job cannot be cancelled in current status", which surfaced as jobs
// that "can't be cancelled" once the user clicked cancel once already.
const CANCELLABLE_JOB_STATUSES = new Set(["pending", "queued", "running", "waiting_worker"]);

export const RETRYABLE_JOB_STATUSES = new Set(["failed", "cancelled", "expired"]);

export function canCancelJob(job: JobRun) {
  return CANCELLABLE_JOB_STATUSES.has(job.status);
}

export function canRetryJob(job: JobRun) {
  return RETRYABLE_JOB_STATUSES.has(job.status);
}