import {
  H as e,
  I as a,
  ae as r,
  ad as t,
  v as n,
  J as o,
  K as l,
  aE as s,
  bm as i,
  Q as d,
} from "./index-476be0bb.js";
const [m, u] = e("form");
const c = d(
  a({
    name: m,
    props: {
      colon: Boolean,
      disabled: Boolean,
      readonly: Boolean,
      showError: Boolean,
      labelWidth: o,
      labelAlign: String,
      inputAlign: String,
      scrollToError: Boolean,
      validateFirst: Boolean,
      submitOnEnter: l,
      showErrorMessage: l,
      errorMessageAlign: String,
      validateTrigger: { type: [String, Array], default: "onBlur" },
    },
    emits: ["submit", "failed"],
    setup(e, { emit: a, slots: o }) {
      const { children: l, linkChildren: d } = r(i),
        m = (e) => (e ? l.filter((a) => e.includes(a.name)) : l),
        c = (a) => {
          return "string" == typeof a
            ? ((e) => {
                const a = l.find((a) => a.name === e);
                return a
                  ? new Promise((e, r) => {
                      a.validate().then((a) => {
                        a ? r(a) : e();
                      });
                    })
                  : Promise.reject();
              })(a)
            : e.validateFirst
            ? ((r = a),
              new Promise((e, a) => {
                const t = [];
                m(r)
                  .reduce(
                    (e, a) =>
                      e.then(() => {
                        if (!t.length)
                          return a.validate().then((e) => {
                            e && t.push(e);
                          });
                      }),
                    Promise.resolve()
                  )
                  .then(() => {
                    t.length ? a(t) : e();
                  });
              }))
            : ((e) =>
                new Promise((a, r) => {
                  const t = m(e);
                  Promise.all(t.map((e) => e.validate())).then((e) => {
                    (e = e.filter(Boolean)).length ? r(e) : a();
                  });
                }))(a);
          var r;
        },
        g = (e, a) => {
          l.some((r) => r.name === e && (r.$el.scrollIntoView(a), !0));
        },
        h = () =>
          l.reduce(
            (e, a) => (void 0 !== a.name && (e[a.name] = a.formValue.value), e),
            {}
          ),
        f = () => {
          const r = h();
          c()
            .then(() => a("submit", r))
            .catch((t) => {
              a("failed", { values: r, errors: t }),
                e.scrollToError && t[0].name && g(t[0].name);
            });
        },
        v = (e) => {
          s(e), f();
        };
      return (
        d({ props: e }),
        t({
          submit: f,
          validate: c,
          getValues: h,
          scrollToField: g,
          resetValidation: (e) => {
            "string" == typeof e && (e = [e]);
            m(e).forEach((e) => {
              e.resetValidation();
            });
          },
          getValidationStatus: () =>
            l.reduce((e, a) => ((e[a.name] = a.getValidationStatus()), e), {}),
        }),
        () => {
          var e;
          return n("form", { class: u(), onSubmit: v }, [
            null == (e = o.default) ? void 0 : e.call(o),
          ]);
        }
      );
    },
  })
);
export { c as F };
