"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 _dialog = _interopRequireDefault(require("../dialog"));

var _videoBox = _interopRequireDefault(require("./components/video-box"));

var _conditionRender = _interopRequireDefault(require("../condition-render"));

var _bsGlobal = require("../../utils/bs-global");

var _full = _interopRequireDefault(require("./img/full.svg"));

var _small = _interopRequireDefault(require("./img/small.svg"));

var _close = _interopRequireDefault(require("./img/close.png"));

require("./index.css");

var _getBSGlobal;

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

var preCls = "titaui-pc-video-modal";
var userId = (_getBSGlobal = (0, _bsGlobal.getBSGlobal)("loginUserInfo")) === null || _getBSGlobal === void 0 ? void 0 : _getBSGlobal.Id;
var VideoModal = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var videoSrc = props.videoSrc,
      title = props.title,
      videoPoster = props.videoPoster,
      visible = props.visible,
      onClose = props.onClose,
      onOpen = props.onOpen,
      _props$hasextraButton = props.hasextraButton,
      hasextraButton = _props$hasextraButton === void 0 ? true : _props$hasextraButton;

  var _useState = (0, _react.useState)(true),
      _useState2 = _slicedToArray(_useState, 2),
      smallVideoVisible = _useState2[0],
      setSmallVideoVisible = _useState2[1];

  var _useState3 = (0, _react.useState)("0"),
      _useState4 = _slicedToArray(_useState3, 2),
      time = _useState4[0],
      setTime = _useState4[1];

  var modalVideoRef = (0, _react.useRef)(document.createElement("div"));
  var smallVideoRef = (0, _react.useRef)(document.createElement("div")); // 关闭小窗口视频

  var handleClickCloseSmallVideo = function handleClickCloseSmallVideo() {
    setSmallVideoVisible(false); // @ts-ignore

    smallVideoRef.current.closeVideo();
    localStorage.setItem("hide_performance_small_video_".concat(userId), "true");
  }; // 打开dialog弹层 关闭小窗口


  var handleClickToFull = function handleClickToFull() {
    // @ts-ignore
    setTime(smallVideoRef.current.getCurrentTime());
    onOpen(); // @ts-ignore

    smallVideoRef.current.closeVideo();
    setSmallVideoVisible(false);
  }; // 关闭dialog弹层 打开小窗口


  var handleClickToSmall = function handleClickToSmall() {
    onClose(); // @ts-ignore

    setTime(modalVideoRef.current.getCurrentTime());
    setSmallVideoVisible(true);
  }; // 关闭过一次自动播放小窗口 之后不再出现


  (0, _react.useEffect)(function () {
    if (localStorage.getItem("hide_performance_small_video_".concat(userId)) === "true") {
      setSmallVideoVisible(false); // @ts-ignore

      smallVideoRef.current.closeVideo();
    }
  }, []);
  (0, _react.useEffect)(function () {
    if (visible) {
      setSmallVideoVisible(false);
    }
  }, [visible]); // *---提供给父组件调用(因为有时间的联动，视频默认从0开始需要手动调用此方法)---*

  var handleSetDefaultTime = function handleSetDefaultTime() {
    setTime("0");
  };

  (0, _react.useImperativeHandle)(ref, function () {
    return {
      setDefaultTime: handleSetDefaultTime
    };
  });
  (0, _react.useEffect)(function () {
    if (!hasextraButton) {
      setSmallVideoVisible(false);
    }
  }, []);
  return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_conditionRender["default"], {
    condition: !smallVideoVisible
  }, /*#__PURE__*/_react["default"].createElement(_dialog["default"], {
    visible: visible,
    title: title,
    onClose: onClose,
    style: {
      width: "840px",
      borderRadius: "24px"
    },
    noHeadLine: true,
    noFooterLine: true,
    noFooter: true,
    destroyOnClose: true
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__full")
  }, /*#__PURE__*/_react["default"].createElement(_videoBox["default"], {
    videoSrc: videoSrc,
    containerWidth: 840,
    ref: modalVideoRef,
    extraButton: hasextraButton ? /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__full-control"),
      onClick: handleClickToSmall
    }, /*#__PURE__*/_react["default"].createElement("img", {
      src: _small["default"],
      alt: "\u7F29\u5C0F"
    })) : "",
    videoPoster: videoPoster,
    time: time
  })))), /*#__PURE__*/_react["default"].createElement(_conditionRender["default"], {
    condition: smallVideoVisible
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__small")
  }, /*#__PURE__*/_react["default"].createElement(_videoBox["default"], {
    videoSrc: videoSrc,
    containerWidth: 360,
    hasHead: true,
    title: title,
    extraButton: /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__small-enlarge"),
      onClick: handleClickToFull
    }, /*#__PURE__*/_react["default"].createElement("img", {
      src: _full["default"],
      alt: "\u5168\u5C4F"
    })),
    videoPoster: videoPoster,
    headButton: /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__small-close"),
      onClick: handleClickCloseSmallVideo
    }, /*#__PURE__*/_react["default"].createElement("img", {
      src: _close["default"],
      alt: "\u5173\u95ED"
    })),
    ref: smallVideoRef,
    time: time
  }))));
});
var _default = VideoModal;
exports["default"] = _default;