"use strict";

exports.__esModule = true;
exports["default"] = void 0;

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

var _Bridge = _interopRequireDefault(require("./../Bridge"));

var _Photos = _interopRequireDefault(require("./../Photos"));

var _VideoFull = _interopRequireDefault(require("./../VideoFull"));

var _instance = _interopRequireDefault(require("./../Context/instance.js"));

var _excluded = ["onClick", "showPlay", "preview", "onPreviewHide", "routePath", "videoFullProps"];

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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

// 视频栅格
var Videos = (0, _react.forwardRef)(function (_ref, ref) {
  var onClick = _ref.onClick,
      _ref$showPlay = _ref.showPlay,
      showPlay = _ref$showPlay === void 0 ? false : _ref$showPlay,
      _ref$preview = _ref.preview,
      preview = _ref$preview === void 0 ? true : _ref$preview,
      onPreviewHide = _ref.onPreviewHide,
      _ref$routePath = _ref.routePath,
      routePath = _ref$routePath === void 0 ? 'componentPage=1' : _ref$routePath,
      _ref$videoFullProps = _ref.videoFullProps,
      videoFullProps = _ref$videoFullProps === void 0 ? {} : _ref$videoFullProps,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  // context
  var context = (0, _react.useContext)(_instance["default"]) || {};

  var locale = context.locale || function (remark) {
    return remark || '';
  };

  var refVideoFull = (0, _react.useRef)(null); // h5预览

  var _useState = (0, _react.useState)(null),
      previewItem = _useState[0],
      setPreivewItem = _useState[1]; // 点击预览


  function handleClick(e, src, options, index) {
    var item = options[0];

    if (onClick) {
      onClick(e, src, options, index);
    }

    if (!preview) return;

    if (!item.src) {
      _Bridge["default"].showToast(locale('没有参数src', 'no_param_src'), {
        mask: false
      });
    } // 增加历史记录


    _Bridge["default"].addHistoryBack(function () {
      closePreview('history');
    }, "" + routePath); // preview回调


    if (typeof preview === 'function') preview(e, src, options, index); // 客户端预览视频有问题, 所以使用h5预览

    setPreivewItem(item);
  } // 关闭h5预览, [closeType] history: 地址栏返回; 其它: 主动隐藏;


  function closePreview(closeType) {
    if (window.location.href.indexOf(routePath) !== -1) {
      window.history.go(-1);
    } else {
      setPreivewItem(null);
      if (onPreviewHide) onPreviewHide(closeType);
    }
  }
  /*
  // h5录相
  const [showRecord, setShowRecord] = useState(false);
  // 录制视频
  function choose (e) {
  if (Bridge.platform !== 'wq') {
    setShowRecord(true);
    return;
  }
  if (onChoose) {
    onChoose(e);
  }
  }
  // h5录相
  function record (e) {
  if (onChoose) {
    onChoose(e);
  }
  }
  */


  return /*#__PURE__*/_react["default"].createElement(_react.Fragment, null, /*#__PURE__*/_react["default"].createElement(_Photos["default"], _extends({
    ref: ref
  }, others, {
    onClick: handleClick // onChoose={choose}
    ,
    type: "video",
    preview: false,
    children: showPlay ?
    /*#__PURE__*/
    // 视频播放图标
    _react["default"].createElement("div", {
      className: "photos-item-video"
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "photos-item-video-icon"
    })) : null
  })), previewItem && /*#__PURE__*/_react["default"].createElement(_VideoFull["default"], _extends({
    ref: refVideoFull,
    portal: document.getElementById('root') || document.body,
    poster: previewItem.thumb,
    src: previewItem.src,
    autoPlay: true,
    bar: /*#__PURE__*/_react["default"].createElement("div", {
      className: "videofull-close",
      onClick: closePreview
    })
  }, videoFullProps)));
});
var _default = Videos;
exports["default"] = _default;
module.exports = exports.default;