import type { JobStatus } from "./types";

export const STATUSES: JobStatus[] = ["queued", "running", "succeeded", "failed"];

export function parseJson(value: string): Record<string, unknown> {
  if (!value.trim()) return {};
  const parsed = JSON.parse(value) as unknown;
  if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
    return parsed as Record<string, unknown>;
  }
  throw new Error("JSON payload must be an object.");
}

export function formatDateTimeParts(value: string | null) {
  if (!value) return null;
  const date = new Date(value);
  if (Number.isNaN(date.getTime())) return null;
  return {
    date: date.toLocaleDateString(),
    time: date.toLocaleTimeString(),
    full: date.toLocaleString()
  };
}

export function eventTitle(type: string) {
  return type
    .split("_")
    .map((part) => part.slice(0, 1).toUpperCase() + part.slice(1))
    .join(" ");
}
