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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } 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';

var Videos = forwardRef(function (_ref, ref) {
  var onClick = _ref.onClick,
      onChoose = _ref.onChoose,
      others = _objectWithoutProperties(_ref, ['onClick', 'onChoose']);

  var refVideoFull = useRef(null);
  // h5预览

  var _useState = useState(null),
      previewItem = _useState[0],
      setPreivewItem = _useState[1];
  // h5录相


  var _useState2 = useState(false),
      showRecord = _useState2[0],
      setShowRecord = _useState2[1];
  // 点击预览


  function click(e, item, index) {
    if (onClick) {
      onClick(e, item, index);
      return;
    }
    if (!item.src) {
      Bridge.showToast('没有src', { mask: false });
    }
    if (Bridge.platform === 'wechat' || Bridge.platform === 'wework' || Bridge.platform === 'wq') {
      Bridge.perviewFile(item.src);
    } else {
      // h5预览
      setPreivewItem(item);
    }
  }
  // 关闭h5预览
  function closePreview() {
    setPreivewItem(null);
  }
  // 录制视频
  function choose(e) {
    if (Bridge.platform !== 'wq') {
      setShowRecord(true);
      return;
    }
    if (onChoose) {
      onChoose(e);
    }
  }
  // h5录相
  function record(e, file) {
    var event = {
      target: file,
      targetType: 'video',
      type: 'video/mp4'
    };
    if (onChoose) {
      onChoose(event);
    }
  }
  return React.createElement(
    Fragment,
    null,
    React.createElement(Photos, _extends({
      ref: ref
    }, others, {
      onClick: click,
      onChoose: choose,
      type: 'video'
    })),
    previewItem && React.createElement(VideoFull, {
      ref: refVideoFull,
      portal: document.getElementById('root') || document.body,
      poster: previewItem.thumb,
      src: previewItem.src,
      autoPlay: true,
      bar: React.createElement('div', { className: 'videofull-close', onClick: closePreview })
    }),
    showRecord && React.createElement(Camera, {
      onHide: function onHide() {
        return setShowRecord(false);
      },
      onRecord: record
    })
  );
});

export default Videos;