import q from "../../@babel/runtime/helpers/esm/extends.mjs";
import C from "../../@babel/runtime/helpers/esm/defineProperty.mjs";
import I from "../../@babel/runtime/helpers/esm/objectWithoutProperties.mjs";
import P from "../../@babel/runtime/helpers/esm/regeneratorRuntime.mjs";
import O from "../../@babel/runtime/helpers/esm/typeof.mjs";
import U from "../../@babel/runtime/helpers/esm/asyncToGenerator.mjs";
import D from "../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import j from "../../@babel/runtime/helpers/esm/classCallCheck.mjs";
import T from "../../@babel/runtime/helpers/esm/createClass.mjs";
import $ from "../../@babel/runtime/helpers/esm/inherits.mjs";
import R from "../../@babel/runtime/helpers/esm/createSuper.mjs";
import S, { Component as K } from "react";
import L from "../../classnames/index.mjs";
import W from "../../rc-util/es/pickAttrs.mjs";
import B from "./request.mjs";
import M from "./uid.mjs";
import w from "./attr-accept.mjs";
import _ from "./traverseFileTree.mjs";
var x = ["component", "prefixCls", "className", "disabled", "id", "style", "multiple", "accept", "capture", "children", "directory", "openFileDialogOnClick", "onMouseEnter", "onMouseLeave"], ue = /* @__PURE__ */ function(A) {
  $(g, A);
  var E = R(g);
  function g() {
    var e;
    j(this, g);
    for (var l = arguments.length, r = new Array(l), u = 0; u < l; u++)
      r[u] = arguments[u];
    return e = E.call.apply(E, [this].concat(r)), e.state = {
      uid: M()
    }, e.reqs = {}, e.fileInput = void 0, e._isMounted = void 0, e.onChange = function(n) {
      var s = e.props, t = s.accept, i = s.directory, p = n.target.files, a = D(p).filter(function(c) {
        return !i || w(c, t);
      });
      e.uploadFiles(a), e.reset();
    }, e.onClick = function(n) {
      var s = e.fileInput;
      if (s) {
        var t = e.props, i = t.children, p = t.onClick;
        if (i && i.type === "button") {
          var a = s.parentNode;
          a.focus(), a.querySelector("button").blur();
        }
        s.click(), p && p(n);
      }
    }, e.onKeyDown = function(n) {
      n.key === "Enter" && e.onClick(n);
    }, e.onFileDrop = function(n) {
      var s = e.props.multiple;
      if (n.preventDefault(), n.type !== "dragover")
        if (e.props.directory)
          _(Array.prototype.slice.call(n.dataTransfer.items), e.uploadFiles, function(i) {
            return w(i, e.props.accept);
          });
        else {
          var t = D(n.dataTransfer.files).filter(function(i) {
            return w(i, e.props.accept);
          });
          s === !1 && (t = t.slice(0, 1)), e.uploadFiles(t);
        }
    }, e.uploadFiles = function(n) {
      var s = D(n), t = s.map(function(i) {
        return i.uid = M(), e.processFile(i, s);
      });
      Promise.all(t).then(function(i) {
        var p = e.props.onBatchStart;
        p == null || p(i.map(function(a) {
          var c = a.origin, f = a.parsedFile;
          return {
            file: c,
            parsedFile: f
          };
        })), i.filter(function(a) {
          return a.parsedFile !== null;
        }).forEach(function(a) {
          e.post(a);
        });
      });
    }, e.processFile = /* @__PURE__ */ function() {
      var n = U(/* @__PURE__ */ P().mark(function s(t, i) {
        var p, a, c, f, h, y, d, b, F;
        return P().wrap(function(o) {
          for (; ; )
            switch (o.prev = o.next) {
              case 0:
                if (p = e.props.beforeUpload, a = t, !p) {
                  o.next = 14;
                  break;
                }
                return o.prev = 3, o.next = 6, p(t, i);
              case 6:
                a = o.sent, o.next = 12;
                break;
              case 9:
                o.prev = 9, o.t0 = o.catch(3), a = !1;
              case 12:
                if (a !== !1) {
                  o.next = 14;
                  break;
                }
                return o.abrupt("return", {
                  origin: t,
                  parsedFile: null,
                  action: null,
                  data: null
                });
              case 14:
                if (c = e.props.action, typeof c != "function") {
                  o.next = 21;
                  break;
                }
                return o.next = 18, c(t);
              case 18:
                f = o.sent, o.next = 22;
                break;
              case 21:
                f = c;
              case 22:
                if (h = e.props.data, typeof h != "function") {
                  o.next = 29;
                  break;
                }
                return o.next = 26, h(t);
              case 26:
                y = o.sent, o.next = 30;
                break;
              case 29:
                y = h;
              case 30:
                return d = // string type is from legacy `transformFile`.
                // Not sure if this will work since no related test case works with it
                (O(a) === "object" || typeof a == "string") && a ? a : t, d instanceof File ? b = d : b = new File([d], t.name, {
                  type: t.type
                }), F = b, F.uid = t.uid, o.abrupt("return", {
                  origin: t,
                  data: y,
                  parsedFile: F,
                  action: f
                });
              case 35:
              case "end":
                return o.stop();
            }
        }, s, null, [[3, 9]]);
      }));
      return function(s, t) {
        return n.apply(this, arguments);
      };
    }(), e.saveFileInput = function(n) {
      e.fileInput = n;
    }, e;
  }
  return T(g, [{
    key: "componentDidMount",
    value: function() {
      this._isMounted = !0;
    }
  }, {
    key: "componentWillUnmount",
    value: function() {
      this._isMounted = !1, this.abort();
    }
  }, {
    key: "post",
    value: function(l) {
      var r = this, u = l.data, n = l.origin, s = l.action, t = l.parsedFile;
      if (this._isMounted) {
        var i = this.props, p = i.onStart, a = i.customRequest, c = i.name, f = i.headers, h = i.withCredentials, y = i.method, d = n.uid, b = a || B, F = {
          action: s,
          filename: c,
          data: u,
          file: t,
          headers: f,
          withCredentials: h,
          method: y || "post",
          onProgress: function(o) {
            var m = r.props.onProgress;
            m == null || m(o, t);
          },
          onSuccess: function(o, m) {
            var v = r.props.onSuccess;
            v == null || v(o, t, m), delete r.reqs[d];
          },
          onError: function(o, m) {
            var v = r.props.onError;
            v == null || v(o, m, t), delete r.reqs[d];
          }
        };
        p(n), this.reqs[d] = b(F);
      }
    }
  }, {
    key: "reset",
    value: function() {
      this.setState({
        uid: M()
      });
    }
  }, {
    key: "abort",
    value: function(l) {
      var r = this.reqs;
      if (l) {
        var u = l.uid ? l.uid : l;
        r[u] && r[u].abort && r[u].abort(), delete r[u];
      } else
        Object.keys(r).forEach(function(n) {
          r[n] && r[n].abort && r[n].abort(), delete r[n];
        });
    }
  }, {
    key: "render",
    value: function() {
      var l, r = this.props, u = r.component, n = r.prefixCls, s = r.className, t = r.disabled, i = r.id, p = r.style, a = r.multiple, c = r.accept, f = r.capture, h = r.children, y = r.directory, d = r.openFileDialogOnClick, b = r.onMouseEnter, F = r.onMouseLeave, k = I(r, x), o = L((l = {}, C(l, n, !0), C(l, "".concat(n, "-disabled"), t), C(l, s, s), l)), m = y ? {
        directory: "directory",
        webkitdirectory: "webkitdirectory"
      } : {}, v = t ? {} : {
        onClick: d ? this.onClick : function() {
        },
        onKeyDown: d ? this.onKeyDown : function() {
        },
        onMouseEnter: b,
        onMouseLeave: F,
        onDrop: this.onFileDrop,
        onDragOver: this.onFileDrop,
        tabIndex: "0"
      };
      return /* @__PURE__ */ S.createElement(u, q({}, v, {
        className: o,
        role: "button",
        style: p
      }), /* @__PURE__ */ S.createElement("input", q({}, W(k, {
        aria: !0,
        data: !0
      }), {
        id: i,
        disabled: t,
        type: "file",
        ref: this.saveFileInput,
        onClick: function(N) {
          return N.stopPropagation();
        },
        key: this.state.uid,
        style: {
          display: "none"
        },
        accept: c
      }, m, {
        multiple: a,
        onChange: this.onChange
      }, f != null ? {
        capture: f
      } : {})), h);
    }
  }]), g;
}(K);
export {
  ue as default
};
