import{defineComponent as te,computed as l,ref as le,watch as oe,createVNode as ie}from"vue";import ne from"lodash-es/isEqual";import se from"../node_modules/.pnpm/scroll-into-view-if-needed@3.1.0/node_modules/scroll-into-view-if-needed/dist/index.mjs";import ue from"../_util/classNames.mjs";import de from"../_util/props-util/initDefaultProps.mjs";import{functionType as c,booleanType as f,stringType as w,someType as R,anyType as me,objectType as P,tuple as ce}from"../_util/type.mjs";import A from"../_util/vue-types/index.mjs";import{useProviderDisabled as fe}from"../config-provider/DisabledContext.mjs";import{useProviderSize as ve}from"../config-provider/SizeContext.mjs";import{useInjectGlobalForm as he}from"../config-provider/context.mjs";import ge from"../config-provider/hooks/useConfigInject.mjs";import Fe from"./FormItem.mjs";import{useProvideForm as be}from"./context.mjs";import Pe from"./style/index.mjs";import ye from"./useForm.mjs";import{allPromiseFinish as Me}from"./utils/asyncUtil.mjs";import{defaultValidateMessages as pe}from"./utils/messages.mjs";import{toArray as y}from"./utils/typeUtil.mjs";import{cloneByNamePathList as I,getNamePath as L,containsNamePath as Ce}from"./utils/valueUtil.mjs";const qe=()=>({layout:A.oneOf(ce("horizontal","inline","vertical")),labelCol:P(),wrapperCol:P(),colon:f(),labelAlign:w(),labelWrap:f(),prefixCls:String,requiredMark:R([String,Boolean]),hideRequiredMark:f(),model:A.object,rules:P(),validateMessages:P(),validateOnRuleChange:f(),scrollToFirstError:me(),onSubmit:c(),name:String,validateTrigger:R([String,Array]),size:w(),disabled:f(),onValuesChange:c(),onFieldsChange:c(),onFinish:c(),onFinishFailed:c(),onValidate:c()});function ke(r,s){return ne(y(r),y(s))}const He=te({compatConfig:{MODE:3},name:"AForm",inheritAttrs:!1,props:de(qe(),{layout:"horizontal",hideRequiredMark:!1,colon:!0}),Item:Fe,useForm:ye,setup(r,{emit:s,slots:$,expose:x,attrs:T}){const{prefixCls:u,direction:z,form:v,size:M,disabled:D}=ge("form",r),j=l(()=>r.requiredMark===""||r.requiredMark),N=l(()=>j.value!==void 0?j.value:v&&v.value?.requiredMark!==void 0?v.value.requiredMark:!r.hideRequiredMark);ve(M),fe(D);const B=l(()=>r.colon??v.value?.colon),{validateMessages:W}=he(),E=l(()=>({...pe,...W.value,...r.validateMessages})),[G,H]=Pe(u),J=l(()=>ue(u.value,{[`${u.value}-${r.layout}`]:!0,[`${u.value}-hide-required-mark`]:N.value===!1,[`${u.value}-rtl`]:z.value==="rtl",[`${u.value}-${M.value}`]:M.value},H.value)),p=le(),d={},K=(e,a)=>{d[e]=a},Q=e=>{delete d[e]},C=e=>{const a=!!e,t=a?y(e).map(L):[];return a?Object.values(d).filter(o=>t.findIndex(n=>ke(n,o.fieldName.value))>-1):Object.values(d)},U=e=>{r.model&&C(e).forEach(a=>{a.resetField()})},X=e=>{C(e).forEach(a=>{a.clearValidate()})},Y=e=>{const{scrollToFirstError:a}=r;if(s("finishFailed",e),a&&e.errorFields.length){let t={};typeof a=="object"&&(t=a),S(e.errorFields[0].name,t)}},Z=(...e)=>_(...e),S=(e,a={})=>{const t=C(e?[e]:void 0);if(t.length){const o=t[0].fieldId.value,n=o?document.getElementById(o):null;n&&se(n,{scrollMode:"if-needed",block:"nearest",...a})}},q=(e=!0)=>{if(e===!0){const a=[];return Object.values(d).forEach(({namePath:t})=>{a.push(t.value)}),I(r.model,a)}else return I(r.model,e)},h=(e,a)=>{if(!r.model)return Promise.reject("Form `model` is required for validateFields to work.");const t=!!e,o=t?y(e).map(L):[],n=[];Object.values(d).forEach(i=>{if(t||o.push(i.namePath.value),!i.rules?.value.length)return;const m=i.namePath.value;if(!t||Ce(o,m)){const F=i.validateRules({validateMessages:E.value,...a});n.push(F.then(()=>({name:m,errors:[],warnings:[]})).catch(re=>{const b=[],k=[];return re.forEach(({rule:{warningOnly:ae},errors:V})=>{ae?k.push(...V):b.push(...V)}),b.length?Promise.reject({name:m,errors:b,warnings:k}):{name:m,errors:b,warnings:k}}))}});const g=Me(n);p.value=g;const O=g.then(()=>p.value===g?Promise.resolve(q(o)):Promise.reject([])).catch(i=>{const m=i.filter(F=>F&&F.errors.length);return Promise.reject({values:q(o),errorFields:m,outOfDate:p.value!==g})});return O.catch(i=>i),O},_=(...e)=>h(...e),ee=e=>{e.preventDefault(),e.stopPropagation(),s("submit",e),r.model&&h().then(t=>{s("finish",t)}).catch(t=>{Y(t)})};return x({resetFields:U,clearValidate:X,validateFields:h,getFieldsValue:q,validate:Z,scrollToField:S}),be({model:l(()=>r.model),name:l(()=>r.name),labelAlign:l(()=>r.labelAlign),labelCol:l(()=>r.labelCol),labelWrap:l(()=>r.labelWrap),wrapperCol:l(()=>r.wrapperCol),vertical:l(()=>r.layout==="vertical"),colon:B,requiredMark:N,validateTrigger:l(()=>r.validateTrigger),rules:l(()=>r.rules),addField:K,removeField:Q,onValidate:(e,a,t)=>{s("validate",e,a,t)},validateMessages:E}),oe(()=>r.rules,()=>{r.validateOnRuleChange&&h()}),()=>G(ie("form",{...T,onSubmit:ee,class:[J.value,T.class]},[$.default?.()]))}});export{He as default,qe as formProps};
