"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _upload = _interopRequireDefault(require("@alifd/next/lib/upload"));
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _message = _interopRequireDefault(require("@alifd/next/lib/message"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = _interopRequireWildcard(require("react"));
var _coreLibVueBeta = require("@cverse/core-lib-vue-beta");
var _locale = require("../../../src/locale");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
var CVideoUploadSetter = function CVideoUploadSetter(props) {
  var imageUploadQuery = new _coreLibVueBeta.ImageUploadQuery({
    appId: '617'
  });
  var value = props.value,
    onChange = props.onChange;
  var _useState = (0, _react.useState)(value || []),
    videoUrl = _useState[0],
    setVideoUrl = _useState[1];
  var _useState2 = (0, _react.useState)(false),
    loading = _useState2[0],
    setLoading = _useState2[1];
  var beforeUpload = /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee(file) {
      var maxSize;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            setLoading(true);
            maxSize = 5 * 1024 * 1024; // 5MB
            if (file.size > maxSize) {
              _message["default"].error((0, _locale.t)('tool-video.key_YlYhgW2xUpfe'));
            }
          case 3:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    return function beforeUpload(_x) {
      return _ref.apply(this, arguments);
    };
  }();
  function customRequest(_x2) {
    return _customRequest.apply(this, arguments);
  }
  function _customRequest() {
    _customRequest = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2(option) {
      var result;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            _context2.next = 2;
            return imageUploadQuery.uploadImage(option.file);
          case 2:
            result = _context2.sent;
            if (result) {
              setVideoUrl(result);
              _message["default"].success((0, _locale.t)('tool-video.key_YlYhgW2xsucc'));
            } else {
              _message["default"].success((0, _locale.t)('tool-video.key_YlYhW2failed'));
            }
            setLoading(false);
          case 5:
          case "end":
            return _context2.stop();
        }
      }, _callee2);
    }));
    return _customRequest.apply(this, arguments);
  }
  (0, _react.useEffect)(function () {
    onChange(videoUrl);
  }, [videoUrl]);
  return /*#__PURE__*/_react["default"].createElement(_upload["default"], {
    beforeUpload: beforeUpload,
    request: customRequest
  }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
    loading: loading
  }, (0, _locale.t)('tool-video.key_YlYhgW2xUplo')));
};
var _default = exports["default"] = /*#__PURE__*/function (_Component) {
  function _default() {
    return _Component.apply(this, arguments) || this;
  }
  (0, _inheritsLoose2["default"])(_default, _Component);
  var _proto = _default.prototype;
  _proto.render = function render() {
    return /*#__PURE__*/_react["default"].createElement(CVideoUploadSetter, this.props);
  };
  return _default;
}(_react.Component);