"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");

var _video = _interopRequireDefault(require("./lib/video.js"));

require("./lib/video-js.min.css");

require("@videojs/themes/dist/fantasy/index.css");

require("@tower1229/videojs-plugin-marker/dist/style.css");

require("@tower1229/videojs-plugin-source-switcher/dist/style.css");

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

window.videojs = _video.default;

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

var loadPlugins = Promise.all([Promise.resolve().then(function () {
  return _interopRequireWildcard(require('@tower1229/videojs-plugin-marker'));
}), Promise.resolve().then(function () {
  return _interopRequireWildcard(require('@tower1229/videojs-plugin-source-switcher'));
})]);

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__ = [];
var _default2 = {
  _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: (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];
    }
  },
  watch: {
    seekAble: {
      handler: function handler() {
        this.setControlBar();
      },
      immediate: true
    }
  },
  mounted: function mounted() {
    var _this2 = this;

    // 默认配置
    var options = (0, _extends2.default)({
      playbackRates: [0.5, 1, 1.5, 2],
      language: 'zh-CN',
      fluid: true
    }, this.options);
    var player = (0, _video.default)(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();
        }
      }
    }
  }
};
exports.default = _default2;