/** Matches rows in `swi_document_status`. */
export const SWI_DOCUMENT_STATUS = {
  OPENED: 1,
  ASSIGNED: 2,
  IN_PROGRESS: 3,
  COMPLETED: 4,
};

export function canStartWork(statusId) {
  const id = Number(statusId);
  return id === SWI_DOCUMENT_STATUS.ASSIGNED || !id;
}

export function canSaveDraft(statusId) {
  const id = Number(statusId);
  return (
    id === SWI_DOCUMENT_STATUS.IN_PROGRESS ||
    id === SWI_DOCUMENT_STATUS.ASSIGNED
  );
}

export function canComplete(statusId) {
  return Number(statusId) === SWI_DOCUMENT_STATUS.IN_PROGRESS;
}

export function isCompleted(statusId) {
  return Number(statusId) === SWI_DOCUMENT_STATUS.COMPLETED;
}

export function canEditForm(statusId) {
  return canComplete(statusId) || isCompleted(statusId);
}

export function statusBadgeClass(statusId) {
  const id = Number(statusId);
  if (id === SWI_DOCUMENT_STATUS.OPENED) return "swi-status-badge--opened";
  if (id === SWI_DOCUMENT_STATUS.ASSIGNED) return "swi-status-badge--assigned";
  if (id === SWI_DOCUMENT_STATUS.IN_PROGRESS) return "swi-status-badge--in-progress";
  if (id === SWI_DOCUMENT_STATUS.COMPLETED) return "swi-status-badge--completed";
  return "swi-status-badge--unknown";
}

/** Admin may assign only documents that are still Opened and unassigned. */
export function canAdminAssignDocument(doc) {
  if (!doc) return false;
  if (doc.assigned_to_user_id != null && doc.assigned_to_user_id !== "") {
    return false;
  }
  const statusId = Number(doc.document_status_id);
  return !statusId || statusId === SWI_DOCUMENT_STATUS.OPENED;
}
