"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _ahooks = require("ahooks");

var _utils = require("@vtx/utils");

var _antd = require("antd");

var _propTypes = _interopRequireDefault(require("prop-types"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function getPlaybackUrl() {
  var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return new Promise(function (resolve) {
    _utils.request.get('/cloud/vis/base/np/artemis/playbackUrl.smvc', {
      body: params
    }).then(function (res) {
      if ((res === null || res === void 0 ? void 0 : res.result) == 0) {
        resolve(res);
      }
    });
  });
}

function loadJs(path) {
  if (!path || path.length === 0) {
    throw new Error('argument "path" is required !');
  }

  var head = document.getElementsByTagName('head')[0];
  var script = document.createElement('script');
  script.src = path;
  script.type = 'text/javascript';
  head.appendChild(script);
}

function isSupportH5() {
  return getBrowseVersion().indexOf('msie') != -1 ? false : true;
}

function getBrowseVersion() {
  var userAgent = navigator.userAgent.toLowerCase();
  var browser = userAgent.match(/(firefox|chrome|safari|opera|msie)/);
  var browserId = 'msie';
  var browserVersion = '';
  var isIE11 = userAgent.toLowerCase().indexOf('trident') > -1 && userAgent.indexOf('rv') > -1;

  if (isIE11) {
    browserId = 'msie';
    browserVersion = '11.0';
  } else {
    if (browser.length >= 2) {
      browserId = browser[1];
    } else {
      browserId = 'msie';
    }

    browserVersion = (userAgent.match(new RegExp('.+(?:version)[/: ]([\\d.]+)')) || userAgent.match(new RegExp('(?:' + browserId + ')[/: ]([\\d.]+)')) || [0, '0'])[1];
  }

  return browserId + browserVersion;
}

var winList = [],
    videoObj = [];

function History(props) {
  var _useSetState = (0, _ahooks.useSetState)({
    winIndex: 0,
    videoId: props.videoId || 'videoHistoryPlay',
    winNum: props.winNum || 1
  }),
      _useSetState2 = _slicedToArray(_useSetState, 2),
      state = _useSetState2[0],
      setState = _useSetState2[1];

  props.historyVideoPlay$ && props.historyVideoPlay$.useSubscription(function (data) {
    play(data);
  });

  function initWindow(num) {
    var winNum = num || state.winNum;
    var wins = [],
        style = {
      height: '100%',
      width: '100%'
    };

    if (winNum == 4) {
      style = {
        height: '50%',
        width: '50%'
      };
    } else if (winNum == 9) {
      style = {
        height: '33.33%',
        width: '33.33%'
      };
    }

    var _loop = function _loop(index) {
      wins.push( /*#__PURE__*/_react["default"].createElement("div", {
        key: "".concat(state.videoId).concat(index),
        id: "".concat(state.videoId).concat(index),
        className: "videoItem ".concat(state.winIndex === index ? 'checked' : ''),
        style: style,
        onClick: function onClick() {
          setState({
            winIndex: index
          });
        }
      }));
    };

    for (var index = 0; index < winNum; index++) {
      _loop(index);
    }

    return wins;
  }

  function play(obj) {
    videoObj[state.wndIndex] && videoObj[state.wndIndex].destroy();
    obj && getPlaybackUrl({
      channelNum: obj.channelNum,
      deviceId: obj.deviceId,
      beginTime: obj.startTime,
      endTime: obj.endTime,
      linkMode: 'rtsp',
      tenantId: (0, _utils.getUrlParam)('tenantId')
    }).then(function (res) {
      if (!!res && !res.result) {
        var data = res.data;

        if (data.url && data.url.length > 0) {
          winList[state.winIndex] = data; // eslint-disable-next-line no-undef

          videoObj[state.winIndex] = initPlayer(state.videoId + state.winIndex, data.url, false);

          if (state.winIndex === state.winNum - 1) {
            setState({
              winIndex: 0
            });
          } else {
            setState({
              winIndex: state.winIndex + 1
            });
          }
        } else {
          _antd.message.warn('未获取到监控点流地址');
        }
      } else {
        _antd.message.warn('未获取到监控点流地址');
      }
    });
  }

  function initJS() {
    if (isSupportH5()) {
      Promise.all([loadJs('./resources/lib/video/h5/flv.min.js'), loadJs('./resources/lib/video/h5/DPlayer.min.js'), loadJs('./resources/lib/video/h5/vtx-player-h5.js')]).then(function () {
        play();
      });
    } else {
      Promise.all([loadJs('./resources/lib/video/flash/ckplayer.min.js'), loadJs('./resources/lib/video/flash/vtx-player-flash.js')]).then(function () {
        play();
      });
    }
  }

  (0, _react.useEffect)(function () {
    initJS();
  }, []);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "videoList"
  }, initWindow());
}

History.propTypes = {
  videoId: _propTypes["default"].any,
  winNum: _propTypes["default"].any,
  historyVideoPlay$: _propTypes["default"].any
};
var _default = History;
exports["default"] = _default;
//# sourceMappingURL=index.js.map
