var z=Object.defineProperty,A=Object.defineProperties;var C=Object.getOwnPropertyDescriptors;var g=Object.getOwnPropertySymbols;var J=Object.prototype.hasOwnProperty,P=Object.prototype.propertyIsEnumerable;var O=(i,t,e)=>t in i?z(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,f=(i,t)=>{for(var e in t||(t={}))J.call(t,e)&&O(i,e,t[e]);if(g)for(var e of g(t))P.call(t,e)&&O(i,e,t[e]);return i},b=(i,t)=>A(i,C(t));var m=(i,t,e)=>new Promise((l,F)=>{var d=r=>{try{y(e.next(r))}catch(V){F(V)}},h=r=>{try{y(e.throw(r))}catch(V){F(V)}},y=r=>r.done?l(r.value):Promise.resolve(r.value).then(d,h);y((e=e.apply(i,t)).next())});import{L as R,a5 as j,Q as q,U as S,m as L,b4 as M}from"../jse/index-index-BUJ_4tQs.js";import{ct as Q,cu as T}from"./bootstrap-Bqxdt4iG.js";const k=(i,t)=>i.map(e=>{const l=e.fieldName;return l in t&&!("defaultValue"in e)?b(f({},e),{defaultValue:t[l]}):e}),H=i=>{const{emptyValues:t,schema:e,firstInputId:l,wrapperClass:F}=i,d=R(f({},t)),h=k(j(e),t),[y,r]=Q(b(f({},T),{schema:h,wrapperClass:F}));q(e)&&S(e,a=>{const o=k(a,t);r.setState({schema:o})},{deep:!0});const V=a=>{const o=Object.keys(t);return Object.fromEntries(o.map(c=>{const s=a[c];if(typeof s=="string"&&s){const u=N(c,e);if(u==="string")return[c,s];if(u==="dayjs"){const n=M(s);return[c,n.isValid()?n:null]}}return[c,s]}))},N=(a,o)=>{var n;const s=j(o).find(p=>p.fieldName===a);return(s==null?void 0:s.component)!=="DatePicker"?null:((n=s.componentProps)==null?void 0:n.valueFormat)?"string":"dayjs"},D=a=>{var c;const o={};for(const s of Object.keys(t))o[s]=(c=a[s])!=null?c:t[s];return o},I=()=>{if(l){const a=document.querySelector(`#${l}`);a==null||a.focus()}},E=()=>m(null,null,function*(){const a=yield r.getValues(),o=D(a),c=L(d.value);for(const s of Object.keys(t)){const u=o[s],n=c[s];if(!(u===null&&n===null)){if(u===null||n===null)return!0;if(typeof u!="object"&&typeof n!="object"){if(u!==n)return!0;continue}try{if(JSON.stringify(u)!==JSON.stringify(n))return!0}catch(p){return!0}}}return!1}),x=()=>m(null,null,function*(){yield r.resetForm(),yield r.setValues(t),d.value=f({},t)}),w=a=>m(null,null,function*(){const o=V(a),c=new Set(j(e).filter(n=>n.component==="ApiSelect").map(n=>n.fieldName)),s={},u={};for(const[n,p]of Object.entries(o))c.has(n)?u[n]=p:s[n]=p;Object.keys(s).length>0&&(yield r.setValues(s)),Object.keys(u).length>0&&setTimeout(()=>m(null,null,function*(){yield r.setValues(u)}),100),d.value=f({},o)}),v=()=>{d.value=f({},t)};return{Form:y,formApi:r,focus:I,isDirty:E,resetForm:x,resetInitialValues:v,setFormValues:w,submitForm:()=>m(null,null,function*(){const{valid:a}=yield r.validate();return a?yield r.getValues():null}),watchEntity:(a,o)=>{S(a,c=>m(null,null,function*(){c&&o()==="edit"?yield w(c):o()==="create"&&v()}),{immediate:!0})}}};export{H as u};
