import P from "../../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import M from "../../../@babel/runtime/helpers/esm/defineProperty.mjs";
import w from "../../../@babel/runtime/helpers/esm/regeneratorRuntime.mjs";
import O from "../../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import F from "../../../@babel/runtime/helpers/esm/asyncToGenerator.mjs";
import S from "../../../async-validator/dist-web/index.mjs";
import * as V from "react";
import { warningOnce as G } from "../../../rc-util/es/warning.mjs";
import { defaultValidateMessages as T } from "./messages.mjs";
import { merge as Y } from "../../../rc-util/es/utils/set.mjs";
var D = S;
function q(o, i) {
  return o.replace(/\$\{\w+\}/g, function(c) {
    var u = c.slice(2, -1);
    return i[u];
  });
}
var L = "CODE_LOGIC_ERROR";
function C(o, i, c, u, l) {
  return E.apply(this, arguments);
}
function E() {
  return E = F(/* @__PURE__ */ w().mark(function o(i, c, u, l, f) {
    var e, d, v, b, n, s, p, h, m;
    return w().wrap(function(r) {
      for (; ; )
        switch (r.prev = r.next) {
          case 0:
            return e = O({}, u), delete e.ruleIndex, D.warning = function() {
            }, e.validator && (d = e.validator, e.validator = function() {
              try {
                return d.apply(void 0, arguments);
              } catch (a) {
                return console.error(a), Promise.reject(L);
              }
            }), v = null, e && e.type === "array" && e.defaultField && (v = e.defaultField, delete e.defaultField), b = new D(M({}, i, [e])), n = Y(T, l.validateMessages), b.messages(n), s = [], r.prev = 10, r.next = 13, Promise.resolve(b.validate(M({}, i, c), O({}, l)));
          case 13:
            r.next = 18;
            break;
          case 15:
            r.prev = 15, r.t0 = r.catch(10), r.t0.errors && (s = r.t0.errors.map(function(a, t) {
              var y = a.message, R = y === L ? n.default : y;
              return /* @__PURE__ */ V.isValidElement(R) ? (
                // Wrap ReactNode with `key`
                /* @__PURE__ */ V.cloneElement(R, {
                  key: "error_".concat(t)
                })
              ) : R;
            }));
          case 18:
            if (!(!s.length && v)) {
              r.next = 23;
              break;
            }
            return r.next = 21, Promise.all(c.map(function(a, t) {
              return C("".concat(i, ".").concat(t), a, v, l, f);
            }));
          case 21:
            return p = r.sent, r.abrupt("return", p.reduce(function(a, t) {
              return [].concat(P(a), P(t));
            }, []));
          case 23:
            return h = O(O({}, u), {}, {
              name: i,
              enum: (u.enum || []).join(", ")
            }, f), m = s.map(function(a) {
              return typeof a == "string" ? q(a, h) : a;
            }), r.abrupt("return", m);
          case 26:
          case "end":
            return r.stop();
        }
    }, o, null, [[10, 15]]);
  })), E.apply(this, arguments);
}
function x(o, i, c, u, l, f) {
  var e = o.join("."), d = c.map(function(n, s) {
    var p = n.validator, h = O(O({}, n), {}, {
      ruleIndex: s
    });
    return p && (h.validator = function(m, g, r) {
      var a = !1, t = function() {
        for (var A = arguments.length, $ = new Array(A), k = 0; k < A; k++)
          $[k] = arguments[k];
        Promise.resolve().then(function() {
          G(!a, "Your validator function has already return a promise. `callback` will be ignored."), a || r.apply(void 0, $);
        });
      }, y = p(m, g, t);
      a = y && typeof y.then == "function" && typeof y.catch == "function", G(a, "`callback` is deprecated. Please return a promise instead."), a && y.then(function() {
        r();
      }).catch(function(R) {
        r(R || " ");
      });
    }), h;
  }).sort(function(n, s) {
    var p = n.warningOnly, h = n.ruleIndex, m = s.warningOnly, g = s.ruleIndex;
    return !!p == !!m ? h - g : p ? 1 : -1;
  }), v;
  if (l === !0)
    v = new Promise(/* @__PURE__ */ function() {
      var n = F(/* @__PURE__ */ w().mark(function s(p, h) {
        var m, g, r;
        return w().wrap(function(t) {
          for (; ; )
            switch (t.prev = t.next) {
              case 0:
                m = 0;
              case 1:
                if (!(m < d.length)) {
                  t.next = 12;
                  break;
                }
                return g = d[m], t.next = 5, C(e, i, g, u, f);
              case 5:
                if (r = t.sent, !r.length) {
                  t.next = 9;
                  break;
                }
                return h([{
                  errors: r,
                  rule: g
                }]), t.abrupt("return");
              case 9:
                m += 1, t.next = 1;
                break;
              case 12:
                p([]);
              case 13:
              case "end":
                return t.stop();
            }
        }, s);
      }));
      return function(s, p) {
        return n.apply(this, arguments);
      };
    }());
  else {
    var b = d.map(function(n) {
      return C(e, i, n, u, f).then(function(s) {
        return {
          errors: s,
          rule: n
        };
      });
    });
    v = (l ? B(b) : z(b)).then(function(n) {
      return Promise.reject(n);
    });
  }
  return v.catch(function(n) {
    return n;
  }), v;
}
function z(o) {
  return I.apply(this, arguments);
}
function I() {
  return I = F(/* @__PURE__ */ w().mark(function o(i) {
    return w().wrap(function(u) {
      for (; ; )
        switch (u.prev = u.next) {
          case 0:
            return u.abrupt("return", Promise.all(i).then(function(l) {
              var f, e = (f = []).concat.apply(f, P(l));
              return e;
            }));
          case 1:
          case "end":
            return u.stop();
        }
    }, o);
  })), I.apply(this, arguments);
}
function B(o) {
  return j.apply(this, arguments);
}
function j() {
  return j = F(/* @__PURE__ */ w().mark(function o(i) {
    var c;
    return w().wrap(function(l) {
      for (; ; )
        switch (l.prev = l.next) {
          case 0:
            return c = 0, l.abrupt("return", new Promise(function(f) {
              i.forEach(function(e) {
                e.then(function(d) {
                  d.errors.length && f([d]), c += 1, c === i.length && f([]);
                });
              });
            }));
          case 2:
          case "end":
            return l.stop();
        }
    }, o);
  })), j.apply(this, arguments);
}
export {
  x as validateRules
};
