/* eslint-disable  @typescript-eslint/no-explicit-any */
import { useMemo } from 'react'

import { secondsToHms } from 'helpers'

import type { Chapters } from '../../types'
import { calculateChapterStyles } from './helpers/calculateChapterStyles'

export type Props = {
  activeChapterIndex: number,
  allPlayedProgress: number,
  chapters?: Chapters,
  duration: number,
  loadedProgress: number,
  onPlayedProgressChange: (progress: number, seeking: boolean) => void,
  onTouchEnd?: () => any,
  onTouchStart?: () => any,
  playedProgress: number,
}

export const useProgressBar = ({
  activeChapterIndex,
  allPlayedProgress,
  chapters = [],
  duration,
  loadedProgress,
  playedProgress,
}: Props) => {
  const calculatedChapters = useMemo(
    () => calculateChapterStyles({
      activeChapterIndex,
      chapters,
      loadedProgress,
      playedProgress,
      videoDuration: duration,
    }),
    [
      activeChapterIndex,
      loadedProgress,
      playedProgress,
      duration,
      chapters,
    ],
  )

  return {
    calculatedChapters,
    playedProgressInPercent: Math.min(allPlayedProgress * 100 / duration, 100),
    time: secondsToHms(allPlayedProgress / 1000),
  }
}
