/* eslint-disable no-useless-catch */
/* eslint-disable no-unused-vars */
import axiosInstance from '../../../../utils/axiosInstance'

const getSoalNumber = title => {
	const match = title.match(/\d+$/)
	return match ? parseInt(match[0], 10) : 0
}

const getLevelId = async (topicId, levelName) => {
	try {
		const response = await axiosInstance.get(`/level/topic/${topicId}`)
		const filteredData = response.data.data.levels.filter(
			item => item.NAME_LEVEL === levelName
		)
		return filteredData[0].ID_LEVEL
	} catch (error) {
		return []
	}
}

const createStdLearning = async data => {
	try {
		const response = await axiosInstance.post(`/stdLearning`, data)
		return response.data
	} catch (error) {
		console.error('Error creating std_learning:', error)
		throw error
	}
}

const fetchReview = async stdLearning => {
	try {
		const response = await axiosInstance.get(`/studentAnswers/${stdLearning}`)

		return response.data
	} catch (error) {
		throw error
	}
}

const submitAnswer = async dataAnswer => {
	try {
		const response = await axiosInstance.post(`/stdExercise`, dataAnswer)
		return response.data
	} catch (error) {
		throw error
	}
}

const checkStdLearning = async level => {
	try {
		const response = await axiosInstance.get(`/stdLearning/level/${level}`)
		return response.data.payload.ID_STUDENT_LEARNING
	} catch (error) {
		//   console.error('Error checking std_learning:', error);
		//   throw error;
		return null
	}
}

const getScore = async stdLearning => {
	try {
		const response = await axiosInstance.get(
			`/stdLearning/score/${stdLearning}`
		)
		return response.data
	} catch (error) {
		console.error('Error fetching result:', error)
		throw error
	}
}

export default {
	getLevelId,
	checkStdLearning,
	createStdLearning,
	fetchReview,
	submitAnswer,
	getScore,
}
