import React, { useState, useEffect, useRef } from 'react'

function LoadEvent({ api, event, flv, hls, loadNoticeHandle }) {
  const loadFlag = useRef(false)
  /**
    * 流播放成功况捕获
    * @param  {...any} args 
    * @returns 
  */
  const loadHandle = (...args) => {
    //内部处理在这里做

    //之后抛出事件
    loadNoticeHandle && loadNoticeHandle()
  }

  useEffect(() => {

    try {
      if (flv && flv?._emitter) {
        flv.on('statistics_info', (res) => {
          if (!loadFlag.current) {
            loadFlag.current = true
            loadHandle()
          }
        })
      }
      if (hls) {
        hls.on('hlsManifestParsed', loadHandle)
      }
    } catch (e) {
      console.warn(e)
    }

    event.addEventListener('loadHandle', loadHandle, false)
    return () => {
      try {
        if (flv) {
          flv.off('loading_complete', loadHandle)
        }
        if (hls) {
          hls.off('hlsManifestParsed', loadHandle)
        }
      } catch (e) {
        console.warn(e)
      }
      event.removeEventListener('loadHandle', loadHandle, false)
    }
  }, [event, flv, hls])

  return <></>
}

export default LoadEvent
