import { useEffect } from 'react'
import {
  initSCORM,
  finishSCORM,
  getScore,
  setScore,
  getLessonStatus,
  setLessonStatus,
  getLessonLocation,
  setLessonLocation,
  getSuspendData,
  setSuspendData,
  getStudentName,
  getScormData,
  setScormData,
  completeLesson,
  isConnected,
} from '../scorm/scorm'

export function useSCORM() {
  useEffect(() => {
    initSCORM()
    window.addEventListener('beforeunload', finishSCORM)
    return () => {
      window.removeEventListener('beforeunload', finishSCORM)
      finishSCORM()
    }
  }, [])

  return {
    isConnected,
    getStudentName,
    getScore,
    setScore,
    getLessonStatus,
    setLessonStatus,
    getLessonLocation,
    setLessonLocation,
    getSuspendData,
    setSuspendData,
    getScormData,
    setScormData,
    completeLesson,
    finishSCORM,
  }
}
