import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _Upload from "@alifd/next/es/upload";
import _Button from "@alifd/next/es/button";
import _Message from "@alifd/next/es/message";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React, { Component, useEffect, useState } from "react";
import { ImageUploadQuery } from "@cverse/core-lib-vue-beta";
import { t } from "../../../src/locale";
var CVideoUploadSetter = function CVideoUploadSetter(props) {
  var imageUploadQuery = new ImageUploadQuery({
    appId: '617'
  });
  var value = props.value,
    onChange = props.onChange;
  var _useState = useState(value || []),
    videoUrl = _useState[0],
    setVideoUrl = _useState[1];
  var _useState2 = useState(false),
    loading = _useState2[0],
    setLoading = _useState2[1];
  var beforeUpload = /*#__PURE__*/function () {
    var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(file) {
      var maxSize;
      return _regeneratorRuntime.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.error(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 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(option) {
      var result;
      return _regeneratorRuntime.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.success(t('tool-video.key_YlYhgW2xsucc'));
            } else {
              _Message.success(t('tool-video.key_YlYhW2failed'));
            }
            setLoading(false);
          case 5:
          case "end":
            return _context2.stop();
        }
      }, _callee2);
    }));
    return _customRequest.apply(this, arguments);
  }
  useEffect(function () {
    onChange(videoUrl);
  }, [videoUrl]);
  return /*#__PURE__*/React.createElement(_Upload, {
    beforeUpload: beforeUpload,
    request: customRequest
  }, /*#__PURE__*/React.createElement(_Button, {
    loading: loading
  }, t('tool-video.key_YlYhgW2xUplo')));
};
var _default = /*#__PURE__*/function (_Component) {
  function _default() {
    return _Component.apply(this, arguments) || this;
  }
  _inheritsLoose(_default, _Component);
  var _proto = _default.prototype;
  _proto.render = function render() {
    return /*#__PURE__*/React.createElement(CVideoUploadSetter, this.props);
  };
  return _default;
}(Component);
export { _default as default };