import {
  canComplete,
  isCompleted,
} from "../constants/swiDocumentStatus.js";

export function assignmentKey(row) {
  return `${row.document_id}-${row.document_number}`;
}

export function formatAssignmentDate(value) {
  if (!value) return "—";
  const d = new Date(value);
  if (Number.isNaN(d.getTime())) return String(value);
  return d.toLocaleDateString(undefined, {
    year: "numeric",
    month: "short",
    day: "numeric",
  });
}

export function formatAssignmentDateTime(value) {
  if (!value) return "—";
  const d = new Date(value);
  if (Number.isNaN(d.getTime())) return String(value);
  return d.toLocaleString(undefined, {
    year: "numeric",
    month: "short",
    day: "numeric",
    hour: "2-digit",
    minute: "2-digit",
  });
}

export function editFormButtonLabel(statusId) {
  return isCompleted(statusId) ? "Edit form" : "Fill form";
}

/** Edit is enabled only while In Progress. */
export function assignmentListEditEnabled(statusId) {
  return canComplete(statusId);
}

/** Show Edit button for In Progress (enabled) and Completed (disabled). */
export function assignmentListShowEditButton(statusId) {
  return canComplete(statusId) || isCompleted(statusId);
}

/** @param {object} row assignment row from API */
export function assignmentFeatureTags(row) {
  const tags = [];
  if (row.location_name) tags.push({ label: "Plant", value: row.location_name });
  if (row.department_name) tags.push({ label: "Work area", value: row.department_name });
  if (row.job_area_name) tags.push({ label: "Job area", value: row.job_area_name });
  if (row.process_name) tags.push({ label: "Process", value: row.process_name });
  if (row.page_count != null) {
    tags.push({
      label: "Pages",
      value: String(row.page_count),
    });
  }
  return tags;
}
