"use strict";
const React = require("react");
const classNames = require("../_util/classNames.js");
const index$1 = require("./list/index.js");
const uploader = require("./uploader.js");
const is = require("../_util/is.js");
const _interface = require("./interface.js");
const index = require("../ConfigProvider/index.js");
const omit = require("../_util/omit.js");
const useMergeProps = require("../_util/hooks/useMergeProps.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __rest = globalThis && globalThis.__rest || function(s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var processFile = function(fileList) {
  var res = {};
  if (!is.isArray(fileList)) {
    return res;
  }
  fileList.forEach(function(file, index2) {
    if (file.uid) {
      res[file.uid] = __assign({
        status: _interface.STATUS.success,
        percent: 100
      }, file);
    } else {
      var uid = "" + String(+new Date()) + index2;
      res[uid] = __assign(__assign({}, file), {
        uid,
        status: _interface.STATUS.success,
        percent: 100
      });
    }
  });
  return res;
};
var getFileList = function(uploadState) {
  return Object.keys(uploadState).map(function(x) {
    return uploadState[x];
  });
};
var defaultProps = {
  listType: "text",
  autoUpload: true,
  showUploadList: true,
  beforeUpload: function() {
    return true;
  }
};
var Upload = function(baseProps, ref) {
  var _a;
  var _b = React.useContext(index.ConfigContext), getPrefixCls = _b.getPrefixCls, componentConfig = _b.componentConfig, rtl = _b.rtl;
  var props = useMergeProps(baseProps, defaultProps, componentConfig === null || componentConfig === void 0 ? void 0 : componentConfig.Upload);
  var prefixCls = getPrefixCls("upload");
  var uploaderRef = React.useRef();
  var uploadState = React.useRef({});
  var _c = __read(React.useState("fileList" in props ? processFile(props.fileList) : "defaultFileList" in props ? processFile(props.defaultFileList) : {}), 2), innerUploadState = _c[0], setInnerUploadState = _c[1];
  uploadState.current = "fileList" in props ? processFile(props.fileList) : innerUploadState;
  var deleteUpload = function(file) {
    var obj = __assign({}, uploadState.current);
    delete obj[file.uid];
    if (!("fileList" in props)) {
      setInnerUploadState(obj);
    }
    props.onChange && props.onChange(getFileList(obj), file);
  };
  var uploadFile = function(file) {
    file && setTimeout(function() {
      uploaderRef.current && uploaderRef.current.upload(file);
    }, 0);
  };
  var reuploadFile = function(file) {
    uploaderRef.current && uploaderRef.current.reupload(file);
    props.onReupload && props.onReupload(file);
  };
  var removeFile = function(file) {
    if (file) {
      var onRemove = props.onRemove;
      Promise.resolve(is.isFunction(onRemove) ? onRemove(file) : onRemove).then(function(val) {
        if (val !== false) {
          uploaderRef.current && uploaderRef.current.abort(file);
          deleteUpload(file);
        }
      }).catch(function(e) {
        console.error(e);
      });
    }
  };
  var abortFile = function(file) {
    if (file) {
      uploaderRef.current && uploaderRef.current.abort(file);
    }
  };
  React.useImperativeHandle(ref, function() {
    return {
      submit: function(file) {
        var list = [];
        if (file) {
          list = [file];
        } else {
          list = getFileList(uploadState.current).filter(function(x) {
            return x.status === _interface.STATUS.init;
          });
        }
        list.forEach(function(x) {
          uploadFile(x);
        });
      },
      abort: function(file) {
        abortFile(file);
      },
      reupload: function(file) {
        reuploadFile(file);
      }
    };
  });
  var listType = props.listType, className = props.className, style = props.style, renderUploadItem = props.renderUploadItem, showUploadList = props.showUploadList, renderUploadList = props.renderUploadList, progressProps = props.progressProps, rest = __rest(props, ["listType", "className", "style", "renderUploadItem", "showUploadList", "renderUploadList", "progressProps"]);
  var fileList = getFileList(uploadState.current);
  var limit = is.isNumber(props.limit) ? {
    hideOnExceedLimit: true,
    maxCount: props.limit
  } : __assign({
    hideOnExceedLimit: true
  }, props.limit);
  var exceedLimit = limit.maxCount && limit.maxCount <= fileList.length;
  var disabledUploadDom = "disabled" in props ? props.disabled : !limit.hideOnExceedLimit && exceedLimit;
  var uploadDom = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
    ...__assign({}, omit(rest, ["disabled", "directory", "onReupload", "defaultFileList", "fileList", "autoUpload", "error", "action", "multiple", "name", "accept", "customRequest", "children", "autoUpload", "limit", "drag", "tip", "headers", "data", "withCredentials", "onChange", "onPreview", "onRemove", "onProgress", "onExceedLimit", "beforeUpload", "onDrop"]), {
      className: classNames(prefixCls, (_a = {}, _a[prefixCls + "-type-" + listType] = listType, _a[prefixCls + "-drag"] = props.drag, _a[prefixCls + "-disabled"] = disabledUploadDom, _a[prefixCls + "-hide"] = limit.hideOnExceedLimit && exceedLimit, _a[prefixCls + "-rtl"] = rtl, _a), className),
      style
    }),
    children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(uploader, {
      ...__assign({
        ref: uploaderRef
      }, props, {
        limit: limit.maxCount,
        hide: limit.hideOnExceedLimit && exceedLimit,
        disabled: disabledUploadDom,
        prefixCls,
        fileList,
        onProgress: function(file, e) {
          if (file) {
            if (!("fileList" in props)) {
              setInnerUploadState(function(v) {
                var _a2;
                return __assign(__assign({}, v), (_a2 = {}, _a2[file.uid] = file, _a2));
              });
            }
            props.onProgress && props.onProgress(file, e);
          }
        },
        onFileStatusChange: function(file) {
          var _a2;
          if (!("fileList" in props)) {
            setInnerUploadState(function(v) {
              var _a3;
              return __assign(__assign({}, v), (_a3 = {}, _a3[file.uid] = file, _a3));
            });
          }
          props.onChange && props.onChange(getFileList(__assign(__assign({}, uploadState.current), (_a2 = {}, _a2[file.uid] = file, _a2))), file);
        }
      })
    })
  });
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(emotionReactJsxRuntime_browser_esm.Fragment, {
    children: [listType !== "picture-card" && uploadDom, showUploadList && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$1.default, {
      progressProps,
      showUploadList,
      disabled: props.disabled,
      listType,
      fileList,
      renderUploadItem,
      renderUploadList,
      onUpload: uploadFile,
      onAbort: abortFile,
      onRemove: removeFile,
      onReupload: reuploadFile,
      onPreview: props.onPreview,
      prefixCls
    }), listType === "picture-card" && uploadDom, props.tip && listType === "picture-card" && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      className: prefixCls + "-trigger-tip",
      children: props.tip
    })]
  });
};
var UploadRef = React.forwardRef(Upload);
UploadRef.displayName = "Upload";
const Upload$1 = UploadRef;
module.exports = Upload$1;
//# sourceMappingURL=upload.js.map
