import findIndex from 'lodash/findIndex'
import size from 'lodash/size'

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

export const findChapterByProgress = (chapters: Chapters, progressMs: number) => {
  if (size(chapters) === 1 && chapters[0].isFullMatchChapter) return 0
  return (
    findIndex(chapters, ({ endMs, startMs }) => (
      startMs <= progressMs && progressMs <= endMs
    ))
  )
}
