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

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

import React, { forwardRef, useRef, Fragment, useState } from 'react';
import Bridge from './../Bridge';
import Photos from './../Photos';
import VideoFull from './../VideoFull'; // import Camera from './../Camera'

import locale from './../locale'; // 预览项

var previewIndex = null; // 视频栅格

var Videos = 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,
      _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);

  var videoFullRef = useRef(null); // 预览项

  var _useState = 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.showToast(locale('没有参数src', 'no_param_src'), {
        mask: false
      });
    } // 增加历史记录


    Bridge.addHistoryBack(function () {
      handleClosePreview(e);
    }, "" + routePath); // 预览回调

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


    previewItem = item;
    setPreivewItem(item);
  } // 关闭h5预览


  function handleClosePreview(e) {
    // 已经返回, 路由却没变
    if (window.location.href.indexOf(routePath) !== -1) {
      window.history.go(-1);
      return;
    } // 预览回调


    if (typeof preview === 'function') {
      e.visible = false;
      preview(e, previewItem.src, [previewItem], previewIndex);
    } // 隐藏预览


    previewItem = null;
    setPreivewItem(null);
  }
  /*
  // 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.createElement(Fragment, null, /*#__PURE__*/React.createElement(Photos, _extends({
    ref: ref
  }, others, {
    onClick: handleClick // onChoose={choose}
    ,
    type: "video",
    preview: false,
    children: showPlay ?
    /*#__PURE__*/
    // 视频播放图标
    React.createElement("div", {
      className: "photos-item-video"
    }, /*#__PURE__*/React.createElement("div", {
      className: "photos-item-video-icon"
    })) : null
  })), previewItem && /*#__PURE__*/React.createElement(VideoFull, _extends({
    ref: videoFullRef,
    portal: document.getElementById('root') || document.body,
    poster: previewItem.thumb,
    src: previewItem.src,
    autoPlay: true,
    bar: /*#__PURE__*/React.createElement("div", {
      className: "videofull-close",
      onClick: handleClosePreview
    })
  }, videoFullProps)));
});
export default Videos;