import Q from "../../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import * as a from "react";
import { r as X } from "../../../react-dom/index.mjs";
import M from "../../../classnames/index.mjs";
import Y from "../../../rc-upload/es/Upload.mjs";
import Se from "../../../rc-util/es/hooks/useMergedState.mjs";
import { devUseWarning as $e } from "../_util/warning.mjs";
import "../config-provider/index.mjs";
import Re from "../config-provider/DisabledContext.mjs";
import je from "../locale/en_US.mjs";
import Pe from "./style/index.mjs";
import Te from "./UploadList/index.mjs";
import { file2Obj as _, updateFileList as D, getFileItem as V, removeFileItem as Be } from "./utils.mjs";
import { ConfigContext as Me } from "../config-provider/context.mjs";
import Ve from "../locale/useLocale.mjs";
var Ae = globalThis && globalThis.__awaiter || function(u, O, m, w) {
  function I(p) {
    return p instanceof m ? p : new m(function(d) {
      d(p);
    });
  }
  return new (m || (m = Promise))(function(p, d) {
    function S(c) {
      try {
        f(w.next(c));
      } catch (F) {
        d(F);
      }
    }
    function $(c) {
      try {
        f(w.throw(c));
      } catch (F) {
        d(F);
      }
    }
    function f(c) {
      c.done ? p(c.value) : I(c.value).then(S, $);
    }
    f((w = w.apply(u, O || [])).next());
  });
};
const N = `__LIST_IGNORE_${Date.now()}__`, ze = (u, O) => {
  const {
    fileList: m,
    defaultFileList: w,
    onRemove: I,
    showUploadList: p = !0,
    listType: d = "text",
    onPreview: S,
    onDownload: $,
    onChange: f,
    onDrop: c,
    previewFile: F,
    disabled: R,
    locale: ee,
    iconRender: te,
    isImageUrl: oe,
    progress: ne,
    prefixCls: re,
    className: se,
    type: ae = "select",
    children: C,
    style: ie,
    itemRender: le,
    maxCount: E,
    data: ce = {},
    multiple: de = !1,
    action: pe = "",
    accept: ue = "",
    supportServerRender: me = !0,
    rootClassName: fe
  } = u, ve = a.useContext(Re), h = R ?? ve, [l, ge] = Se(w || [], {
    value: m,
    postState: (e) => e ?? []
  }), [we, he] = a.useState("drop"), U = a.useRef(null);
  if (process.env.NODE_ENV !== "production") {
    const e = $e("Upload");
    process.env.NODE_ENV !== "production" && e("fileList" in u || !("value" in u), "usage", "`value` is not a valid prop, do you mean `fileList`?"), e.deprecated(!("transformFile" in u), "transformFile", "beforeUpload");
  }
  a.useMemo(() => {
    const e = Date.now();
    (m || []).forEach((o, r) => {
      !o.uid && !Object.isFrozen(o) && (o.uid = `__AUTO__${e}_${r}__`);
    });
  }, [m]);
  const L = (e, o, r) => {
    let t = Q(o), n = !1;
    E === 1 ? t = t.slice(-1) : E && (n = t.length > E, t = t.slice(0, E)), X.flushSync(() => {
      ge(t);
    });
    const i = {
      file: e,
      fileList: t
    };
    r && (i.event = r), (!n || // We should ignore event if current file is exceed `maxCount`
    t.some((v) => v.uid === e.uid)) && X.flushSync(() => {
      f == null || f(i);
    });
  }, ye = (e, o) => Ae(void 0, void 0, void 0, function* () {
    const {
      beforeUpload: r,
      transformFile: t
    } = u;
    let n = e;
    if (r) {
      const i = yield r(e, o);
      if (i === !1)
        return !1;
      if (delete e[N], i === N)
        return Object.defineProperty(e, N, {
          value: !0,
          configurable: !0
        }), !1;
      typeof i == "object" && i && (n = i);
    }
    return t && (n = yield t(n)), n;
  }), A = (e) => {
    const o = e.filter((n) => !n.file[N]);
    if (!o.length)
      return;
    const r = o.map((n) => _(n.file));
    let t = Q(l);
    r.forEach((n) => {
      t = D(n, t);
    }), r.forEach((n, i) => {
      let v = n;
      if (o[i].parsedFile)
        n.status = "uploading";
      else {
        const {
          originFileObj: b
        } = n;
        let g;
        try {
          g = new File([b], b.name, {
            type: b.type
          });
        } catch {
          g = new Blob([b], {
            type: b.type
          }), g.name = b.name, g.lastModifiedDate = /* @__PURE__ */ new Date(), g.lastModified = (/* @__PURE__ */ new Date()).getTime();
        }
        g.uid = n.uid, v = g;
      }
      L(v, t);
    });
  }, z = (e, o, r) => {
    try {
      typeof e == "string" && (e = JSON.parse(e));
    } catch {
    }
    if (!V(o, l))
      return;
    const t = _(o);
    t.status = "done", t.percent = 100, t.response = e, t.xhr = r;
    const n = D(t, l);
    L(t, n);
  }, G = (e, o) => {
    if (!V(o, l))
      return;
    const r = _(o);
    r.status = "uploading", r.percent = e.percent;
    const t = D(r, l);
    L(r, t, e);
  }, H = (e, o, r) => {
    if (!V(r, l))
      return;
    const t = _(r);
    t.error = e, t.response = o, t.status = "error";
    const n = D(t, l);
    L(t, n);
  }, be = (e) => {
    let o;
    Promise.resolve(typeof I == "function" ? I(e) : I).then((r) => {
      var t;
      if (r === !1)
        return;
      const n = Be(e, l);
      n && (o = Object.assign(Object.assign({}, e), {
        status: "removed"
      }), l == null || l.forEach((i) => {
        const v = o.uid !== void 0 ? "uid" : "name";
        i[v] === o[v] && !Object.isFrozen(i) && (i.status = "removed");
      }), (t = U.current) === null || t === void 0 || t.abort(o), L(o, n));
    });
  }, j = (e) => {
    he(e.type), e.type === "drop" && (c == null || c(e));
  };
  a.useImperativeHandle(O, () => ({
    onBatchStart: A,
    onSuccess: z,
    onProgress: G,
    onError: H,
    fileList: l,
    upload: U.current
  }));
  const {
    getPrefixCls: Ie,
    direction: J,
    upload: y
  } = a.useContext(Me), s = Ie("upload", re), x = Object.assign(Object.assign({
    onBatchStart: A,
    onError: H,
    onProgress: G,
    onSuccess: z
  }, u), {
    data: ce,
    multiple: de,
    action: pe,
    accept: ue,
    supportServerRender: me,
    prefixCls: s,
    disabled: h,
    beforeUpload: ye,
    onChange: void 0
  });
  delete x.className, delete x.style, (!C || h) && delete x.id;
  const K = `${s}-wrapper`, [P, W, Fe] = Pe(s, K), [Le] = Ve("Upload", je.Upload), {
    showRemoveIcon: k,
    showPreviewIcon: xe,
    showDownloadIcon: Ce,
    removeIcon: Ee,
    previewIcon: Ue,
    downloadIcon: _e
  } = typeof p == "boolean" ? {} : p, De = typeof k > "u" ? !h : !!k, T = (e, o) => p ? /* @__PURE__ */ a.createElement(Te, {
    prefixCls: s,
    listType: d,
    items: l,
    previewFile: F,
    onPreview: S,
    onDownload: $,
    onRemove: be,
    showRemoveIcon: De,
    showPreviewIcon: xe,
    showDownloadIcon: Ce,
    removeIcon: Ee,
    previewIcon: Ue,
    downloadIcon: _e,
    iconRender: te,
    locale: Object.assign(Object.assign({}, Le), ee),
    isImageUrl: oe,
    progress: ne,
    appendAction: e,
    appendActionVisible: o,
    itemRender: le,
    disabled: h
  }) : e, B = M(K, se, fe, W, Fe, y == null ? void 0 : y.className, {
    [`${s}-rtl`]: J === "rtl",
    [`${s}-picture-card-wrapper`]: d === "picture-card",
    [`${s}-picture-circle-wrapper`]: d === "picture-circle"
  }), Ne = Object.assign(Object.assign({}, y == null ? void 0 : y.style), ie);
  if (ae === "drag") {
    const e = M(W, s, `${s}-drag`, {
      [`${s}-drag-uploading`]: l.some((o) => o.status === "uploading"),
      [`${s}-drag-hover`]: we === "dragover",
      [`${s}-disabled`]: h,
      [`${s}-rtl`]: J === "rtl"
    });
    return P(/* @__PURE__ */ a.createElement("span", {
      className: B
    }, /* @__PURE__ */ a.createElement("div", {
      className: e,
      style: Ne,
      onDrop: j,
      onDragOver: j,
      onDragLeave: j
    }, /* @__PURE__ */ a.createElement(Y, Object.assign({}, x, {
      ref: U,
      className: `${s}-btn`
    }), /* @__PURE__ */ a.createElement("div", {
      className: `${s}-drag-container`
    }, C))), T()));
  }
  const Oe = M(s, `${s}-select`, {
    [`${s}-disabled`]: h
  }), q = ((e) => /* @__PURE__ */ a.createElement("div", {
    className: Oe,
    style: e
  }, /* @__PURE__ */ a.createElement(Y, Object.assign({}, x, {
    ref: U
  }))))(C ? void 0 : {
    display: "none"
  });
  return P(d === "picture-card" || d === "picture-circle" ? /* @__PURE__ */ a.createElement("span", {
    className: B
  }, T(q, !!C)) : /* @__PURE__ */ a.createElement("span", {
    className: B
  }, q, T()));
}, Z = /* @__PURE__ */ a.forwardRef(ze);
process.env.NODE_ENV !== "production" && (Z.displayName = "Upload");
const st = Z;
export {
  N as LIST_IGNORE,
  st as default
};
