"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/hooks/useActivitiesHistory.ts
var useActivitiesHistory_exports = {};
__export(useActivitiesHistory_exports, {
  DEFAULT_ACTIVITIES_PAGINATION: () => DEFAULT_ACTIVITIES_PAGINATION,
  DEFAULT_ACTIVITY_FILTER_OPTIONS: () => DEFAULT_ACTIVITY_FILTER_OPTIONS,
  createActivitiesHistoryHook: () => createActivitiesHistoryHook,
  createUseActivitiesHistory: () => createUseActivitiesHistory,
  extractActivityFilterOptions: () => extractActivityFilterOptions,
  transformActivityToTableItem: () => transformActivityToTableItem
});
module.exports = __toCommonJS(useActivitiesHistory_exports);
var import_react = require("react");
var import_dayjs = __toESM(require("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 ? (0, import_dayjs.default)(activity.startDate).format("DD/MM") : "-",
    deadline: activity.finalDate ? (0, import_dayjs.default)(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] = (0, import_react.useState)({
    activities: [],
    loading: false,
    error: null,
    pagination: DEFAULT_ACTIVITIES_PAGINATION,
    apiFilterOptions: DEFAULT_ACTIVITY_FILTER_OPTIONS
  });
  const fetchActivities = (0, import_react.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;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DEFAULT_ACTIVITIES_PAGINATION,
  DEFAULT_ACTIVITY_FILTER_OPTIONS,
  createActivitiesHistoryHook,
  createUseActivitiesHistory,
  extractActivityFilterOptions,
  transformActivityToTableItem
});
//# sourceMappingURL=index.js.map