"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _util = require("../__support/util.js");

require("./index-sfc.css");

/**
 * 波形图音频播放器
 * base on: https://wavesurfer-js.org/docs/options.html
 * */
require('wavesurfer.js');

var _window = window,
    WaveSurfer = _window.WaveSurfer;

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c('div', {
    attrs: {
      "id": _vm.domID
    }
  });
};

var __vue_staticRenderFns__ = [];
var _default2 = {
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__,
  name: "audio-player",
  emits: ['ready', 'ended', 'pause', 'play', 'timeUp'],
  props: {
    options: {
      type: Object,
      default: function _default() {
        return {};
      }
    },
    seekAble: {
      type: Boolean,
      default: true
    },
    subscribeTimes: {
      type: Array,
      default: function _default() {
        /** item format:
         * {
         *   time: Number(s)
         * }
         * */
        return [];
      }
    }
  },
  data: function data() {
    return {
      domID: (0, _util.domId)(),
      player: null,
      currentTime: 0
    };
  },
  computed: {
    timeListeners: function timeListeners() {
      var _this = this;

      return this.subscribeTimes.map(function (item) {
        return (0, _extends2.default)({}, item, {
          pass: item.time < _this.currentTime
        });
      });
    },
    timeTarget: function timeTarget() {
      return this.timeListeners.filter(function (item) {
        return !item.pass;
      })[0];
    }
  },
  mounted: function mounted() {
    var _this2 = this;

    // 默认配置
    var options = (0, _extends2.default)({
      container: "#" + this.domID,
      backend: 'MediaElement',
      progressColor: '#A8DBA8',
      mediaControls: true,
      height: 64
    }, this.options);
    var player = WaveSurfer.create(options);
    this.player = player;
    this.$emit('ready', this.player); // 基本事件支持

    player.on('finish', function () {
      _this2.$emit('ended');
    });
    player.on('pause', function () {
      _this2.$emit('pause');
    });
    player.on('play', function () {
      _this2.$emit('play');
    }); // 拖拽进度控制

    var audioEl = document.querySelector("#" + this.domID + " audio");
    audioEl.addEventListener('seeked', function () {
      // console.log(audioEl.currentTime, this.currentTime);
      if (audioEl.currentTime - _this2.currentTime > 1) {
        if (!_this2.seekAble) {
          audioEl.currentTime = _this2.currentTime;
          console.warn('禁止快进！');
        }
      }
    }); // 进度更新事件

    this.player.on('audioprocess', function (ct) {
      // 监听指定时间
      if (_this2.timeTarget && _this2.timeTarget.time < ct) {
        _this2.$emit('timeUp', _this2.timeTarget);
      } // 同步当前时间


      _this2.currentTime = ct;
    });
  },
  beforeDestroy: function beforeDestroy() {
    if (this.player) {
      this.player.destroy();
    }
  }
};
exports.default = _default2;