// src/hooks/useRecommendedClassDrafts.ts
import { useState as useState2, useCallback as useCallback2 } from "react";

// src/hooks/useRecommendedClassModels.ts
import { useState, useCallback } from "react";
import { z as z2 } from "zod";
import dayjs from "dayjs";

// src/types/recommendedLessons.ts
var RECOMMENDED_CLASS_FILTER_STATUS_OPTIONS = [
  { id: "VENCIDA" /* VENCIDA */, name: "Vencida" },
  { id: "A_VENCER" /* A_VENCER */, name: "Ativa" }
];
var RECOMMENDED_CLASS_STATUS_OPTIONS = [
  { id: "A_VENCER" /* A_VENCER */, name: "A Vencer" },
  { id: "VENCIDA" /* VENCIDA */, name: "Vencida" },
  { id: "CONCLUIDA" /* CONCLUIDA */, name: "Conclu\xEDda" }
];
var RecommendedClassDraftType = /* @__PURE__ */ ((RecommendedClassDraftType2) => {
  RecommendedClassDraftType2["MODELO"] = "MODELO";
  RecommendedClassDraftType2["RASCUNHO"] = "RASCUNHO";
  return RecommendedClassDraftType2;
})(RecommendedClassDraftType || {});

// src/utils/hookErrorHandler.ts
import { z } from "zod";
var createFetchErrorHandler = (validationErrorMessage, genericErrorMessage) => (error) => {
  if (error instanceof z.ZodError) {
    console.error(validationErrorMessage, error);
    return validationErrorMessage;
  }
  console.error(genericErrorMessage, error);
  return genericErrorMessage;
};

// src/hooks/useRecommendedClassModels.ts
var recommendedClassModelResponseSchema = z2.object({
  id: z2.string().uuid(),
  type: z2.nativeEnum(RecommendedClassDraftType),
  title: z2.string(),
  description: z2.string().nullable(),
  creatorUserInstitutionId: z2.string().uuid(),
  subjectId: z2.string().uuid().nullable(),
  subject: z2.object({ id: z2.string(), name: z2.string() }).nullable().optional(),
  startDate: z2.string().nullable(),
  finalDate: z2.string().nullable(),
  createdAt: z2.string(),
  updatedAt: z2.string(),
  // Activity drafts attached to this model. Kept in the schema so Zod
  // preserves the field during validation — used to derive
  // activityDraftsCount for the send-modal "Permitir refazer?" gate.
  activityDrafts: z2.array(
    z2.object({
      activityDraftId: z2.uuid(),
      sequence: z2.number(),
      title: z2.string().nullable().optional()
    })
  ).optional().default([])
});
var recommendedClassModelsApiResponseSchema = z2.object({
  message: z2.string(),
  data: z2.object({
    drafts: z2.array(recommendedClassModelResponseSchema),
    total: z2.number()
  })
});
var transformRecommendedClassModelToTableItem = (model, subjectsMap) => {
  const subjectName = model.subject?.name || (model.subjectId ? subjectsMap?.get(model.subjectId) || "" : "");
  return {
    id: model.id,
    title: model.title || "Sem t\xEDtulo",
    savedAt: dayjs(model.createdAt).format("DD/MM/YYYY"),
    subject: subjectName,
    subjectId: model.subjectId,
    activityDraftsCount: model.activityDrafts?.length ?? 0
  };
};
var handleRecommendedClassModelFetchError = createFetchErrorHandler(
  "Erro ao validar dados de modelos de aulas",
  "Erro ao carregar modelos de aulas"
);

// src/hooks/useRecommendedClassDrafts.ts
var DEFAULT_RECOMMENDED_CLASS_DRAFTS_PAGINATION = {
  total: 0,
  page: 1,
  limit: 10,
  totalPages: 0
};
var handleRecommendedClassDraftFetchError = createFetchErrorHandler(
  "Erro ao validar dados de rascunhos de aulas",
  "Erro ao carregar rascunhos de aulas"
);
var createUseRecommendedClassDrafts = (apiClient) => {
  return () => {
    const [state, setState] = useState2({
      models: [],
      loading: false,
      error: null,
      pagination: DEFAULT_RECOMMENDED_CLASS_DRAFTS_PAGINATION
    });
    const fetchModels = useCallback2(
      async (filters, subjectsMap) => {
        setState((prev) => ({ ...prev, loading: true, error: null }));
        try {
          const response = await apiClient.get(
            "/recommended-class/drafts",
            { params: filters }
          );
          const validatedData = recommendedClassModelsApiResponseSchema.parse(
            response.data
          );
          const tableItems = validatedData.data.drafts.map(
            (draft) => transformRecommendedClassModelToTableItem(draft, subjectsMap)
          );
          const limit = filters?.limit || 10;
          const page = filters?.page || 1;
          const total = validatedData.data.total;
          const totalPages = Math.ceil(total / limit);
          setState({
            models: tableItems,
            loading: false,
            error: null,
            pagination: {
              total,
              page,
              limit,
              totalPages
            }
          });
        } catch (error) {
          const errorMessage = handleRecommendedClassDraftFetchError(error);
          setState((prev) => ({
            ...prev,
            loading: false,
            error: errorMessage
          }));
        }
      },
      [apiClient]
    );
    const deleteModel = useCallback2(
      async (id) => {
        try {
          await apiClient.delete(`/recommended-class/drafts/${id}`);
          return true;
        } catch (error) {
          console.error("Erro ao deletar rascunho:", error);
          return false;
        }
      },
      [apiClient]
    );
    return {
      ...state,
      fetchModels,
      deleteModel
    };
  };
};
var createRecommendedClassDraftsHook = createUseRecommendedClassDrafts;
export {
  DEFAULT_RECOMMENDED_CLASS_DRAFTS_PAGINATION,
  createRecommendedClassDraftsHook,
  createUseRecommendedClassDrafts,
  handleRecommendedClassDraftFetchError
};
//# sourceMappingURL=index.mjs.map