// 自定义图片上传组件
import React, { useContext, useRef } from 'react';
import { ContentUtils } from 'braft-utils';
import { message } from 'antd';
import { ConfigContext } from "../config-provider/context";
import Upload from "../upload";
import Iconfont from "../iconfont";
var MediaUploader = function MediaUploader(_ref) {
  var setEditorState = _ref.setEditorState,
    editorState = _ref.editorState;
  var _useContext = useContext(ConfigContext),
    uiLocale = _useContext.uiLocale;
  var messageRef = useRef();

  // 上传
  var onUpload = function onUpload(_, file) {
    if (!file) return;
    if (!messageRef.current) {
      messageRef.current = message.loading(uiLocale.global.uploading, 0);
    }
    if (file.status === 'done') {
      var _file$response;
      messageRef.current = null;
      message.success(uiLocale.global.uploadSuccess);
      setEditorState(ContentUtils.insertMedias(editorState, [{
        type: file.type.includes('audio') ? 'AUDIO' : 'VIDEO',
        url: (_file$response = file.response) === null || _file$response === void 0 ? void 0 : _file$response.url,
        name: file.name
      }]));
    }
  };
  return /*#__PURE__*/React.createElement(Upload, {
    accept: "video/*,audio/*",
    onChange: onUpload,
    showUploadList: false
  }, /*#__PURE__*/React.createElement("button", {
    type: "button",
    "data-title": uiLocale.BraftEditor.insertMedia,
    className: "control-item button"
  }, /*#__PURE__*/React.createElement(Iconfont, {
    type: "icon-ehr-ui-yinpinshipin",
    style: {
      fontSize: 16
    }
  })));
};
export default MediaUploader;