"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/hooks/useQuestionsList.ts
var useQuestionsList_exports = {};
__export(useQuestionsList_exports, {
  createQuestionsListHook: () => createQuestionsListHook,
  createUseQuestionsList: () => createUseQuestionsList
});
module.exports = __toCommonJS(useQuestionsList_exports);
var import_react = require("react");
var useQuestionsListImpl = (apiClient) => {
  const [state, setState] = (0, import_react.useState)({
    questions: [],
    pagination: null,
    loading: false,
    loadingMore: false,
    error: null,
    currentFilters: null
  });
  const updateState = (0, import_react.useCallback)((updates) => {
    setState((prev) => ({ ...prev, ...updates }));
  }, []);
  const handleError = (0, import_react.useCallback)(
    (error) => {
      console.error("Erro ao carregar quest\xF5es:", error);
      let errorMessage = "Erro ao carregar quest\xF5es";
      if (error && typeof error === "object" && "response" in error) {
        const axiosError = error;
        errorMessage = axiosError?.response?.data?.message || axiosError?.message || errorMessage;
      } else if (error instanceof Error) {
        errorMessage = error.message;
      }
      updateState({
        loading: false,
        loadingMore: false,
        error: errorMessage
      });
    },
    [updateState]
  );
  const convertPagination = (pagination) => {
    if (!pagination) return null;
    return {
      page: pagination.page,
      pageSize: pagination.limit,
      total: pagination.total,
      totalPages: pagination.totalPages,
      hasNext: pagination.hasNext,
      hasPrevious: pagination.hasPrev
    };
  };
  const fetchQuestions = (0, import_react.useCallback)(
    async (filters, append = false) => {
      if (append) {
        setState((prev) => ({ ...prev, loadingMore: true, error: null }));
      } else {
        updateState({ loading: true, error: null, questions: [] });
      }
      try {
        const validatedFilters = {
          ...filters
        };
        const response = await apiClient.post(
          "/questions/list",
          validatedFilters
        );
        setState((prev) => ({
          loading: false,
          loadingMore: false,
          questions: append ? [...prev.questions, ...response.data.data.questions] : response.data.data.questions,
          pagination: convertPagination(response.data.data.pagination),
          error: null,
          currentFilters: validatedFilters
        }));
      } catch (error) {
        setState((prev) => ({
          ...prev,
          loading: false,
          loadingMore: false
        }));
        handleError(error);
      }
    },
    [apiClient, updateState, handleError]
  );
  const fetchRandomQuestions = (0, import_react.useCallback)(
    async (count, filters) => {
      try {
        const validatedFilters = {
          ...filters,
          randomQuestions: count
        };
        const response = await apiClient.post(
          "/questions/list",
          validatedFilters
        );
        return response.data.data.questions;
      } catch (error) {
        handleError(error);
        return [];
      }
    },
    [apiClient, handleError]
  );
  const fetchQuestionsByIds = (0, import_react.useCallback)(
    async (questionIds) => {
      try {
        const body = {
          questionsIds: questionIds
        };
        const response = await apiClient.post(
          "/questions/by-ids",
          body
        );
        return response.data.data.questions;
      } catch (error) {
        handleError(error);
        return [];
      }
    },
    [apiClient, handleError]
  );
  const loadMore = (0, import_react.useCallback)(
    async (externalFilters, externalPagination) => {
      setState((prev) => {
        const {
          currentFilters,
          pagination: internalPagination,
          loadingMore: isLoadingMore
        } = prev;
        const effectiveFilters = currentFilters || externalFilters;
        const effectivePagination = internalPagination || externalPagination;
        if (isLoadingMore || !effectiveFilters || !effectivePagination?.hasNext) {
          return prev;
        }
        const nextPageFilters = {
          ...effectiveFilters,
          page: effectivePagination.page + 1
        };
        fetchQuestions(nextPageFilters, true).catch((error) => {
          console.error("Erro ao carregar mais quest\xF5es:", error);
        });
        return {
          ...prev,
          loadingMore: true
        };
      });
    },
    [fetchQuestions]
  );
  const reset = (0, import_react.useCallback)(() => {
    setState({
      questions: [],
      pagination: null,
      loading: false,
      loadingMore: false,
      error: null,
      currentFilters: null
    });
  }, []);
  return {
    ...state,
    fetchQuestions,
    fetchRandomQuestions,
    fetchQuestionsByIds,
    loadMore,
    reset
  };
};
var createUseQuestionsList = (apiClient) => {
  return () => useQuestionsListImpl(apiClient);
};
var createQuestionsListHook = (apiClient) => {
  return createUseQuestionsList(apiClient);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  createQuestionsListHook,
  createUseQuestionsList
});
//# sourceMappingURL=index.js.map