'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 _reactDom = require('react-dom');

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

var _instance2 = _interopRequireDefault(_instance);

var _CameraRecorder = require('./CameraRecorder.js');

var _CameraRecorder2 = _interopRequireDefault(_CameraRecorder);

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 Camera = (0, _react.forwardRef)(function (_ref, ref) {
  var portal = _ref.portal,
      onHide = _ref.onHide,
      onRecord = _ref.onRecord,
      maxDuration = _ref.maxDuration,
      children = _ref.children,
      others = _objectWithoutProperties(_ref, ['portal', 'onHide', 'onRecord', 'maxDuration', 'children']);

  // context
  var context = (0, _react.useContext)(_instance2.default) || {};
  var locale = context.locale || function (key) {
    return key || '';
  };

  var _useState = (0, _react.useState)({}),
      successResult = _useState[0],
      setSuccessResult = _useState[1];

  var refEl = (0, _react.useRef)(null);
  var refElStart = (0, _react.useRef)(null);
  var refElStop = (0, _react.useRef)(null);
  var refElTime = (0, _react.useRef)(null);
  var refElTimeTarget = (0, _react.useRef)(null);
  var refElSuccess = (0, _react.useRef)(null);
  var refElError = (0, _react.useRef)(null);
  var refElErrorTarget = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return refEl.current;
  });
  function openCamera() {
    _CameraRecorder2.default.init({
      maxDuration: maxDuration,
      timer: refElTimeTarget.current,
      video: refEl.current,
      onOpened: function onOpened(e, stream) {
        if (refElStart.current) refElStart.current.style.display = 'block';
        // e.target.play();
      },
      onStop: function onStop(e) {
        if (refElStop.current) refElStop.current.style.display = 'none';
        if (refElTime.current) refElTime.current.style.display = 'none';
        if (refElSuccess.current) refElSuccess.current.style.display = 'block';
        setSuccessResult(e);
      },
      onError: function onError(e, err) {
        if (refElError.current) refElError.current.style.display = 'block';
        if (refElErrorTarget.current) refElErrorTarget.current.innerHTML = err.errMsg;
      }
    });
  }
  function start() {
    _CameraRecorder2.default.startRecord();
    refElStart.current.style.display = 'none';
    refElStop.current.style.display = 'block';
    refElTime.current.style.display = 'block';
  }
  function stop(e) {
    _CameraRecorder2.default.stopRecord();
  }

  function closeCamera(e) {
    _CameraRecorder2.default.stopRecord(null); // 关闭不走停止onStop回调
    if (onHide) onHide(e);
  }

  function success() {
    onRecord(successResult);
  }

  (0, _react.useEffect)(function () {
    openCamera();
  }, []);

  return (0, _reactDom.createPortal)(_react2.default.createElement(
    'div',
    _extends({ ref: ref }, others, { className: 'camera' + (others.className ? ' ' + others.className : '') }),
    _react2.default.createElement('video', { ref: refEl, width: '100%', height: '100%', autoPlay: '', playsInline: true, muted: true }),
    _react2.default.createElement(
      'div',
      { ref: refElStart, className: 'camera-recorder-start', onClick: start },
      _react2.default.createElement(
        'div',
        { className: 'camera-recorder-start-round' },
        _react2.default.createElement('div', { className: 'camera-recorder-start-dot' })
      )
    ),
    _react2.default.createElement(
      'div',
      { ref: refElStop, className: 'camera-recorder-stop', onClick: stop },
      _react2.default.createElement('div', { className: 'camera-recorder-stop-dot' })
    ),
    _react2.default.createElement(
      'div',
      { ref: refElSuccess, className: 'camera-success', onClick: success },
      _react2.default.createElement('div', { className: 'camera-success-icon' })
    ),
    _react2.default.createElement(
      'div',
      { ref: refElTime, className: 'camera-recorder-timer' },
      _react2.default.createElement('span', { className: 'camera-recorder-timer-dot' }),
      _react2.default.createElement(
        'span',
        { ref: refElTimeTarget, className: 'camera-recorder-timer-label' },
        '00:00'
      )
    ),
    _react2.default.createElement(
      'div',
      { ref: refElError, className: 'camera-recorder-error' },
      _react2.default.createElement('span', { ref: refElErrorTarget, className: 'camera-recorder-error-label' })
    ),
    _react2.default.createElement('div', { className: 'camera-close', onClick: closeCamera }),
    children
  ), portal || context.portal || document.getElementById('root') || document.body);
});

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