/** Matches rows in `swi_document_status` (see sql/swi.sql). */
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 == null || id === 0;
}

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;
}

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