"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const r=require("vue"),Q=require("@aplus-frontend/antdv"),X=require("@vueuse/core"),u=require("lodash-unified");require("../render/index.js");const Y=require("../utils/warning.js"),Z=require("../utils/index.js"),N=require("./constant.js"),z=require("./context.js"),ee=require("./provider/context.js"),b=require("./utils/get.js"),h=require("./utils/params.js"),te=require("./utils/set.js"),V=require("./utils/transform.js"),T=require("../utils/is.js"),re=require("../render/render.js"),ne=r.defineComponent({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(m,{expose:k}){const n=m;function v(e,t,o=!0,i){if(u.isArray(e)){let a=s;for(let F=0;F<e.length-1;F++)e[F]in a||(a[e[F]]={}),a=a[e[F]];i?i(a,e[e.length-1]):a[e[e.length-1]]=t}else i?i(s,e):s[e]=t;let _=u.isArray(e)?e[0]:e;if(o){const a={[_]:s[_]};p.triggerFormChange(n.name,a),n.onValuesChange?.(a,s,e)}y()}const l=r.shallowRef(),I=X.useUrlSearchParams(Z.getRouteType()),p=ee.useInjectFormProvider(),x=Y.useDevWarning("ApForm"),q=r.ref(n.syncToUrl?h.genParams(n.syncToUrl,I,"get"):{});let c=u.cloneDeep(U()),d={};const s=r.reactive(U());let g=[];function U(){const e=n.initialValues||{};return n.syncToUrlPriority?{...e,...r.unref(q)}:{...r.unref(q),...e}}function E(e){const t=V.transformValues(e,d,f);p.triggerFormFinish(n.name,e),n.syncToUrl&&h.setToParams(I,h.genParams(n.syncToUrl,t,"set")),n.onFinish?.(t)}const S=async()=>{l.value?.resetFields(),await r.nextTick(()=>{Object.keys(s).forEach(e=>delete s[e]),Object.assign(s,u.cloneDeep(c)),y(),n.syncToUrl&&h.setToParams(I,h.genParams(n.syncToUrl,V.transformValues(c,d,f),"set"))})},D=e=>{l.value?.clearValidate(e)},C=(e,t)=>l.value?.validateFields(e,t),O=async(e,t)=>{const o=await l.value?.validateFields(e,t);return V.transformValues(o,d,f)},f=e=>e===!0?r.toRaw(s):l.value?.getFieldsValue(e),j=e=>{const t=f(e);return V.transformValues(t,d,f)},W=(e,t)=>l.value?.validate(e,t),$=(e,t={})=>{l.value?.scrollToField(e,t)},K=e=>{Object.assign(s,e),y()},R=(e,t,o=!0,i)=>{v(e,t,o,i),y()};function M(e){return e(r.toRaw(s)),g.push(e),()=>{g=g.filter(t=>t!==e)}}function P(e,t){if(u.isArray(e)){let o=c;for(let i=0;i<e.length-1;i++)T.isDef(o[e[i]])||(o[e[i]]={}),o=o[e[i]];o[e[e.length-1]]=t}else c[e]=t}function L(e,t){if(!T.isDef(e)||!T.isDef(t))return;const o=b.getModelValue(c,t);if(u.isEqual(o,e))return;const i=b.getModelValue(r.unref(q),t);T.isDef(i)&&n.syncToUrlPriority||(v(t,e,!1),P(t,e))}function G(e,t){t&&(d=te.default(d,[e].flat(1),t,!0))}function w({initialValue:e,name:t,transform:o}){L(e,t),G(t,o)}function A(){return c}function H(e){if(x(e===N.INTERNAL_FORM_INSTANCE_MARK,"usage","getInternalInstance should only used at internal"),e===N.INTERNAL_FORM_INSTANCE_MARK)return{registerField:w,registerWatch:M,setFieldValue:R,getInitialValues:A,updateInitialModel:P,validateFields:C}}function y(){if(g.length){const e=r.toRaw(s);g.forEach(t=>{t(e)})}}const B={resetFields:S,clearValidate:D,validateFields:C,getFieldsValue:f,validate:W,scrollToField:$,setFieldsValue:K,setFieldValue:R,registerWatch:M,validateFieldsReturnTransformed:O,getFieldsValueTransformed:j,getInternalInstance:H};r.watch(()=>n.name,e=>{e&&(p.registerForm(e,B),r.onWatcherCleanup(()=>{p.unregisterForm(n.name)}))},{immediate:!0});function J(e){return e.map((t,o)=>o===0&&r.isVNode(t)&&n.autoFocusFirstInput?r.cloneVNode(t,{autoFocus:n.autoFocusFirstInput}):t)}return z.useProvideForm({model:r.computed(()=>s),updateModel:v,internalInstance:{registerWatch:M,registerField:w,setFieldValue:R,getInitialValues:A,updateInitialModel:P,validateFields:C}}),k(B),(e,t)=>(r.openBlock(),r.createBlock(r.unref(Q.Form),r.mergeProps({ref_key:"formRef",ref:l},r.unref(u.omit)(n,["initialValues","onValuesChange","onFinish","syncToUrl","syncToUrlPriority","autoFocusFirstInput"]),{model:s,"data-form-name":m.name,onFinish:E}),{default:r.withCtx(()=>[m.autoFocusFirstInput?(r.openBlock(),r.createBlock(r.unref(re.default),{key:1,renderer:J},{default:r.withCtx(()=>[r.renderSlot(e.$slots,"default")]),_:3})):r.renderSlot(e.$slots,"default",{key:0})]),_:3},16,["model","data-form-name"]))}});exports.default=ne;
