// src/hooks/useQuestionsList.ts
import { useState, useCallback } from "react";
var useQuestionsListImpl = (apiClient) => {
  const [state, setState] = useState({
    questions: [],
    pagination: null,
    loading: false,
    loadingMore: false,
    error: null,
    currentFilters: null
  });
  const updateState = useCallback((updates) => {
    setState((prev) => ({ ...prev, ...updates }));
  }, []);
  const handleError = 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 = 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 = 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 = 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 = 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 = 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);
};
export {
  createQuestionsListHook,
  createUseQuestionsList
};
//# sourceMappingURL=index.mjs.map