import { _ as _extends } from '../_rollupPluginBabelHelpers-736b301a.js';
import React__default from 'react';
import { message, Upload as Upload$1, Button } from 'antd';
import { LMConfig } from '@zenview/micro-basic-library';
import IconFont from '../IconFont/index.js';
import '@zenview/micro-utils';

var _LMConfig$BSConfig;

/* 
  配置参数
  {
    disabled,
    expiretype : 周期类型，0表示永久，1表示7天，2表示30天，3表示90天，默认为0
    name:  发到后台的文件参数名,默认file
  }
*/

// 默认上传的格式
const ACCEPT_FILE = 'image/png,image/jpeg,image/jpg';
const MAX_SIZE = (LMConfig === null || LMConfig === void 0 || (_LMConfig$BSConfig = LMConfig.BSConfig) === null || _LMConfig$BSConfig === void 0 ? void 0 : _LMConfig$BSConfig.maxSize) || 20;
class AntUpload extends React__default.Component {
  constructor(...args) {
    super(...args);
    // file转化为本地base64
    this.uploadLocal = async file => {
      // eslint-disable-next-line no-unused-vars
      const {
        customUpload,
        toBase64 = true
      } = this.props;
      file.uploadType = 'local';
      // if(toBase64) {
      //   const base64 = await Utils.fileToBase64(file);
      //   file.url = base64;
      // }
      customUpload && customUpload(file);
    };
    // 图片上传到远程服务器
    this.uploadRemote = file => {
      const {
        customUpload
      } = this.props;
      customUpload && customUpload(file);
    };
    /** 自定义上传方法
     *
     */
    this.customUpload = async ({
      file
    }) => {
      // eslint-disable-next-line no-unused-vars
      const {
        uploadType = 'remote',
        imageCompress = true
      } = this.props;
      let newFile = file;
      // if (file.type !== "image/svg+xml" && imageCompress) {
      //   newFile = await Utils.imageCompress(file);
      // }
      if (uploadType === 'remote') {
        this.uploadRemote(newFile);
      } else {
        this.uploadLocal(newFile);
      }
    };
    /** 上传之前的钩子函数,做一些上传校验
     * @return 返回false时停止上传，返回true则执行上传（customUpload）
     */
    this.beforeUpload = file => {
      const {
        maxSize = MAX_SIZE,
        accept = ACCEPT_FILE,
        typeErrorMessage,
        sizeErrorMessage,
        beforeUpload
      } = this.props;
      const acceptTypes = accept.split(',');
      const acceptable = acceptTypes.includes(file.type);
      if (!acceptable) {
        message.error(typeErrorMessage || '图片格式必须为JPG或者PNG!');
      }
      const limitSize = maxSize ? file.size / 1024 / 1024 < maxSize : true;
      if (!limitSize) {
        message.error(sizeErrorMessage || `图片大小不能超过${maxSize}M!`);
      }
      const fileSize = file.size;
      if (!fileSize) {
        message.error(`${file.name}:文件错误!`);
      }
      if (acceptable && limitSize && fileSize) {
        beforeUpload && beforeUpload(file);
      }
      return acceptable && limitSize;
    };
  }
  render() {
    const {
      className = '',
      accept = ACCEPT_FILE,
      children = null,
      ...rest
    } = this.props;
    const uploadProps = {
      accept,
      name: 'file',
      multiple: false,
      showUploadList: false,
      beforeUpload: this.beforeUpload,
      customRequest: this.customUpload
    };
    return /*#__PURE__*/React__default.createElement(Upload$1, _extends({
      className: `lm-upload-wrapper ${className}`
    }, uploadProps, rest), children);
  }
}

/**
 * 按钮上传
 * 示例: 人脸图库以图搜图按钮
 */
const UploadButton = ({
  icon = "icon-L_Bar_AddImg",
  text,
  ...rest
}) => /*#__PURE__*/React__default.createElement(Button, rest, icon && /*#__PURE__*/React__default.createElement(IconFont, {
  type: icon
}), text);

const Upload = {
  AntUpload,
  UploadButton
};

export { Upload as default };
//# sourceMappingURL=index.js.map
