import {
  DOCUMENT_STATUS_CHART_ITEMS,
  countDocumentsByStatus,
} from "./documentStatusStats.js";
import { SWI_DOCUMENT_STATUS } from "../constants/swiDocumentStatus.js";

export function userSwiStatusSeries(documents = []) {
  const counts = countDocumentsByStatus(documents);
  return DOCUMENT_STATUS_CHART_ITEMS.map((item) => ({
    ...item,
    value: counts[item.statusId] ?? 0,
  }));
}

export function userSwiSummary(documents = []) {
  const counts = countDocumentsByStatus(documents);
  const total = documents.length;
  const inProgress = counts[SWI_DOCUMENT_STATUS.IN_PROGRESS] ?? 0;
  const completed = counts[SWI_DOCUMENT_STATUS.COMPLETED] ?? 0;
  const pending =
    (counts[SWI_DOCUMENT_STATUS.OPENED] ?? 0) +
    (counts[SWI_DOCUMENT_STATUS.ASSIGNED] ?? 0) +
    inProgress;

  return { total, pending, inProgress, completed, counts };
}

export function filterDocumentsByStatus(documents, statusId) {
  if (statusId == null || statusId === "" || statusId === "all") {
    return documents;
  }
  const id = Number(statusId);
  return documents.filter((doc) => Number(doc.document_status_id) === id);
}
