// SPEC: PJ2026-01040409 MDTODO前端重写; draft-2026-06-26-p0-mdtodo-web-rewrite.
// Responsibility: presentation-only file, status, date and reference formatting.

import type { MdtodoFileRecord } from "@/api";

/** File dropdown shows the direct docs/MDTODO/ file name, not internal titles. */
export function fileDisplayName(file: MdtodoFileRecord): string {
  const path = file.relativePath || file.fileRef || "-";
  return path.split("/").filter(Boolean).at(-1) || path;
}

export function statusLabel(value?: string | null): string {
  if (value === "completed") return "完成";
  if (value === "blocked") return "阻塞";
  if (value === "in_progress") return "进行中";
  if (value === "open") return "待办";
  return value || "未知";
}

export function sourceStatusTone(value?: string | null): string {
  if (value === "active" || value === "ok" || value === "ready") return "ok";
  if (value === "blocked" || value === "error" || value === "failed")
    return "error";
  return "pending";
}

export function displayDate(value?: string | null): string {
  if (!value) return "-";
  const date = new Date(value);
  if (Number.isNaN(date.getTime())) return value;
  return date.toLocaleString("zh-CN", {
    month: "2-digit",
    day: "2-digit",
    hour: "2-digit",
    minute: "2-digit",
  });
}

export function shortRef(value?: string | null): string {
  if (!value) return "-";
  return value.length > 42
    ? `${value.slice(0, 24)}...${value.slice(-10)}`
    : value;
}
