'use strict';

exports.__esModule = true;

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _Bridge = require('./../Bridge');

var _Bridge2 = _interopRequireDefault(_Bridge);

var _Photos = require('./../Photos');

var _Photos2 = _interopRequireDefault(_Photos);

var _VideoFull = require('./../VideoFull');

var _VideoFull2 = _interopRequireDefault(_VideoFull);

var _Camera = require('./../Camera');

var _Camera2 = _interopRequireDefault(_Camera);

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

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

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

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

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


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


  function click(e, item, index) {
    if (onClick) {
      onClick(e, item, index);
      return;
    }
    if (!item.src) {
      _Bridge2.default.showToast('没有src', { mask: false });
    }
    if (_Bridge2.default.platform === 'wechat' || _Bridge2.default.platform === 'wework' || _Bridge2.default.platform === 'wq') {
      _Bridge2.default.perviewFile(item.src);
    } else {
      // h5预览
      setPreivewItem(item);
    }
  }
  // 关闭h5预览
  function closePreview() {
    setPreivewItem(null);
  }
  // 录制视频
  function choose(e) {
    if (_Bridge2.default.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 _react2.default.createElement(
    _react.Fragment,
    null,
    _react2.default.createElement(_Photos2.default, _extends({
      ref: ref
    }, others, {
      onClick: click,
      onChoose: choose,
      type: 'video'
    })),
    previewItem && _react2.default.createElement(_VideoFull2.default, {
      ref: refVideoFull,
      portal: document.getElementById('root') || document.body,
      poster: previewItem.thumb,
      src: previewItem.src,
      autoPlay: true,
      bar: _react2.default.createElement('div', { className: 'videofull-close', onClick: closePreview })
    }),
    showRecord && _react2.default.createElement(_Camera2.default, {
      onHide: function onHide() {
        return setShowRecord(false);
      },
      onRecord: record
    })
  );
});

exports.default = Videos;
module.exports = exports['default'];