import { Parser, Manifest } from 'm3u8-parser'
import reduce from 'lodash/reduce'

export const manifestParser = (manifest: string) => {
  const parser = new Parser()
  parser.push(manifest)
  parser.end()
  const parsedManifest: Manifest = parser.manifest

  const totalDuration = reduce(
    parsedManifest?.segments,
    (sum: number, { duration }) => sum + duration,
    0,
  )
  return totalDuration
}
