import Taro from "@tarojs/taro";

const KEY = "VIDEO_EVENT"
const SET_KEY = 'SET_VIDEO_EVENT'

type StateChangeName = 'play' | 'pause'

/** 视频状态播改变播放 */
function videoState(name: StateChangeName) {
  Taro.eventCenter.trigger(KEY, {name})
}

/** 接受状态播放改变 */
function onVideoState(fn: ({name: StateChangeName}) => void) {
  Taro.eventCenter.once(KEY, fn)
}

/** 设置视频状态 */
function setVideoState(name: StateChangeName) {
  Taro.eventCenter.trigger(SET_KEY, {name})
}

/** 接受状态改变 */
function onSetVideoState(fn: (data: { name: StateChangeName }) => void) {
  Taro.eventCenter.on(SET_KEY, fn)
}

function videoOff() {
  Taro.eventCenter.off(KEY)
  Taro.eventCenter.off(SET_KEY)
}


export default {
  videoState,
  onVideoState,
  onSetVideoState,
  setVideoState,
  videoOff
}
