{"version":3,"sources":["../src/hooks/useExamsHistory.ts"],"sourcesContent":["import { useState, useCallback } from 'react';\nimport dayjs from 'dayjs';\nimport type { BaseApiClient } from '../types/api';\nimport { mapExamStatusToDisplay } from '../types/examsHistory';\nimport type {\n  ExamHistoryResponse,\n  ExamTableItem,\n  ExamsHistoryApiResponse,\n  ExamHistoryFilters,\n  ExamPagination,\n  ExamApiFilterOptions,\n} from '../types/examsHistory';\nimport { createFetchErrorHandler } from '../utils/hookErrorHandler';\nimport {\n  mergeFilterOptions,\n  extractBreakdownFilterOptions,\n} from '../utils/filterHelpers';\n\n/**\n * Hook state interface\n */\nexport interface UseExamsHistoryState {\n  exams: ExamTableItem[];\n  loading: boolean;\n  error: string | null;\n  pagination: ExamPagination;\n  apiFilterOptions: ExamApiFilterOptions;\n}\n\n/**\n * Hook return type\n */\nexport interface UseExamsHistoryReturn extends UseExamsHistoryState {\n  fetchExams: (filters?: ExamHistoryFilters) => Promise<void>;\n}\n\n/**\n * Default pagination values\n */\nexport const DEFAULT_EXAMS_PAGINATION: ExamPagination = {\n  total: 0,\n  page: 1,\n  limit: 10,\n  totalPages: 0,\n};\n\n/**\n * Default API filter options\n */\nexport const DEFAULT_EXAM_FILTER_OPTIONS: ExamApiFilterOptions = {\n  schools: [],\n  classes: [],\n  subjects: [],\n  schoolYears: [],\n};\n\n/**\n * Transform API response to table item format\n * @param exam - Exam from API response\n * @returns Formatted exam for table display\n */\nexport const transformExamToTableItem = (\n  exam: ExamHistoryResponse\n): ExamTableItem => {\n  const firstBreakdown = exam.breakdown?.[0];\n  return {\n    id: exam.id,\n    startDate: exam.startDate\n      ? dayjs(exam.startDate).format('DD/MM/YYYY')\n      : '-',\n    title: exam.title,\n    school: firstBreakdown?.school?.name ?? '-',\n    class: firstBreakdown?.class?.name ?? '-',\n    status: mapExamStatusToDisplay(exam.status),\n    questionCount: exam.questionCount,\n    createdAt: dayjs(exam.createdAt).format('DD/MM/YYYY'),\n    completionPercentage: exam.completionPercentage,\n  };\n};\n\n/**\n * Handle errors during exam fetch\n * Uses the generic error handler factory to reduce code duplication\n */\nexport const handleExamFetchError = createFetchErrorHandler(\n  'Erro ao validar dados de historico de provas',\n  'Erro ao carregar historico de provas'\n);\n\n/**\n * Extract unique filter options from exams API response.\n * Delegates to the shared extractBreakdownFilterOptions helper.\n */\nexport const extractExamFilterOptions = (\n  exams: ExamHistoryResponse[]\n): ExamApiFilterOptions => extractBreakdownFilterOptions(exams);\n\n/**\n * Build query params from filters\n * Always includes type=PROVA to filter for exams\n */\nconst buildQueryParams = (\n  filters?: ExamHistoryFilters\n): Record<string, unknown> => {\n  // Always include type=PROVA for exam filtering\n  if (!filters) return { type: 'PROVA' };\n\n  const params: Record<string, unknown> = { type: 'PROVA' };\n  for (const key in filters) {\n    const value = filters[key as keyof ExamHistoryFilters];\n    if (value !== undefined && value !== null) {\n      params[key] = value;\n    }\n  }\n  return params;\n};\n\n/**\n * Hook implementation\n */\nconst useExamsHistoryImpl = (\n  apiClient: BaseApiClient\n): UseExamsHistoryReturn => {\n  const [state, setState] = useState<UseExamsHistoryState>({\n    exams: [],\n    loading: false,\n    error: null,\n    pagination: DEFAULT_EXAMS_PAGINATION,\n    apiFilterOptions: DEFAULT_EXAM_FILTER_OPTIONS,\n  });\n\n  /**\n   * Fetch exams history from API\n   * @param filters - Optional filters for pagination, search, sorting, etc.\n   */\n  const fetchExams = useCallback(\n    async (filters?: ExamHistoryFilters) => {\n      setState((prev) => ({ ...prev, loading: true, error: null }));\n\n      try {\n        const params = buildQueryParams(filters);\n        // Use activities/history endpoint with type=PROVA\n        const response = await apiClient.get<ExamsHistoryApiResponse>(\n          '/activities/history',\n          { params }\n        );\n\n        const { data } = response.data;\n\n        // Transform activities to table format (response uses 'activities' field)\n        const tableItems = data.activities.map((exam) =>\n          transformExamToTableItem(exam)\n        );\n\n        // Extract filter options from response\n        const extracted = extractExamFilterOptions(data.activities);\n\n        // Update state with transformed data\n        setState((prev) => ({\n          exams: tableItems,\n          loading: false,\n          error: null,\n          pagination: data.pagination,\n          apiFilterOptions: {\n            schools: mergeFilterOptions(\n              prev.apiFilterOptions.schools,\n              extracted.schools\n            ),\n            classes: mergeFilterOptions(\n              prev.apiFilterOptions.classes,\n              extracted.classes\n            ),\n            subjects: mergeFilterOptions(\n              prev.apiFilterOptions.subjects,\n              extracted.subjects\n            ),\n            schoolYears: mergeFilterOptions(\n              prev.apiFilterOptions.schoolYears,\n              extracted.schoolYears\n            ),\n          },\n        }));\n      } catch (error) {\n        const errorMessage = handleExamFetchError(error);\n        setState((prev) => ({\n          ...prev,\n          loading: false,\n          error: errorMessage,\n        }));\n      }\n    },\n    [apiClient]\n  );\n\n  return {\n    ...state,\n    fetchExams,\n  };\n};\n\n/**\n * Factory function to create useExamsHistory hook\n *\n * @param apiClient - API client instance (axios, fetch wrapper, etc.)\n * @returns Hook for managing exams history\n *\n * @example\n * ```tsx\n * // In your app setup\n * import { createUseExamsHistory } from 'analytica-frontend-lib';\n * import api from '@/services/apiService';\n *\n * export const useExamsHistory = createUseExamsHistory(api);\n *\n * // In your component\n * const { exams, loading, error, pagination, fetchExams } = useExamsHistory();\n * ```\n */\nexport const createUseExamsHistory = (apiClient: BaseApiClient) => {\n  return (): UseExamsHistoryReturn => useExamsHistoryImpl(apiClient);\n};\n\n/**\n * Alias for createUseExamsHistory\n */\nexport const createExamsHistoryHook = createUseExamsHistory;\n"],"mappings":";;;;;;;;;;;;AAAA,SAAS,UAAU,mBAAmB;AACtC,OAAO,WAAW;AAsCX,IAAM,2BAA2C;AAAA,EACtD,OAAO;AAAA,EACP,MAAM;AAAA,EACN,OAAO;AAAA,EACP,YAAY;AACd;AAKO,IAAM,8BAAoD;AAAA,EAC/D,SAAS,CAAC;AAAA,EACV,SAAS,CAAC;AAAA,EACV,UAAU,CAAC;AAAA,EACX,aAAa,CAAC;AAChB;AAOO,IAAM,2BAA2B,CACtC,SACkB;AAClB,QAAM,iBAAiB,KAAK,YAAY,CAAC;AACzC,SAAO;AAAA,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