// src/hooks/useActivitiesHistory.ts
import { useState, useCallback } from "react";
import dayjs from "dayjs";

// src/types/common.ts
var mapApiStatusToDisplay = (apiStatus) => {
  const statusMap = {
    ["A_VENCER" /* A_VENCER */]: "ATIVA" /* ATIVA */,
    ["VENCIDA" /* VENCIDA */]: "VENCIDA" /* VENCIDA */,
    ["CONCLUIDA" /* CONCLUIDA */]: "CONCLU\xCDDA" /* CONCLUIDA */
  };
  return statusMap[apiStatus];
};

// src/utils/filterHelpers.ts
var mergeFilterOptions = (base, extra) => {
  if (extra.length === 0) return base;
  const baseIds = new Set(base.map((item) => item.id));
  const hasNew = extra.some((item) => !baseIds.has(item.id));
  if (!hasNew) return base;
  const map = new Map(base.map((item) => [item.id, item.name]));
  extra.forEach((item) => {
    if (!map.has(item.id)) map.set(item.id, item.name);
  });
  return Array.from(map.entries()).map(([id, name]) => ({ id, name })).sort((a, b) => a.name.localeCompare(b.name, "pt-BR"));
};

// src/hooks/useActivitiesHistory.ts
var DEFAULT_ACTIVITIES_PAGINATION = {
  total: 0,
  page: 1,
  limit: 10,
  totalPages: 0
};
var DEFAULT_ACTIVITY_FILTER_OPTIONS = {
  schools: [],
  classes: [],
  subjects: [],
  schoolYears: []
};
var transformActivityToTableItem = (activity) => {
  return {
    id: activity.id,
    startDate: activity.startDate ? dayjs(activity.startDate).format("DD/MM") : "-",
    deadline: activity.finalDate ? dayjs(activity.finalDate).format("DD/MM") : "-",
    creator: activity.creator?.name?.trim() ? activity.creator.name : "-",
    creatorId: activity.creator?.id ?? null,
    title: activity.title,
    school: activity.schoolName || "-",
    year: activity.year || "-",
    subject: activity.subjectName || "-",
    class: activity.className || "-",
    status: mapApiStatusToDisplay(activity.status),
    completionPercentage: activity.completionPercentage
  };
};
var extractActivityFilterOptions = (activities) => {
  const schoolsMap = /* @__PURE__ */ new Map();
  const classesMap = /* @__PURE__ */ new Map();
  const subjectsMap = /* @__PURE__ */ new Map();
  const schoolYearsMap = /* @__PURE__ */ new Map();
  for (const activity of activities) {
    if (activity.schoolId && activity.schoolName) {
      schoolsMap.set(activity.schoolId, activity.schoolName);
    }
    if (activity.subjectId && activity.subjectName) {
      subjectsMap.set(activity.subjectId, activity.subjectName);
    }
    if (activity.className) {
      classesMap.set(activity.className, activity.className);
    }
    if (activity.year) {
      schoolYearsMap.set(activity.year, activity.year);
    }
  }
  const toOptions = (map) => Array.from(map.entries()).map(([id, name]) => ({ id, name })).sort((a, b) => a.name.localeCompare(b.name, "pt-BR"));
  return {
    schools: toOptions(schoolsMap),
    classes: toOptions(classesMap),
    subjects: toOptions(subjectsMap),
    schoolYears: toOptions(schoolYearsMap)
  };
};
var buildQueryParams = (filters, activityCategory) => {
  const params = {};
  if (activityCategory) {
    params.type = activityCategory;
  }
  if (filters) {
    for (const key in filters) {
      const value = filters[key];
      if (value !== void 0 && value !== null) {
        params[key] = value;
      }
    }
  }
  return params;
};
var useActivitiesHistoryImpl = (apiClient, options) => {
  const [state, setState] = useState({
    activities: [],
    loading: false,
    error: null,
    pagination: DEFAULT_ACTIVITIES_PAGINATION,
    apiFilterOptions: DEFAULT_ACTIVITY_FILTER_OPTIONS
  });
  const fetchActivities = useCallback(
    async (filters) => {
      setState((prev) => ({ ...prev, loading: true, error: null }));
      try {
        const params = buildQueryParams(filters, options?.activityCategory);
        const response = await apiClient.get(
          "/activities/history",
          { params }
        );
        const { data } = response.data;
        const tableItems = data.activities.map(transformActivityToTableItem);
        const extracted = extractActivityFilterOptions(data.activities);
        setState((prev) => ({
          activities: tableItems,
          loading: false,
          error: null,
          pagination: data.pagination,
          apiFilterOptions: {
            schools: mergeFilterOptions(
              prev.apiFilterOptions.schools,
              extracted.schools
            ),
            classes: mergeFilterOptions(
              prev.apiFilterOptions.classes,
              extracted.classes
            ),
            subjects: mergeFilterOptions(
              prev.apiFilterOptions.subjects,
              extracted.subjects
            ),
            schoolYears: mergeFilterOptions(
              prev.apiFilterOptions.schoolYears,
              extracted.schoolYears
            )
          }
        }));
      } catch (error) {
        console.error("Erro ao carregar hist\xF3rico:", error);
        setState((prev) => ({
          ...prev,
          loading: false,
          error: "Erro ao carregar hist\xF3rico de atividades"
        }));
      }
    },
    [apiClient, options?.activityCategory]
  );
  return { ...state, fetchActivities };
};
var createUseActivitiesHistory = (apiClient, options) => {
  return () => useActivitiesHistoryImpl(apiClient, options);
};
var createActivitiesHistoryHook = createUseActivitiesHistory;
export {
  DEFAULT_ACTIVITIES_PAGINATION,
  DEFAULT_ACTIVITY_FILTER_OPTIONS,
  createActivitiesHistoryHook,
  createUseActivitiesHistory,
  extractActivityFilterOptions,
  transformActivityToTableItem
};
//# sourceMappingURL=index.mjs.map