function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import React, { useCallback, useRef } from 'react';
import classNames from 'classnames';
import IconFont from "../iconfont";
import { getText } from "../../locales";
import "./PisellImport.less";

/** 默认导入图标（upload-cloud，与 Export 的 download-cloud 对应） */
var DEFAULT_ICON_TYPE = 'pisell2-upload-cloud-01';
var BYTES_PER_MB = 1024 * 1024;

/**
 * PisellImport 导入触发器组件
 *
 * @description 点击后打开文件选择，支持 accept、multiple、maxSize 校验，选择完成后回调 onImport(files)
 */
export function PisellImport(props) {
  var _props$variant = props.variant,
    variant = _props$variant === void 0 ? 'button' : _props$variant,
    _props$size = props.size,
    size = _props$size === void 0 ? 'medium' : _props$size,
    _props$type = props.type,
    type = _props$type === void 0 ? 'default' : _props$type,
    _props$disabled = props.disabled,
    disabled = _props$disabled === void 0 ? false : _props$disabled,
    label = props.label,
    icon = props.icon,
    accept = props.accept,
    _props$multiple = props.multiple,
    multiple = _props$multiple === void 0 ? false : _props$multiple,
    maxSize = props.maxSize,
    onImport = props.onImport,
    onError = props.onError,
    className = props.className,
    style = props.style;
  var inputRef = useRef(null);
  var displayLabel = label !== undefined ? label : getText('pisell-import-button-label');
  var iconNode = icon !== undefined ? icon : /*#__PURE__*/React.createElement(IconFont, {
    type: DEFAULT_ICON_TYPE,
    className: "pisell-import-icon",
    style: {
      fontSize: 20,
      width: 20,
      height: 20,
      display: 'inline-flex',
      alignItems: 'center',
      justifyContent: 'center'
    }
  });
  var handleClick = useCallback(function (e) {
    var _inputRef$current;
    if (disabled) return;
    (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || _inputRef$current.click();
  }, [disabled]);
  var handleFileChange = useCallback(function (e) {
    var files = e.target.files;
    if (!files || files.length === 0) return;
    var fileList = Array.from(files);
    var maxBytes = maxSize != null ? maxSize * BYTES_PER_MB : undefined;
    if (maxBytes != null) {
      var _iterator = _createForOfIteratorHelper(fileList),
        _step;
      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var file = _step.value;
          if (file.size > maxBytes) {
            var rawMsg = getText('pisell-import-error-max-size');
            var message = typeof rawMsg === 'string' ? rawMsg.replace(/\{\{maxSize\}\}/g, String(maxSize)) : "\u6587\u4EF6 ".concat(file.name, " \u8D85\u8FC7 ").concat(maxSize, "MB \u9650\u5236");
            onError === null || onError === void 0 || onError({
              type: 'MAX_SIZE',
              message: message,
              file: file
            });
            e.target.value = '';
            return;
          }
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
    }
    onImport === null || onImport === void 0 || onImport(fileList, e);
    e.target.value = '';
  }, [maxSize, onImport, onError]);
  var rootClass = classNames('pisell-import', "pisell-import--".concat(variant), "pisell-import--".concat(size), "pisell-import--".concat(type), disabled && 'pisell-import--disabled', className);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("input", {
    ref: inputRef,
    type: "file",
    accept: accept,
    multiple: multiple,
    onChange: handleFileChange,
    style: {
      display: 'none'
    },
    tabIndex: -1,
    "aria-hidden": true
  }), /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: rootClass,
    style: style,
    disabled: disabled,
    onClick: handleClick,
    "aria-label": typeof displayLabel === 'string' ? displayLabel : undefined
  }, /*#__PURE__*/React.createElement("span", {
    className: "pisell-import-icon-wrap"
  }, iconNode), variant === 'button' && /*#__PURE__*/React.createElement("span", {
    className: "pisell-import-label"
  }, displayLabel)));
}