var _excluded = ["fileProps", "list", "uploadPosition", "upload", "uploading", "onBeforeChoose", "onChoose", "onFileChange", "onDelete", "onReUpload", "onPreview", "disabled", "children"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
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; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useImperativeHandle, forwardRef, useState, useRef } from 'react';
import Choose from "./Choose";
import List from "./List";
var Upload = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var fileProps = _ref.fileProps,
    list = _ref.list,
    _ref$uploadPosition = _ref.uploadPosition,
    uploadPosition = _ref$uploadPosition === void 0 ? 'end' : _ref$uploadPosition,
    upload = _ref.upload,
    uploading = _ref.uploading,
    onBeforeChoose = _ref.onBeforeChoose,
    onChoose = _ref.onChoose,
    onFileChange = _ref.onFileChange,
    onDelete = _ref.onDelete,
    onReUpload = _ref.onReUpload,
    onPreview = _ref.onPreview,
    disabled = _ref.disabled,
    children = _ref.children,
    props = _objectWithoutProperties(_ref, _excluded);
  // 因为在click事件内改变数据的可能性, 所以更新句柄, 防止synchronization模式读取创建时的状态
  var onChooseRef = useRef();
  var onFileChangeRef = useRef();
  onChooseRef.current = onChoose;
  onFileChangeRef.current = onFileChange;

  // 根节点
  var rootRef = useRef(null);
  var _useState = useState(0),
    _useState2 = _slicedToArray(_useState, 2),
    _updateStatus = _useState2[0],
    setUpdateStatus = _useState2[1];

  // Expose
  useImperativeHandle(ref, function () {
    return {
      rootDOM: rootRef.current,
      getRootDOM: function getRootDOM() {
        return rootRef.current;
      },
      updateStatus: function updateStatus() {
        setUpdateStatus(_updateStatus + 1);
      }
    };
  });

  // 上传node
  function getChooseNode(props) {
    return /*#__PURE__*/React.createElement(Choose, _extends({
      disabled: disabled
      // file框属性
      ,
      fileProps: fileProps
      // 上传DOM
      ,
      upload: upload
      // 上传中DOM
      ,
      uploading: uploading
      // Choose events
      ,
      onChoose: onChooseRef.current,
      onBeforeChoose: onBeforeChoose,
      onFileChange: onFileChangeRef.current
    }, props));
  }

  // Custom render
  if (children) {
    return /*#__PURE__*/React.createElement("div", _extends({
      ref: rootRef
    }, props, {
      className: "upload".concat(props.className ? ' ' + props.className : '')
    }), getChooseNode({
      className: 'image-choose-hidden'
    }), children);
  }
  return /*#__PURE__*/React.createElement("div", _extends({
    ref: rootRef
  }, props, {
    className: "upload".concat(props.className ? ' ' + props.className : '')
  }), uploadPosition === 'start' && (onChoose || onFileChange) && getChooseNode(), /*#__PURE__*/React.createElement(List, {
    list: list,
    uploading: uploading
    // Events
    ,
    onDelete: onDelete,
    onReUpload: onReUpload,
    onPreview: onPreview
  }), uploadPosition === 'end' && (onChoose || onFileChange) && getChooseNode());
});
export default Upload;