'use strict'
!(function(require, directRequire) {
  const a = require('./dfd9a72b9ff6078018aa4a64eed949a5.js'),
    b = require('./5dc59f418d85e854c1a12d402d29e2d7.js'),
    c = require('./0634ee2ebd3e560d9d4804ecc960160f.js'),
    d = require('./bc78839ccca8df9e5ceeb7fae11b7be2.js')
  let e
  module.exports = {
    initMusic: function(d) {
      if (e) return
      let f = a()
      e = document.createElement('audio')
      let g = global.windowMap.get('MAIN'),
        h = g.window.document.body
      h.appendChild(e),
        e.addEventListener('play', () => {
          d({
            type: c.SIMULATOR_SET_BACKGROUND_AUDIO,
            data: { status: b.MUSIC_STATE_PLAY }
          }),
            f.triggerOnEvent({
              eventName: 'onMusicPlay',
              data: { dataUrl: e.src }
            }),
            f.triggerOnEvent({
              eventName: 'onBackgroundAudioStateChange',
              data: { state: 'play' }
            })
        }),
        e.addEventListener('pause', () => {
          e &&
            !e.ended &&
            (!e.error &&
              d({
                type: c.SIMULATOR_SET_BACKGROUND_AUDIO,
                data: { status: b.MUSIC_STATE_PAUSE }
              }),
            f.triggerOnEvent({
              eventName: 'onMusicPause',
              data: { dataUrl: e.src }
            }),
            f.triggerOnEvent({
              eventName: 'onBackgroundAudioStateChange',
              data: { state: 'pause' }
            }))
        }),
        e.addEventListener('ended', () => {
          d({
            type: c.SIMULATOR_SET_BACKGROUND_AUDIO,
            data: { status: b.MUSIC_STATE_END }
          }),
            f.triggerOnEvent({
              eventName: 'onMusicEnd',
              data: { dataUrl: e.src }
            }),
            f.triggerOnEvent({
              eventName: 'onBackgroundAudioStateChange',
              data: { state: 'ended' }
            }),
            e.remove(),
            (e = void 0)
        }),
        e.addEventListener('waiting', () => {
          f.triggerOnEvent({
            eventName: 'onBackgroundAudioStateChange',
            data: { state: 'waiting' }
          })
        }),
        e.addEventListener('canplay', () => {
          f.triggerOnEvent({
            eventName: 'onBackgroundAudioStateChange',
            data: { state: 'canplay' }
          })
        }),
        e.addEventListener('error', a => {
          d({
            type: c.SIMULATOR_SET_BACKGROUND_AUDIO,
            data: { status: b.MUSIC_STATE_ERROR }
          })
          let { target: g } = a,
            { error: h } = g,
            i = h.code
          i =
            i === h.MEDIA_ERR_NETWORK
              ? 10002
              : i === h.MEDIA_ERR_DECODE
                ? 10003
                : 10001
          let j = h.code
          f.triggerOnEvent({
            eventName: 'onMusicError',
            data: { dataUrl: e.src, errMsg: 'Error: ' + e.error.code }
          }),
            f.triggerOnEvent({
              eventName: 'onBackgroundAudioStateChange',
              data: { state: 'error', errMsg: j, errCode: i }
            }),
            e.remove(),
            (e = void 0)
        })
    },
    playMusic: function(a = {}) {
      a.src && a.src != e.src
        ? ((e.src = a.src), e.play(), (e.currentTime = a.startTime || 0))
        : e.src && e.play()
    },
    pauseMusic: function(a = {}) {
      return !!e && (e.paused || e.pause(), !0)
    },
    resumeMusic: function(a = {}) {
      e && e.paused && e.play()
    },
    stopMusic: function(f = {}) {
      if (!e) return !1
      let g = a(),
        h = e.src
      return (
        setTimeout(() => {
          const a = d.dispatch.bind(d)
          a({
            type: c.SIMULATOR_SET_BACKGROUND_AUDIO,
            data: { status: b.MUSIC_STATE_END }
          }),
            g.triggerOnEvent({ eventName: 'onMusicEnd', data: { dataUrl: h } }),
            g.triggerOnEvent({
              eventName: 'onBackgroundAudioStateChange',
              data: { state: 'stop' }
            })
        }),
        e.remove(),
        (e = void 0),
        !0
      )
    },
    seekMusic: function(a = {}) {
      e && !e.paused && (e.currentTime = 1 * a.currentTime)
    },
    getAudioBufferd: function() {
      let a = e ? e.buffered : ''
      if (a) {
        let c = e.currentTime
        for (let d, b = 0, e = a.length; b < e; b++)
          if (((d = a), a.start(b) <= c && a.end(b) >= c)) return a.end(b)
      }
      return 0
    },
    getAudio: function() {
      return e
    },
    removeAudio: function() {
      e && (e.remove(), (e = void 0))
    }
  }
})(require('lazyload'), require)
