import { defineComponent as re, shallowRef as oe, ref as ne, reactive as ie, unref as f, nextTick as le, toRaw as P, watch as ae, onWatcherCleanup as se, isVNode as ue, cloneVNode as ce, computed as fe, openBlock as N, createBlock as _, mergeProps as de, withCtx as x, renderSlot as O } from "vue";
import { Form as pe } from "@aplus-frontend/antdv";
import { useUrlSearchParams as ge } from "@vueuse/core";
import { isArray as B, cloneDeep as W, isEqual as me, omit as Fe } from "lodash-unified";
import "../render/index.mjs";
import { useDevWarning as he } from "../utils/warning.mjs";
import { getRouteType as ye } from "../utils/index.mjs";
import { INTERNAL_FORM_INSTANCE_MARK as j } from "./constant.mjs";
import { useProvideForm as Ve } from "./context.mjs";
import { useInjectFormProvider as Te } from "./provider/context.mjs";
import { getModelValue as S } from "./utils/get.mjs";
import { genParams as M, setToParams as q } from "./utils/params.mjs";
import ve from "./utils/set.mjs";
import { transformValues as F } from "./utils/transform.mjs";
import { isDef as h } from "../utils/is.mjs";
import Ie from "../render/render.mjs";
const je = /* @__PURE__ */ re({
  name: "ApForm",
  __name: "ap-form",
  props: {
    layout: { default: "horizontal" },
    labelCol: {},
    wrapperCol: {},
    colon: { type: Boolean, default: !0 },
    labelAlign: { default: "right" },
    labelWrap: { type: Boolean },
    prefixCls: {},
    requiredMark: {},
    hideRequiredMark: { type: Boolean, default: !1 },
    rules: {},
    validateMessages: {},
    validateOnRuleChange: { type: Boolean, default: !0 },
    scrollToFirstError: {},
    onSubmit: {},
    name: {},
    validateTrigger: { default: "change" },
    size: {},
    disabled: { type: Boolean },
    onFieldsChange: {},
    onFinish: {},
    onFinishFailed: {},
    onValidate: {},
    variant: {},
    initialValues: {},
    onValuesChange: {},
    syncToUrl: { type: [Boolean, Function], default: !1 },
    syncToUrlPriority: { type: Boolean },
    autoFocusFirstInput: { type: Boolean }
  },
  setup(y, { expose: D }) {
    const r = y;
    function V(e, t, o = !0, i) {
      if (B(e)) {
        let l = n;
        for (let p = 0; p < e.length - 1; p++)
          e[p] in l || (l[e[p]] = {}), l = l[e[p]];
        i ? i(l, e[e.length - 1]) : l[e[e.length - 1]] = t;
      } else
        i ? i(n, e) : n[e] = t;
      let E = B(e) ? e[0] : e;
      if (o) {
        const l = { [E]: n[E] };
        g.triggerFormChange(r.name, l), r.onValuesChange?.(l, n, e);
      }
      m();
    }
    const a = oe(), T = ge(ye()), g = Te(), $ = he("ApForm"), v = ne(
      r.syncToUrl ? M(r.syncToUrl, T, "get") : {}
    );
    let s = W(w()), u = {};
    const n = ie(w());
    let d = [];
    function w() {
      const e = r.initialValues || {};
      return r.syncToUrlPriority ? {
        ...e,
        ...f(v)
      } : {
        ...f(v),
        ...e
      };
    }
    function K(e) {
      const t = F(
        e,
        u,
        c
      );
      g.triggerFormFinish(r.name, e), r.syncToUrl && q(T, M(r.syncToUrl, t, "set")), r.onFinish?.(t);
    }
    const L = async () => {
      a.value?.resetFields(), await le(() => {
        Object.keys(n).forEach((e) => delete n[e]), Object.assign(n, W(s)), m(), r.syncToUrl && q(
          T,
          M(
            r.syncToUrl,
            F(
              s,
              u,
              c
            ),
            "set"
          )
        );
      });
    }, G = (e) => {
      a.value?.clearValidate(e);
    }, I = (e, t) => a.value?.validateFields(e, t), H = async (e, t) => {
      const o = await a.value?.validateFields(
        e,
        t
      );
      return F(
        o,
        u,
        c
      );
    }, c = (e) => e === !0 ? P(n) : a.value?.getFieldsValue(e), J = (e) => {
      const t = c(e);
      return F(t, u, c);
    }, Q = (e, t) => a.value?.validate(e, t), X = (e, t = {}) => {
      a.value?.scrollToField(e, t);
    }, Y = (e) => {
      Object.assign(n, e), m();
    }, C = (e, t, o = !0, i) => {
      V(e, t, o, i), m();
    };
    function U(e) {
      return e(P(n)), d.push(e), () => {
        d = d.filter((t) => t !== e);
      };
    }
    function R(e, t) {
      if (B(e)) {
        let o = s;
        for (let i = 0; i < e.length - 1; i++)
          h(o[e[i]]) || (o[e[i]] = {}), o = o[e[i]];
        o[e[e.length - 1]] = t;
      } else
        s[e] = t;
    }
    function Z(e, t) {
      if (!h(e) || !h(t))
        return;
      const o = S(s, t);
      if (me(o, e))
        return;
      const i = S(f(v), t);
      h(i) && r.syncToUrlPriority || (V(t, e, !1), R(t, e));
    }
    function z(e, t) {
      t && (u = ve(
        u,
        [e].flat(1),
        t,
        !0
      ));
    }
    function A({ initialValue: e, name: t, transform: o }) {
      Z(e, t), z(t, o);
    }
    function b() {
      return s;
    }
    function ee(e) {
      if ($(
        e === j,
        "usage",
        "getInternalInstance should only used at internal"
      ), e === j)
        return {
          registerField: A,
          registerWatch: U,
          setFieldValue: C,
          getInitialValues: b,
          updateInitialModel: R,
          validateFields: I
        };
    }
    function m() {
      if (d.length) {
        const e = P(n);
        d.forEach((t) => {
          t(e);
        });
      }
    }
    const k = {
      resetFields: L,
      clearValidate: G,
      validateFields: I,
      getFieldsValue: c,
      validate: Q,
      scrollToField: X,
      setFieldsValue: Y,
      setFieldValue: C,
      registerWatch: U,
      validateFieldsReturnTransformed: H,
      getFieldsValueTransformed: J,
      getInternalInstance: ee
    };
    ae(
      () => r.name,
      (e) => {
        e && (g.registerForm(e, k), se(() => {
          g.unregisterForm(r.name);
        }));
      },
      {
        immediate: !0
      }
    );
    function te(e) {
      return e.map((t, o) => o === 0 && ue(t) && r.autoFocusFirstInput ? ce(t, { autoFocus: r.autoFocusFirstInput }) : t);
    }
    return Ve({
      model: fe(() => n),
      updateModel: V,
      internalInstance: {
        registerWatch: U,
        registerField: A,
        setFieldValue: C,
        getInitialValues: b,
        updateInitialModel: R,
        validateFields: I
      }
    }), D(k), (e, t) => (N(), _(f(pe), de(
      {
        ref_key: "formRef",
        ref: a
      },
      f(Fe)(r, [
        "initialValues",
        "onValuesChange",
        "onFinish",
        "syncToUrl",
        "syncToUrlPriority",
        "autoFocusFirstInput"
      ]),
      {
        model: n,
        "data-form-name": y.name,
        onFinish: K
      }
    ), {
      default: x(() => [
        y.autoFocusFirstInput ? (N(), _(f(Ie), {
          key: 1,
          renderer: te
        }, {
          default: x(() => [
            O(e.$slots, "default")
          ]),
          _: 3
        })) : O(e.$slots, "default", { key: 0 })
      ]),
      _: 3
    }, 16, ["model", "data-form-name"]));
  }
});
export {
  je as default
};
