"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/useRecommendedLessons.ts
var useRecommendedLessons_exports = {};
__export(useRecommendedLessons_exports, {
  createRecommendedLessonsHistoryHook: () => createRecommendedLessonsHistoryHook,
  createUseRecommendedLessonsHistory: () => createUseRecommendedLessonsHistory,
  determineRecommendedClassStatus: () => determineRecommendedClassStatus,
  handleRecommendedClassFetchError: () => handleRecommendedClassFetchError,
  recommendedClassHistoryApiResponseSchema: () => recommendedClassHistoryApiResponseSchema,
  transformRecommendedClassToTableItem: () => transformRecommendedClassToTableItem
});
module.exports = __toCommonJS(useRecommendedLessons_exports);
var import_react = require("react");
var import_zod = require("zod");
var import_dayjs = __toESM(require("dayjs"));
var recommendedClassSubjectSchema = import_zod.z.object({
  id: import_zod.z.string().uuid(),
  name: import_zod.z.string()
}).nullable();
var recommendedClassCreatorSchema = import_zod.z.object({
  id: import_zod.z.string().uuid(),
  name: import_zod.z.string()
}).nullable();
var recommendedClassStatsSchema = import_zod.z.object({
  totalStudents: import_zod.z.number(),
  completedCount: import_zod.z.number(),
  completionPercentage: import_zod.z.number()
});
var recommendedClassBreakdownSchema = import_zod.z.object({
  classId: import_zod.z.string().uuid(),
  className: import_zod.z.string(),
  schoolId: import_zod.z.string(),
  schoolName: import_zod.z.string(),
  studentCount: import_zod.z.number(),
  completedCount: import_zod.z.number()
});
var recommendedClassDataSchema = import_zod.z.object({
  id: import_zod.z.string().uuid(),
  title: import_zod.z.string(),
  startDate: import_zod.z.string().nullable(),
  finalDate: import_zod.z.string().nullable(),
  createdAt: import_zod.z.string(),
  progress: import_zod.z.number(),
  totalLessons: import_zod.z.number()
});
var recommendedClassHistoryItemSchema = import_zod.z.object({
  recommendedClass: recommendedClassDataSchema,
  subject: recommendedClassSubjectSchema,
  creator: recommendedClassCreatorSchema,
  stats: recommendedClassStatsSchema,
  breakdown: import_zod.z.array(recommendedClassBreakdownSchema)
});
var recommendedClassHistoryApiResponseSchema = import_zod.z.object({
  message: import_zod.z.string(),
  data: import_zod.z.object({
    recommendedClass: import_zod.z.array(recommendedClassHistoryItemSchema),
    total: import_zod.z.number()
  })
});
var determineRecommendedClassStatus = (finalDate, completionPercentage) => {
  if (completionPercentage === 100) {
    return "CONCLU\xCDDA" /* CONCLUIDA */;
  }
  if (finalDate) {
    const now = (0, import_dayjs.default)();
    const deadline = (0, import_dayjs.default)(finalDate);
    if (deadline.isBefore(now)) {
      return "VENCIDA" /* VENCIDA */;
    }
  }
  return "ATIVA" /* ATIVA */;
};
var transformRecommendedClassToTableItem = (item) => {
  const firstBreakdown = item.breakdown[0];
  const schoolName = firstBreakdown?.schoolName || "-";
  const className = firstBreakdown?.className || "-";
  const classDisplay = item.breakdown.length > 1 ? `${item.breakdown.length} turmas` : className;
  return {
    id: item.recommendedClass.id,
    startDate: item.recommendedClass.startDate ? (0, import_dayjs.default)(item.recommendedClass.startDate).format("DD/MM") : "-",
    deadline: item.recommendedClass.finalDate ? (0, import_dayjs.default)(item.recommendedClass.finalDate).format("DD/MM") : "-",
    title: item.recommendedClass.title,
    school: schoolName,
    year: "-",
    // API doesn't provide year directly
    creator: item.creator?.name || "-",
    subject: item.subject?.name || "-",
    class: classDisplay,
    status: determineRecommendedClassStatus(
      item.recommendedClass.finalDate,
      item.stats.completionPercentage
    ),
    completionPercentage: item.stats.completionPercentage
  };
};
var handleRecommendedClassFetchError = (error) => {
  if (error instanceof import_zod.z.ZodError) {
    console.error("Erro ao validar dados de hist\xF3rico de aulas:", error);
    return "Erro ao validar dados de hist\xF3rico de aulas";
  }
  console.error("Erro ao carregar hist\xF3rico de aulas:", error);
  return "Erro ao carregar hist\xF3rico de aulas";
};
var createUseRecommendedLessonsHistory = (fetchRecommendedClassHistory) => {
  return () => {
    const [state, setState] = (0, import_react.useState)({
      recommendedClass: [],
      loading: false,
      error: null,
      pagination: {
        total: 0,
        page: 1,
        limit: 10,
        totalPages: 0
      }
    });
    const fetchRecommendedClass = (0, import_react.useCallback)(
      async (filters) => {
        setState((prev) => ({ ...prev, loading: true, error: null }));
        try {
          const responseData = await fetchRecommendedClassHistory(filters);
          const validatedData = recommendedClassHistoryApiResponseSchema.parse(responseData);
          const tableItems = validatedData.data.recommendedClass.map(
            transformRecommendedClassToTableItem
          );
          const page = filters?.page || 1;
          const limit = filters?.limit || 10;
          const total = validatedData.data.total;
          const totalPages = Math.ceil(total / limit);
          setState({
            recommendedClass: tableItems,
            loading: false,
            error: null,
            pagination: {
              total,
              page,
              limit,
              totalPages
            }
          });
        } catch (error) {
          const errorMessage = handleRecommendedClassFetchError(error);
          setState((prev) => ({
            ...prev,
            loading: false,
            error: errorMessage
          }));
        }
      },
      [fetchRecommendedClassHistory]
    );
    return {
      ...state,
      fetchRecommendedClass
    };
  };
};
var createRecommendedLessonsHistoryHook = createUseRecommendedLessonsHistory;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  createRecommendedLessonsHistoryHook,
  createUseRecommendedLessonsHistory,
  determineRecommendedClassStatus,
  handleRecommendedClassFetchError,
  recommendedClassHistoryApiResponseSchema,
  transformRecommendedClassToTableItem
});
//# sourceMappingURL=index.js.map