// src/hooks/useActivityModels.ts
import { useState, useCallback } from "react";
import dayjs from "dayjs";
var DEFAULT_MODELS_PAGINATION = {
  total: 0,
  page: 1,
  limit: 10,
  totalPages: 0
};
var transformModelToTableItem = (model, subjectsMap) => {
  let subject = model.subject;
  if (!subject && model.subjectId && subjectsMap) {
    const subjectName = subjectsMap.get(model.subjectId);
    if (subjectName) {
      subject = {
        id: model.subjectId,
        name: subjectName,
        icon: "BookOpen",
        color: "#6B7280"
      };
    }
  }
  const activityType = model.type === "MODELO" /* MODELO */ ? "MODELO" /* MODELO */ : "RASCUNHO" /* RASCUNHO */;
  return {
    id: model.id,
    type: activityType,
    title: model.title || "Sem t\xEDtulo",
    savedAt: dayjs(model.createdAt).format("DD/MM/YYYY"),
    subject: subject || null,
    subjectId: model.subjectId
  };
};
var buildQueryParams = (filters, activityCategory) => {
  const params = {
    type: "MODELO" /* MODELO */
    // models = MODELO
  };
  if (activityCategory) {
    params.activityType = activityCategory;
  }
  if (filters) {
    for (const key in filters) {
      const value = filters[key];
      if (value !== void 0 && value !== null) {
        params[key] = value;
      }
    }
  }
  return params;
};
var useActivityModelsImpl = (apiClient, options) => {
  const [state, setState] = useState({
    models: [],
    loading: false,
    error: null,
    pagination: DEFAULT_MODELS_PAGINATION
  });
  const fetchModels = useCallback(
    async (filters, subjectsMap) => {
      setState((prev) => ({ ...prev, loading: true, error: null }));
      try {
        const params = buildQueryParams(filters, options?.activityCategory);
        const response = await apiClient.get(
          "/activity-drafts",
          { params }
        );
        const tableItems = response.data.data.activityDrafts.map(
          (model) => transformModelToTableItem(model, subjectsMap)
        );
        const limit = filters?.limit || 10;
        const page = filters?.page || 1;
        const total = response.data.data.total;
        const totalPages = Math.ceil(total / limit);
        setState({
          models: tableItems,
          loading: false,
          error: null,
          pagination: { total, page, limit, totalPages }
        });
      } catch (error) {
        console.error("Erro ao carregar modelos:", error);
        setState((prev) => ({
          ...prev,
          loading: false,
          error: "Erro ao carregar modelos de atividades"
        }));
      }
    },
    [apiClient, options?.activityCategory]
  );
  const deleteModel = useCallback(
    async (id) => {
      try {
        await apiClient.delete(`/activity-drafts/${id}`);
        setState((prev) => ({
          ...prev,
          models: prev.models.filter((m) => m.id !== id),
          pagination: {
            ...prev.pagination,
            total: Math.max(0, prev.pagination.total - 1)
          }
        }));
        return true;
      } catch (error) {
        console.error("Erro ao deletar modelo:", error);
        return false;
      }
    },
    [apiClient]
  );
  return { ...state, fetchModels, deleteModel };
};
var createUseActivityModels = (apiClient, options) => {
  return () => useActivityModelsImpl(apiClient, options);
};
var createActivityModelsHook = createUseActivityModels;
export {
  DEFAULT_MODELS_PAGINATION,
  createActivityModelsHook,
  createUseActivityModels,
  transformModelToTableItem
};
//# sourceMappingURL=index.mjs.map