"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/useActivityModels.ts
var useActivityModels_exports = {};
__export(useActivityModels_exports, {
  DEFAULT_MODELS_PAGINATION: () => DEFAULT_MODELS_PAGINATION,
  createActivityModelsHook: () => createActivityModelsHook,
  createUseActivityModels: () => createUseActivityModels,
  transformModelToTableItem: () => transformModelToTableItem
});
module.exports = __toCommonJS(useActivityModels_exports);
var import_react = require("react");
var import_dayjs = __toESM(require("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: (0, import_dayjs.default)(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] = (0, import_react.useState)({
    models: [],
    loading: false,
    error: null,
    pagination: DEFAULT_MODELS_PAGINATION
  });
  const fetchModels = (0, import_react.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 = (0, import_react.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;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DEFAULT_MODELS_PAGINATION,
  createActivityModelsHook,
  createUseActivityModels,
  transformModelToTableItem
});
//# sourceMappingURL=index.js.map