import _extends from "@babel/runtime/helpers/esm/extends";
import { domId } from '../__support/util.js';
import videojs from './lib/video.js';
import './lib/video-js.min.css';
import '@videojs/themes/dist/fantasy/index.css';
import '@tower1229/videojs-plugin-marker/dist/style.css';
import '@tower1229/videojs-plugin-source-switcher/dist/style.css';
window.videojs = videojs;

require('./lib/zh-CN.js');

var loadPlugins = Promise.all([import('@tower1229/videojs-plugin-marker'), import('@tower1229/videojs-plugin-source-switcher')]);
import './index-sfc.css';

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

  var _h = _vm.$createElement;

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

  return _c('div', {
    staticStyle: {
      "width": "100%",
      "height": "100%"
    }
  }, [_c('video', {
    staticClass: "video-js vjs-theme-fantasy",
    staticStyle: {
      "width": "100%",
      "height": "100%"
    },
    attrs: {
      "id": _vm.domID,
      "controlslist": "nodownload noremoteplayback",
      "oncontextmenu": "return false"
    }
  })]);
};

var __vue_staticRenderFns__ = [];
export default {
  _scopeId: 'data-v-24cd70b7',
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__,
  name: "video-player",
  emits: ['ready', 'ended', 'pause', 'play', 'timeUp'],
  props: {
    options: {
      type: Object,
      default: function _default() {
        return {// CryptoConfig
          // RequestOption
        };
      }
    },
    seekAble: {
      type: Boolean,
      default: true
    },
    subscribeTimes: {
      type: Array,
      default: function _default() {
        /**
         * 监听一组播放进度，通过 ‘timeUp’ 事件回调
         * item format:
         * {
         *   time: Number(s)
         * }
         * */
        return [];
      }
    }
  },
  data: function data() {
    return {
      domID: domId(),
      player: null,
      currentTime: 0
    };
  },
  computed: {
    timeListeners: function timeListeners() {
      var _this = this;

      return this.subscribeTimes.map(function (item) {
        return _extends({}, item, {
          pass: item.time < _this.currentTime
        });
      });
    },
    timeTarget: function timeTarget() {
      return this.timeListeners.filter(function (item) {
        return !item.pass;
      })[0];
    }
  },
  watch: {
    seekAble: {
      handler: function handler() {
        this.setControlBar();
      },
      immediate: true
    }
  },
  mounted: function mounted() {
    var _this2 = this;

    // 默认配置
    var options = _extends({
      playbackRates: [0.5, 1, 1.5, 2],
      language: 'zh-CN',
      fluid: true
    }, this.options);

    var player = videojs(this.domID, options, function () {
      loadPlugins.then(function () {
        _this2.$emit('ready', player);
      });
    }); // 基本事件

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

    this.setControlBar(); // 用户活动事件
    // this.player.on('useractive', e => {
    //   console.log('useractive', e)
    // })
    // this.player.on('userinactive', e => {
    //   console.log('userinactive', e)
    // })
    // 进度更新事件

    this.player.on('timeupdate', function () {
      var ct = _this2.player.currentTime(); // 监听指定时间


      if (_this2.timeTarget && _this2.timeTarget.time < ct) {
        _this2.$emit('timeUp', _this2.timeTarget);
      } // 同步当前时间


      _this2.currentTime = ct;
    });
  },
  beforeDestroy: function beforeDestroy() {
    if (this.player) {
      this.player.dispose();
    }
  },
  methods: {
    setControlBar: function setControlBar() {
      if (this.player) {
        if (this.seekAble) {
          this.player.controlBar.progressControl.enable();
        } else {
          this.player.controlBar.progressControl.disable();
        }
      }
    }
  }
};