@formhaus/core 0.10.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  var Formhaus=(function(exports){'use strict';typeof console==="undefined"&&(globalThis.console={log:function(){},warn:function(){},error:function(){}});
2
- var Ot=Object.defineProperty,Lt=Object.defineProperties;var $t=Object.getOwnPropertyDescriptors;var ot=Object.getOwnPropertySymbols;var jt=Object.prototype.hasOwnProperty,Nt=Object.prototype.propertyIsEnumerable;var lt=(t,e,r)=>e in t?Ot(t,e,{enumerable:true,configurable:true,writable:true,value:r}):t[e]=r,p=(t,e)=>{for(var r in e||(e={}))jt.call(e,r)&&lt(t,r,e[r]);if(ot)for(var r of ot(e))Nt.call(e,r)&&lt(t,r,e[r]);return t},J=(t,e)=>Lt(t,$t(e));function K(t){return t==null||t===""||Array.isArray(t)&&!t.length}function U(t,e){let r=e[t.field];return t.eq!==void 0?r===t.eq:t.neq!==void 0?r!==t.neq:t.in!==void 0?t.in.includes(r):t.notIn!==void 0?!t.notIn.includes(r):t.notEmpty===true?!K(r):true}function h(t,e){var i,n;let r=s=>U(s,e);return ((i=t.show)!=null?i:[]).every(r)&&(!((n=t.showAny)!=null&&n.length)||t.showAny.some(r))}var x=h;function at(t){var e,r;return !!((e=t.fields)!=null&&e.length)&&!!((r=t.steps)!=null&&r.length)}function F(t){var e;return !!((e=t.steps)!=null&&e.some(r=>{var i;return (i=r.routes)==null?void 0:i.length}))}function S(t){var e,r;return [...(e=t.show)!=null?e:[],...(r=t.showAny)!=null?r:[]].map(i=>i.field)}function w(t){var e,r;return (e=t.steps)!=null&&e.length?t.steps.flatMap(i=>i.fields):(r=t.fields)!=null?r:[]}function I(t,e){let r={};for(let i of t)i.defaultValue!==void 0&&(r[i.key]=i.defaultValue);return Object.assign(r,e)}function E(t,e){let r=new Set([...Object.keys(t),...Object.keys(e)]);return new Set([...r].filter(i=>!Object.is(t[i],e[i])))}function V(t){var s;let e=(s=t.steps)!=null?s:[],r=at(t)?['FormEngine rejects a definition with both "fields" and "steps".']:[],i=new Map,n=new Map;e.forEach((o,l)=>{i.has(o.id)&&r.push(`Duplicate step id "${o.id}".`),i.set(o.id,l);for(let a of o.fields)n.set(a.key,l);});for(let{key:o}of w(t))(o in Object.prototype||o==="prototype")&&r.push(`Reserved field key "${o}".`);return F(t)&&e.forEach((o,l)=>{var a,u;for(let d of (a=o.routes)!=null?a:[]){let c=d.to===null?null:i.get(d.to);(c===void 0||c!==null&&c<=l)&&r.push(`Invalid route from "${o.id}" to "${d.to}": targets must be later steps or null.`);for(let f of S(d))((u=n.get(f))!=null?u:1/0)<=l||r.push(`Invalid route on "${o.id}": condition field "${f}" must belong to this or an earlier step.`);}}),r}function ut(t){var i,n,s,o;let e=(i=t.steps)!=null?i:[],r=[];for(let l of e){let a=new Set(((n=l.routes)!=null?n:[]).map(u=>u.to).filter(u=>u!==null));for(let u of a){let d=e.findIndex(y=>y.id===u),c=e[d+1],f=(o=(s=e[d])==null?void 0:s.routes)==null?void 0:o.some(y=>{var v,b;return !((v=y.show)!=null&&v.length)&&!((b=y.showAny)!=null&&b.length)});c&&a.has(c.id)&&!f&&r.push(`Route branch "${u}" from "${l.id}" continues into sibling branch "${c.id}". Add an unconditional route where it should continue.`);}}return r}function tt(t,e){var o,l;let r=(o=t.steps)!=null?o:[];if(!F(t))return r.filter(a=>x(a,e));let i=new Map(r.map((a,u)=>[a.id,u])),n=[],s={};for(let a=0;a<r.length;){let u=r[a];if(!x(u,s)){a++;continue}n.push(u),Object.assign(s,ct(u,e,s));let d=(l=u.routes)==null?void 0:l.find(c=>{var f;return h(c,s)&&(c.to===null||((f=i.get(c.to))!=null?f:-1)>a&&x(r[i.get(c.to)],s))});if((d==null?void 0:d.to)===null)break;a=d?i.get(d.to):a+1;}return n}function dt(t,e,r){var o;let i=(o=t[r])!=null?o:t[t.length-1],n=e.findIndex(l=>l.id===(i==null?void 0:i.id));if(n!==-1)return n;let s=new Map(e.map((l,a)=>[l.id,a]));for(let l=Math.min(r-1,t.length-1);l>=0;l--){let a=s.get(t[l].id);if(a!==void 0)return a}return 0}function ct(t,e,r){let i=p({},r);for(let s of t.fields)i[s.key]=e[s.key];let n={};for(let s of t.fields)h(s,i)&&e[s.key]!==void 0&&(n[s.key]=e[s.key]);return n}var P=class{constructor(e){this.definition=e;this.source=null;this.activeStepList=[];this.activeValueMap={};this.enabled=F(e);}invalidate(){this.source=null;}steps(e){return this.refresh(e),this.activeStepList}values(e){return this.refresh(e),this.activeValueMap}refresh(e){if(this.source===e)return;this.source=e,this.activeStepList=tt(this.definition,e);let r={};for(let i of this.activeStepList)Object.assign(r,ct(i,e,r));this.activeValueMap=r;}};function pt(t){var l,a;let e=[],r=new Map,i=w(t),n=new Set;for(let{key:u}of i)n.has(u)&&e.push(`Duplicate field key "${u}" \u2014 fields will share state and overwrite each other`),n.add(u);let s=(u,d,c,f)=>{var v;let y=S(c);for(let b of y)n.has(b)||e.push(`${u} "${d}" has show condition referencing non-existent field "${b}"`);if(y.length)for(let b of f)r.set(b,[...(v=r.get(b))!=null?v:[],...y]);};for(let u of i){s("Field",u.key,u,[u.key]);let d=(l=u.validation)==null?void 0:l.pattern;if(d!==void 0)try{new RegExp(d);}catch(c){e.push(`Field "${u.key}" has invalid regex pattern: "${d}"`);}}let o=new Set;for(let u of (a=t.steps)!=null?a:[]){s("Step",u.id,u,u.fields.map(d=>d.key));for(let d of S(u))F(t)&&n.has(d)&&!o.has(d)&&e.push(`Step "${u.id}" condition field "${d}" must belong to an earlier step.`);for(let{key:d}of u.fields)o.add(d);u.skip&&(u.next===false||!t.steps[1])&&e.push(`Step "${u.id}" has "skip" but no Next button or later step.`);}return {graph:r,warnings:e}}function Tt(t){var i;let e=new Map,r=[];for(let n of t.keys()){if(e.has(n))continue;e.set(n,true);let s=[[n,0]];for(;s.length>0;){let o=s[s.length-1],l=(i=t.get(o[0]))==null?void 0:i[o[1]++];if(l===void 0)e.set(o[0],false),s.pop();else if(e.get(l)){let a=s.map(([u])=>u);r.push([...a.slice(a.indexOf(l)),l]);}else e.has(l)||(e.set(l,true),s.push([l,0]));}}return r}function O(t){let{graph:e,warnings:r}=pt(t);for(let i of Tt(e))r.push(`Circular show condition detected: ${i.join(" -> ")}`);return [...V(t),...r,...ut(t)]}function Kt(t){let e=V(t),r=new Set(e);return {errors:e,warnings:O(t).filter(i=>!r.has(i))}}var et=class extends Error{constructor(r,i){super(`The action completed, but ${r} failed: ${i instanceof Error?i.message:String(i)}`);this.phase=r;this.committed=true;this.name="FormLifecycleError",this.cause=i;}};async function B(t,e){try{await e();}catch(r){throw new et(t,r)}}function ft(t){return t.visibleSteps.map(e=>e.id).join(`
3
- `)}function L(t,e){let r=ft(t);return ()=>ft(t)!==r||Object.keys(e).some(i=>e[i]!==void 0&&!Object.is(t.values[i],e[i]))}var m={email:"Enter a valid email",number:"Enter a number",boolean:"Must be true or false",option:"Select one of the available options",options:"Must be a list of available options",string:"Must be text",date:"Enter a valid date",datetime:"Enter a valid date and time",file:"Must be a file reference",value:"Invalid value"};function k(t,e){var r,i;switch(t){case "required":return "This field is required";case "minLength":return `Must be at least ${e==null?void 0:e.min} ${(r=e==null?void 0:e.unit)!=null?r:"characters"}`;case "maxLength":return `Must be at most ${e==null?void 0:e.max} ${(i=e==null?void 0:e.unit)!=null?i:"characters"}`;case "min":return `Must be at least ${e==null?void 0:e.min}`;case "max":return `Must be at most ${e==null?void 0:e.max}`;case "pattern":return "Invalid format";case "matchField":return "Fields must match";default:return "Invalid value"}}var rt=new Set(["checkbox","switch"]);function Pt(t,e){return K(t)||t===false&&rt.has(e.type)}function ht(t,e,r,i){var o,l;let n=r[`min${e}`],s=r[`max${e}`];if(n!==void 0&&t<n)return (o=r[`min${e}Message`])!=null?o:k(`min${e}`,{min:n,unit:i});if(s!==void 0&&t>s)return (l=r[`max${e}Message`])!=null?l:k(`max${e}`,{max:s,unit:i})}function Bt(t,e){var r;if(e.pattern!==void 0)try{return new RegExp(e.pattern).test(String(t))?void 0:(r=e.patternMessage)!=null?r:k("pattern")}catch(i){return}}function M(t,e,r,i){var l,a,u,d;let n=t.validation;if(!n)return null;if(Pt(e,t))return n.required?typeof n.required=="string"?n.required:k("required"):null;let s=typeof e=="string"||Array.isArray(e)?ht(e.length,"Length",n,Array.isArray(e)?"items":void 0):typeof e=="number"?ht(e,"",n):void 0,o=n.matchField!==void 0&&e!==r[n.matchField]?(l=n.matchFieldMessage)!=null?l:k("matchField"):void 0;return ((d=(a=s!=null?s:Bt(e,n))!=null?a:o)!=null?d:n.validator?(u=i==null?void 0:i[n.validator])==null?void 0:u.call(i,e,r):void 0)||null}function R(t,e,r){return G(t.fields,e,r)}function G(t,e,r){let i={};for(let n of t){if(!h(n,e))continue;let s=M(n,e[n.key],e,r);s&&(i[n.key]=s);}return i}function q(t,e,r=false){let i=t.errors;t.errors={},t.topLevelErrors=[];for(let[l,a]of Object.entries(e))t.visibility.isFieldVisible(l,t.values)?t.errors[l]=a:t.topLevelErrors.push(a);let n=Object.keys(e).find(l=>t.errors[l]!==void 0),s=r&&n&&t.isMultiStep?t.visibility.findStepIndex(n,t.values,t.currentStepIndex):null,o=s!==null&&s!==t.currentStepIndex;s!==null&&(t.currentStepIndex=s),t.notify({fieldKeys:E(i,t.errors),structureChanged:o});}function mt(t,e){var i;let r=[];for(let n of t.visibility.allFields)((i=n.validation)==null?void 0:i.matchField)!==e||t.errors[n.key]===void 0||M(n,t.values[n.key],D(t),t.validators)||(delete t.errors[n.key],r.push(n.key));return r}function gt(t){let e=new Set(t.visibleSteps.flatMap(i=>i.fields.map(n=>n.key))),r=Object.keys(t.errors).filter(i=>!e.has(i));for(let i of r)delete t.errors[i];return r}function D(t){return F(t.definition)?C(t):yt(t,p({},t.values))}function yt(t,e){for(let r of [...t.skipped.values()].flat())delete e[r];return e}function $(t){var i;let e=t.errors,r={};if(t.isMultiStep)for(let n of t.visibleSteps)t.skipped.has(n.id)||Object.assign(r,R(n,D(t),t.validators));else Object.assign(r,G((i=t.definition.fields)!=null?i:[],t.values,t.validators));return t.errors=r,t.notify({fieldKeys:E(e,r)}),r}function bt(t,e){let r=t.visibility.fieldByKey.get(e);if(!r||!t.visibility.isFieldVisible(e,t.values))return null;let i=t.errors[e],n=M(r,t.values[e],D(t),t.validators);n?t.errors[e]=n:delete t.errors[e];let s=Object.is(i,t.errors[e])?[]:[e];return t.notify({fieldKeys:s}),n}function C(t){let e={},r=t.visibility.getVisibleFieldKeys(t.values,t.currentStepIndex);for(let i of r)t.values[i]!==void 0&&(e[i]=t.values[i]);return yt(t,e)}function St(t,e){let r=t.values,i=t.errors,n=Object.keys(t.fieldLoading);t.validationEpoch++,t.operationEpoch++,t.submitting=false,t.values=I(t.visibility.allFields,e),t.errors={},t.topLevelErrors=[],t.fieldLoading={},t.stepValidating=false,t.currentStepIndex=0,t.skipped.clear(),t.visibility.reconcileHidden(t.values,t.errors);let s=E(r,t.values),o=new Set([...s,...E(i,t.errors),...n]);t.notify({fieldKeys:o,structureChanged:true,valuesChanged:s.size>0});}function vt(t){var i,n;let e=(n=(i=t.currentStep)==null?void 0:i.fields)!=null?n:[],r=p({},t.values);for(let{key:s}of e)delete r[s];return Object.assign(r,I(e))}function H(t){return tt(t.definition,vt(t))[t.currentStepIndex+1]}function j(t){let e=t.currentStep,r=e.fields.map(({key:i})=>i);t.values=vt(t);for(let i of r)delete t.errors[i];return t.skipped.set(e.id,r),t.visibility.reconcileHidden(t.values,t.errors),t.notify({fieldKeys:r,structureChanged:true,valuesChanged:true}),r}function N(t){let e=t.currentStep;e&&t.skipped.delete(e.id);}function Ft(t,e){return t.currentStepIndex=e,t.notify({structureChanged:true}),j(t)}async function _(t,e,r=false){if(t.submitting||t.stepValidating)return false;r||N(t);let i=p({},t.lifecycle),n=false,s=t.validationEpoch,o=++t.operationEpoch,l=C(t),a=L(t,l),u=()=>s!==t.validationEpoch||o!==t.operationEpoch||a();t.submitting=true,t.notify();try{if(u()||Object.keys($(t)).length>0||u())return !1;if(i.onBeforeSubmit){let d=await i.onBeforeSubmit(l);if(u()||d===!1)return !1}if(u())return !1;if(i.onBeforeSubmit){if(Object.keys($(t)).length>0||u())return !1;l=C(t);}return n=!0,await e(l,[...t.skipped.keys()]),o!==t.operationEpoch||await B("afterSubmit",()=>{var d;return (d=i.onAfterSubmit)==null?void 0:d.call(i,l)}),!0}catch(d){if(!n&&u())return false;throw d}finally{o===t.operationEpoch&&(t.submitting=false,t.notify());}}function it(t){let e=t.currentStep;if(!e)return true;let r=R(e,D(t),t.validators);if(Object.keys(r).length===0)return false;let i=p({},t.errors);return Object.assign(t.errors,r),t.notify({fieldKeys:E(i,t.errors)}),true}function z(t,e){t.validationEpoch++,t.currentStepIndex=e,t.notify({structureChanged:true});}function wt(t){return t.isLastStep?false:(z(t,t.currentStepIndex+1),true)}function kt(t){return t.isMultiStep?(N(t),!it(t)&&wt(t)):false}function xt(t){return !t.isMultiStep||!H(t)?false:(j(t),wt(t))}async function Et(t,e){if(!t.isMultiStep||t.stepValidating||t.submitting)return false;if(H(t))return Y(t,"next","skip");let r=t.currentStep;if(!e||!r)return false;let i=t.values,n=p({},t.errors),s=false,o=j(t),l=t.values,a=L(t,p({},l));try{return await _(t,async(u,d)=>{await e(u,d),s=!0;},!0)}finally{!s&&t.values===l&&!a()&&(t.values=i,t.errors=p(p({},n),t.errors),t.skipped.delete(r.id),t.notify({fieldKeys:o,structureChanged:true,valuesChanged:true}));}}async function Y(t,e,r=e){let i=r==="skip";if(!t.isMultiStep||t.stepValidating||t.submitting||!i&&(e==="next"?t.isLastStep:t.isFirstStep))return false;let n=t.currentStep,s=e==="next"&&!i;if(s&&N(t),s&&it(t))return false;let o=p({},t.lifecycle),l=t.onStepValidate,a=false,d=t.currentStepIndex+(e==="next"?1:-1),c=i?H(t):t.visibleSteps[d];if(!c)return false;let f={fromStepId:n.id,toStepId:c.id,direction:e,reason:r,values:p({},D(t))},y=t.validationEpoch,v=++t.operationEpoch,b=L(t,f.values),A=()=>{var g;return y!==t.validationEpoch||v!==t.operationEpoch||((g=t.currentStep)==null?void 0:g.id)!==n.id||b()};t.stepValidating=true,t.notify();try{if(A())return !1;if(s&&l){let g=await l(n.id,f.values);if(A())return !1;if(g&&Object.keys(g).length>0)return q(t,g),!1}if(o.onBeforeStepChange){let g=await o.onBeforeStepChange(f);if(A()||g===!1)return !1}return A()||s&&it(t)?!1:(a=!0,i&&j(t),z(t,d),await B("afterStepChange",()=>{var g;return (g=o.onAfterStepChange)==null?void 0:g.call(o,f)}),!0)}catch(g){if(!a&&A())return false;throw g}finally{v===t.operationEpoch&&(t.stepValidating=false,t.notify());}}function Rt(t){t.isMultiStep&&!t.isFirstStep&&z(t,t.currentStepIndex-1);}function It(t,e){if(!t.isMultiStep)return;let r=t.visibility.findStepIndex(e,t.values,t.currentStepIndex);r===t.currentStepIndex?t.notify():r!==null&&z(t,r);}var W=class{constructor(){this.version=0;this.listeners=new Set;this.fieldVersions=new Map;this.fieldListeners=new Map;this.structureVersion=0;this.structureListeners=new Set;}subscribe(e){return this.listeners.add(e),()=>this.listeners.delete(e)}getSnapshot(){return this.version}subscribeField(e,r){var n;let i=(n=this.fieldListeners.get(e))!=null?n:new Set;return i.add(r),this.fieldListeners.set(e,i),()=>{i.delete(r),i.size===0&&this.fieldListeners.delete(e);}}getFieldSnapshot(e){var r;return (r=this.fieldVersions.get(e))!=null?r:0}subscribeStructure(e){return this.structureListeners.add(e),()=>this.structureListeners.delete(e)}getStructureSnapshot(){return this.structureVersion}notify({fieldKeys:e=[],structureChanged:r=false}={}){for(let i of new Set(e))this.notifyField(i);r&&this.notifyStructure(),this.version++;for(let i of this.listeners)i();}notifyField(e){var r,i;this.fieldVersions.set(e,((r=this.fieldVersions.get(e))!=null?r:0)+1);for(let n of (i=this.fieldListeners.get(e))!=null?i:[])n();}notifyStructure(){this.structureVersion++;for(let e of this.structureListeners)e();}};function nt(t,e){var r;for(let i of S(t)){let n=(r=e.get(i))!=null?r:new Set;n.add(t),e.set(i,n);}}var X=class{constructor(e){this.definition=e;this.routeFields=new Set;this.fieldDependents=new Map;this.stepDependents=new Map;this.stepByFieldKey=new Map;this.visibilityDirty=true;this.canGoNextDirty=true;this.cache={visibleSteps:[],currentStep:null,visibleFields:[],canGoNext:false};var r;this.allFields=w(e),this.fieldByKey=new Map(this.allFields.map(i=>[i.key,i])),this.path=new P(e);for(let i of (r=e.steps)!=null?r:[])for(let n of i.fields)this.stepByFieldKey.set(n.key,i);this.buildIndexes();}markChanged(e,r){(e||r)&&this.path.invalidate(),e?(this.visibilityDirty=true,this.canGoNextDirty=true):r&&(this.canGoNextDirty=true);}state(e,r){return this.recomputeVisibility(e,r),this.cache}getCanGoNext(e,r,i){if(this.recomputeVisibility(e,r),!this.canGoNextDirty)return this.cache.canGoNext;this.canGoNextDirty=false;let n=this.cache.currentStep,s=n?R(n,this.conditionValues(e),i):{};return this.cache.canGoNext=this.isMultiStep&&Object.keys(s).length===0,this.cache.canGoNext}findStepIndex(e,r,i){let s=this.state(r,i).visibleSteps.findIndex(o=>o.fields.some(l=>l.key===e));return s===-1?null:s}affectsStructure(e,r){return this.routeFields.has(e)||this.fieldDependents.has(e)||this.stepDependents.has(e)||r.size>0}cascadeHiddenFields(e,r,i){return this.path.invalidate(),this.hasRoutes?this.reconcileHidden(r,i):this.drainQueue(new Set([e]),r,i)}reconcileHidden(e,r){this.path.invalidate();let i=new Set([...this.fieldDependents.keys(),...this.stepDependents.keys()]);return this.drainQueue(i,e,r)}drainQueue(e,r,i){let n=[...e],s=new Set(n),o=new Set,l=a=>{delete r[a],delete i[a],this.path.invalidate(),o.add(a),!s.has(a)&&(s.add(a),n.push(a));};for(let a=0;a<n.length;a++){let u=n[a];s.delete(u),this.clearHiddenFields(this.fieldDependents.get(u),r,l),this.clearHiddenSteps(this.stepDependents.get(u),r,l);}return o}isFieldVisible(e,r){let i=this.fieldByKey.get(e);if(!i||!h(i,this.conditionValues(r)))return false;let n=this.stepByFieldKey.get(e);return !n||this.path.steps(r).includes(n)}getVisibleFieldKeys(e,r){var s;if(this.hasRoutes)return new Set(Object.keys(this.path.values(e)));let i=new Set,n=this.isMultiStep?this.state(e,r).visibleSteps.flatMap(o=>o.fields):(s=this.definition.fields)!=null?s:[];for(let o of n)h(o,this.conditionValues(e))&&i.add(o.key);return i}conditionValues(e){return this.hasRoutes?this.path.values(e):e}get hasRoutes(){return this.path.enabled}get isMultiStep(){var e;return ((e=this.definition.steps)!=null?e:[]).length>0}buildIndexes(){var e,r;for(let i of this.allFields)nt(i,this.fieldDependents);for(let i of (e=this.definition.steps)!=null?e:[]){nt(i,this.stepDependents);for(let n of (r=i.routes)!=null?r:[])for(let s of S(n))this.routeFields.add(s);}}recomputeVisibility(e,r){var s,o,l;if(!this.visibilityDirty)return;if(this.visibilityDirty=false,!this.isMultiStep){this.cache.visibleSteps=[],this.cache.currentStep=null,this.cache.visibleFields=((s=this.definition.fields)!=null?s:[]).filter(a=>h(a,this.conditionValues(e)));return}this.cache.visibleSteps=this.path.steps(e);let i=this.cache.visibleSteps;this.cache.currentStep=(l=(o=i[r])!=null?o:i[i.length-1])!=null?l:null;let n=this.cache.currentStep;this.cache.visibleFields=n?n.fields.filter(a=>h(a,this.conditionValues(e))):[];}clearHiddenFields(e,r,i){for(let n of e!=null?e:[]){let s=this.stepByFieldKey.get(n.key);this.hasRoutes&&s&&!this.path.steps(r).includes(s)||h(n,this.conditionValues(r))||r[n.key]===void 0||i(n.key);}}clearHiddenSteps(e,r,i){for(let n of e!=null?e:[])if(!x(n,this.conditionValues(r)))for(let s of n.fields)r[s.key]!==void 0&&i(s.key);}};var Q=class{constructor(e,r,i){this.definition=e;this.errors={};this.topLevelErrors=[];this.currentStepIndex=0;this.fieldLoading={};this.stepValidating=false;this.submitting=false;this.skipped=new Map;this.validationEpoch=0;this.operationEpoch=0;this.subscriptions=new W;var o;let n=V(e);if(n.length)throw new Error(n.join(`
4
- `));this.lifecycle=i!=null?i:{},this.validators=(o=i==null?void 0:i.validators)!=null?o:{},this.visibility=new X(e);let s=O(e);for(let{key:l,validation:a}of this.visibility.allFields){let u=a==null?void 0:a.validator;u&&!this.validators[u]&&s.push(`Field "${l}" references unknown validator "${u}".`);}for(let l of s)console.warn(`[FormEngine] ${l}`);this.values=I(this.visibility.allFields,r),this.visibility.reconcileHidden(this.values,this.errors);}get onStepValidate(){return this.lifecycle.onStepValidate}get isMultiStep(){var e;return ((e=this.definition.steps)!=null?e:[]).length>0}get visibleSteps(){return this.visibility.state(this.values,this.currentStepIndex).visibleSteps}get currentStep(){return this.visibility.state(this.values,this.currentStepIndex).currentStep}get visibleFields(){return this.visibility.state(this.values,this.currentStepIndex).visibleFields}get isFirstStep(){return this.currentStepIndex===0}get isLastStep(){return this.currentStepIndex>=this.visibleSteps.length-1}get canGoNext(){return this.visibility.getCanGoNext(this.values,this.currentStepIndex,this.validators)}get progress(){let e=this.visibleSteps.length;return {current:Math.min(this.currentStepIndex+1,e),total:e}}subscribe(e){return this.subscriptions.subscribe(e)}getSnapshot(){return this.subscriptions.getSnapshot()}subscribeField(e,r){return this.subscriptions.subscribeField(e,r)}getFieldSnapshot(e){return this.subscriptions.getFieldSnapshot(e)}subscribeStructure(e){return this.subscriptions.subscribeStructure(e)}getStructureSnapshot(){return this.subscriptions.getStructureSnapshot()}setValue(e,r){let i=this.isMultiStep?this.visibleSteps:null,n=!Object.is(this.values[e],r),s=this.errors[e]!==void 0;this.values[e]=r,delete this.errors[e];for(let[c,f]of this.skipped)n&&f.includes(e)&&this.skipped.delete(c);let o=this.visibility.cascadeHiddenFields(e,this.values,this.errors),l=mt(this.internals,e),a=new Set([...o,...l]);(n||s)&&a.add(e);let u=n||o.size>0,d=u&&this.visibility.affectsStructure(e,o);if(i&&d){this.visibility.markChanged(true,true),this.currentStepIndex=dt(i,this.visibleSteps,this.currentStepIndex);for(let c of gt(this.internals))a.add(c);}this.notify({fieldKeys:a,structureChanged:d,valuesChanged:u});}setErrors(e){q(this.internals,e,true);}setFieldLoading(e,r){let i=this.fieldLoading[e]===true;r?this.fieldLoading[e]=true:delete this.fieldLoading[e],this.notify({fieldKeys:i===r?[]:[e]});}cancelPendingActions(){this.validationEpoch++,this.operationEpoch++,this.stepValidating=false,this.submitting=false,this.notify();}nextStep(){return kt(this.internals)}nextStepAsync(e="next"){return Y(this.internals,"next",e)}prevStepAsync(){return Y(this.internals,"back")}skipStep(){return xt(this.internals)}skipStepAsync(e){return Et(this.internals,e)}isStepSkipped(e){return this.skipped.has(e)}submitAsync(e){return _(this.internals,e)}prevStep(){Rt(this.internals);}validate(){return $(this.internals)}validateField(e){return bt(this.internals,e)}getSubmitValues(){return C(this.internals)}goToStepWithField(e){It(this.internals,e);}reset(e){St(this.internals,e);}get internals(){return this}notify(e={}){this.visibility.markChanged(!!e.structureChanged,!!e.valuesChanged),this.subscriptions.notify(e);}};var T=1e4,Gt=254,qt=1e3,Ht=/^[^\s@]+@[^\s@.]+(?:\.[^\s@.]+)+$/,_t=/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/,Vt="\\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\\d|3[01])",zt=new RegExp(`^${Vt}$`),Yt=new RegExp(`^${Vt}T(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d(?:\\.\\d{1,3})?)?(?:Z|[+-](?:[01]\\d|2[0-3]):[0-5]\\d)?$`),Wt=new Set(["text","phone","password","textarea","autocomplete"]),Xt=new Map([["email",[Ht,m.email,Gt]],["date",[zt,m.date,T]],["datetime",[Yt,m.datetime,T]]]);function Mt(t,e){if(t.type!=="number"||typeof e!="string"||!_t.test(e))return e;let r=Number(e);return Number.isFinite(r)?r:e}function Dt(t,e){return !t.options||t.options.some(r=>r.value===e)}function Ct(t,e){var i,n,s,o;let r=Math.min(T,(n=(i=t.validation)==null?void 0:i.maxLength)!=null?n:T);return e.length<=r?null:(o=(s=t.validation)==null?void 0:s.maxLengthMessage)!=null?o:k("maxLength",{max:r})}function Qt(t,e){var i,n;return Array.isArray(e)?e.length>((n=(i=t.options)==null?void 0:i.length)!=null?n:qt)?m.option:e.every(s=>typeof s=="string"&&s.length<=T&&Dt(t,s))?null:m.option:m.options}function Z(t,e,r){return typeof e=="string"?Ct(t,e):r}function Zt(t,e,[r,i,n]){var s;return typeof e!="string"?i:(s=Ct(t,e))!=null?s:e.length<=n&&r.test(e)?null:i}function Jt(t,e){return typeof e=="boolean"||typeof e=="number"&&Number.isFinite(e)?null:Z(t,e,m.value)}function Ut(t,e){return e==null||e===""||t.type==="multiselect"&&Array.isArray(e)&&!e.length}function At(t,e){var i;if(Ut(t,e))return null;if(rt.has(t.type))return typeof e=="boolean"?null:m.boolean;if(t.type==="number")return typeof e=="number"&&Number.isFinite(e)?null:m.number;if(t.type==="multiselect")return Qt(t,e);if(t.type==="file")return Z(t,e,m.file);let r=Xt.get(t.type);return r?Zt(t,e,r):t.type==="select"||t.type==="radio"?(i=Z(t,e,m.option))!=null?i:Dt(t,e)?null:m.option:Wt.has(t.type)?Z(t,e,m.string):Jt(t,e)}var te=()=>null,st=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);function ee(t,e){let r=e!==null&&typeof e=="object"&&!Array.isArray(e)?e:{},i={};for(let n of t)st(r,n.key)&&(i[n.key]=Mt(n,r[n.key]));return i}function re(t,e){let r=new Map;for(let i of t){let n=st(e,i.key)?At(i,e[i.key]):null;n&&r.set(i.key,n);}return r}function ie(t,e){var n;let r=s=>s.map(o=>e.has(o.key)?J(p({},o),{validation:void 0}):o),i=(n=t.steps)==null?void 0:n.map(s=>J(p({},s),{fields:r(s.fields)}));return p(p(p({},t),t.fields&&{fields:r(t.fields)}),i&&{steps:i})}function ne(t){let e={};for(let{validation:r}of t)r!=null&&r.validator&&(e[r.validator]=te);return e}function se(t,e){if(!Array.isArray(e))return;let r=new Set(e);for(let i=0;i<t.visibleSteps.length;i++){let n=t.visibleSteps[i];n.skip&&r.has(n.id)&&Ft(t,i);}}function oe(t,e,r={}){let i=w(t),n=ee(i,e),s=re(i,n),o=new Q(ie(t,s),n,{validators:ne(i)});se(o,r.skippedSteps);let l=o.validate(),a=o.getSubmitValues();for(let[u,d]of s)st(a,u)&&(l[u]=d);return {values:a,errors:l}}exports.checkDefinition=Kt;exports.validateSubmission=oe;return exports;})({});//# sourceMappingURL=server.iife.js.map
2
+ var Gt=Object.defineProperty,qt=Object.defineProperties;var Ht=Object.getOwnPropertyDescriptors;var pt=Object.getOwnPropertySymbols;var _t=Object.prototype.hasOwnProperty,zt=Object.prototype.propertyIsEnumerable;var ft=(t,e,r)=>e in t?Gt(t,e,{enumerable:true,configurable:true,writable:true,value:r}):t[e]=r,p=(t,e)=>{for(var r in e||(e={}))_t.call(e,r)&&ft(t,r,e[r]);if(pt)for(var r of pt(e))zt.call(e,r)&&ft(t,r,e[r]);return t},rt=(t,e)=>qt(t,Ht(e));function B(t){return t==null||t===""||Array.isArray(t)&&!t.length}function it(t,e){let r=e[t.field];return t.eq!==void 0?r===t.eq:t.neq!==void 0?r!==t.neq:t.in!==void 0?t.in.includes(r):t.notIn!==void 0?!t.notIn.includes(r):t.notEmpty===true?!B(r):true}function h(t,e){var i,n;let r=s=>it(s,e);return ((i=t.show)!=null?i:[]).every(r)&&(!((n=t.showAny)!=null&&n.length)||t.showAny.some(r))}var x=h;function ht(t){var e,r;return !!((e=t.fields)!=null&&e.length)&&!!((r=t.steps)!=null&&r.length)}function v(t){var e;return !!((e=t.steps)!=null&&e.some(r=>{var i;return (i=r.routes)==null?void 0:i.length}))}function S(t){var e,r;return [...(e=t.show)!=null?e:[],...(r=t.showAny)!=null?r:[]].map(i=>i.field)}function w(t){var e,r;return (e=t.steps)!=null&&e.length?t.steps.flatMap(i=>i.fields):(r=t.fields)!=null?r:[]}function I(t,e){let r={};for(let i of t)i.defaultValue!==void 0&&(r[i.key]=i.defaultValue);return Object.assign(r,e)}function E(t,e){let r=new Set([...Object.keys(t),...Object.keys(e)]);return new Set([...r].filter(i=>!Object.is(t[i],e[i])))}function V(t){var s;let e=(s=t.steps)!=null?s:[],r=ht(t)?['FormEngine rejects a definition with both "fields" and "steps".']:[],i=new Map,n=new Map;e.forEach((o,l)=>{i.has(o.id)&&r.push(`Duplicate step id "${o.id}".`),i.set(o.id,l);for(let u of o.fields)n.set(u.key,l);});for(let{key:o}of w(t))(o in Object.prototype||o==="prototype")&&r.push(`Reserved field key "${o}".`);return v(t)&&e.forEach((o,l)=>{var u,a;for(let d of (u=o.routes)!=null?u:[]){let c=d.to===null?null:i.get(d.to);(c===void 0||c!==null&&c<=l)&&r.push(`Invalid route from "${o.id}" to "${d.to}": targets must be later steps or null.`);for(let f of S(d))((a=n.get(f))!=null?a:1/0)<=l||r.push(`Invalid route on "${o.id}": condition field "${f}" must belong to this or an earlier step.`);}}),r}function mt(t){var i,n,s,o;let e=(i=t.steps)!=null?i:[],r=[];for(let l of e){let u=new Set(((n=l.routes)!=null?n:[]).map(a=>a.to).filter(a=>a!==null));for(let a of u){let d=e.findIndex(b=>b.id===a),c=e[d+1],f=(o=(s=e[d])==null?void 0:s.routes)==null?void 0:o.some(b=>{var F,y;return !((F=b.show)!=null&&F.length)&&!((y=b.showAny)!=null&&y.length)});c&&u.has(c.id)&&!f&&r.push(`Route branch "${a}" from "${l.id}" continues into sibling branch "${c.id}". Add an unconditional route where it should continue.`);}}return r}function nt(t,e){var o,l;let r=(o=t.steps)!=null?o:[];if(!v(t))return r.filter(u=>x(u,e));let i=new Map(r.map((u,a)=>[u.id,a])),n=[],s={};for(let u=0;u<r.length;){let a=r[u];if(!x(a,s)){u++;continue}n.push(a),Object.assign(s,yt(a,e,s));let d=(l=a.routes)==null?void 0:l.find(c=>{var f;return h(c,s)&&(c.to===null||((f=i.get(c.to))!=null?f:-1)>u&&x(r[i.get(c.to)],s))});if((d==null?void 0:d.to)===null)break;u=d?i.get(d.to):u+1;}return n}function gt(t,e,r){var o;let i=(o=t[r])!=null?o:t[t.length-1],n=e.findIndex(l=>l.id===(i==null?void 0:i.id));if(n!==-1)return n;let s=new Map(e.map((l,u)=>[l.id,u]));for(let l=Math.min(r-1,t.length-1);l>=0;l--){let u=s.get(t[l].id);if(u!==void 0)return u}return 0}function yt(t,e,r){let i=p({},r);for(let s of t.fields)i[s.key]=e[s.key];let n={};for(let s of t.fields)h(s,i)&&e[s.key]!==void 0&&(n[s.key]=e[s.key]);return n}var G=class{constructor(e){this.definition=e;this.source=null;this.activeStepList=[];this.activeValueMap={};this.enabled=v(e);}invalidate(){this.source=null;}steps(e){return this.refresh(e),this.activeStepList}values(e){return this.refresh(e),this.activeValueMap}refresh(e){if(this.source===e)return;this.source=e,this.activeStepList=nt(this.definition,e);let r={};for(let i of this.activeStepList)Object.assign(r,yt(i,e,r));this.activeValueMap=r;}};function bt(t){var l,u;let e=[],r=new Map,i=w(t),n=new Set;for(let{key:a}of i)n.has(a)&&e.push(`Duplicate field key "${a}" \u2014 fields will share state and overwrite each other`),n.add(a);let s=(a,d,c,f)=>{var F;let b=S(c);for(let y of b)typeof y!="string"?e.push(`${a} "${d}" has a show condition without a "field" name`):n.has(y)||e.push(`${a} "${d}" has show condition referencing non-existent field "${y}"`);if(b.length)for(let y of f)r.set(y,[...(F=r.get(y))!=null?F:[],...b]);};for(let a of i){s("Field",a.key,a,[a.key]);let d=(l=a.validation)==null?void 0:l.pattern;if(d!==void 0)try{new RegExp(d);}catch(c){e.push(`Field "${a.key}" has invalid regex pattern: "${d}"`);}}let o=new Set;for(let a of (u=t.steps)!=null?u:[]){s("Step",a.id,a,a.fields.map(d=>d.key));for(let d of S(a))v(t)&&n.has(d)&&!o.has(d)&&e.push(`Step "${a.id}" condition field "${d}" must belong to an earlier step.`);for(let{key:d}of a.fields)o.add(d);a.skip&&(a.next===false||!t.steps[1])&&e.push(`Step "${a.id}" has "skip" but no Next button or later step.`);}return {graph:r,warnings:e}}var q=t=>typeof t=="object"&&t!==null&&!Array.isArray(t);function M(t,e,r,i=false){return t==null&&!i?[]:Array.isArray(t)?t.flatMap((n,s)=>r(n,`${e}[${s}]`)):[`${e} must be an array.`]}function H(t,e){return (r,i)=>q(r)?e(r,i):[`${i} must be ${t}.`]}var St=H("a condition object",()=>[]);function st(t,e){return [...M(t.show,`${e}.show`,St),...M(t.showAny,`${e}.showAny`,St)]}var Ft=H("a field object",st),Wt=H("a route object",st),Yt=H("a step object",(t,e)=>[...M(t.fields,`${e}.fields`,Ft,true),...st(t,e),...M(t.routes,`${e}.routes`,Wt)]);function A(t){return q(t)?[...M(t.fields,"fields",Ft),...M(t.steps,"steps",Yt)]:["Form definition must be an object."]}function Xt(t){var i;let e=new Map,r=[];for(let n of t.keys()){if(e.has(n))continue;e.set(n,true);let s=[[n,0]];for(;s.length>0;){let o=s[s.length-1],l=(i=t.get(o[0]))==null?void 0:i[o[1]++];if(l===void 0)e.set(o[0],false),s.pop();else if(e.get(l)){let u=s.map(([a])=>a);r.push([...u.slice(u.indexOf(l)),l]);}else e.has(l)||(e.set(l,true),s.push([l,0]));}}return r}function L(t){let e=A(t);if(e.length)return e;let r=t,{graph:i,warnings:n}=bt(r);q(r.submit)||n.unshift('Form definition is missing the "submit" action.');for(let s of Xt(i))n.push(`Circular show condition detected: ${s.join(" -> ")}`);return [...V(r),...n,...mt(r)]}function Qt(t){let e=A(t);if(e.length)return {errors:e,warnings:[]};let r=t,i=V(r),n=new Set(i);return {errors:i,warnings:L(r).filter(s=>!n.has(s))}}var ot=class extends Error{constructor(r,i){super(`The action completed, but ${r} failed: ${i instanceof Error?i.message:String(i)}`);this.phase=r;this.committed=true;this.name="FormLifecycleError",this.cause=i;}};async function _(t,e){try{await e();}catch(r){throw new ot(t,r)}}function vt(t){return t.visibleSteps.map(e=>e.id).join(`
3
+ `)}function j(t,e){let r=vt(t);return ()=>vt(t)!==r||Object.keys(e).some(i=>e[i]!==void 0&&!Object.is(t.values[i],e[i]))}var m={email:"Enter a valid email",number:"Enter a number",boolean:"Must be true or false",option:"Select one of the available options",options:"Must be a list of available options",string:"Must be text",date:"Enter a valid date",datetime:"Enter a valid date and time",file:"Must be a file reference",value:"Invalid value"};function k(t,e){var r,i;switch(t){case "required":return "This field is required";case "minLength":return `Must be at least ${e==null?void 0:e.min} ${(r=e==null?void 0:e.unit)!=null?r:"characters"}`;case "maxLength":return `Must be at most ${e==null?void 0:e.max} ${(i=e==null?void 0:e.unit)!=null?i:"characters"}`;case "min":return `Must be at least ${e==null?void 0:e.min}`;case "max":return `Must be at most ${e==null?void 0:e.max}`;case "pattern":return "Invalid format";case "matchField":return "Fields must match";default:return "Invalid value"}}var at=new Set(["checkbox","switch"]);function Zt(t,e){return B(t)||t===false&&at.has(e.type)}function wt(t,e,r,i){var o,l;let n=r[`min${e}`],s=r[`max${e}`];if(n!==void 0&&t<n)return (o=r[`min${e}Message`])!=null?o:k(`min${e}`,{min:n,unit:i});if(s!==void 0&&t>s)return (l=r[`max${e}Message`])!=null?l:k(`max${e}`,{max:s,unit:i})}function Jt(t,e){var r;if(e.pattern!==void 0)try{return new RegExp(e.pattern).test(String(t))?void 0:(r=e.patternMessage)!=null?r:k("pattern")}catch(i){return}}function D(t,e,r,i){var l,u,a,d;let n=t.validation;if(!n)return null;if(Zt(e,t))return n.required?typeof n.required=="string"?n.required:k("required"):null;let s=typeof e=="string"||Array.isArray(e)?wt(e.length,"Length",n,Array.isArray(e)?"items":void 0):typeof e=="number"?wt(e,"",n):void 0,o=n.matchField!==void 0&&e!==r[n.matchField]?(l=n.matchFieldMessage)!=null?l:k("matchField"):void 0;return ((d=(u=s!=null?s:Jt(e,n))!=null?u:o)!=null?d:n.validator?(a=i==null?void 0:i[n.validator])==null?void 0:a.call(i,e,r):void 0)||null}function R(t,e,r){return z(t.fields,e,r)}function z(t,e,r){let i={};for(let n of t){if(!h(n,e))continue;let s=D(n,e[n.key],e,r);s&&(i[n.key]=s);}return i}function W(t,e,r=false){let i=t.errors;t.errors={},t.topLevelErrors=[];for(let[s,o]of Object.entries(e))t.visibility.isFieldVisible(s,t.values)?t.errors[s]=o:t.topLevelErrors.push(o);let n=r&&kt(t,e);t.notify({fieldKeys:E(i,t.errors),structureChanged:n});}function kt(t,e){let r=Object.keys(e).find(n=>t.errors[n]!==void 0),i=r&&t.isMultiStep?t.visibility.findStepIndex(r,t.values,t.currentStepIndex):null;return i===null||i===t.currentStepIndex?false:(t.currentStepIndex=i,true)}function xt(t,e){var i;let r=[];for(let n of t.visibility.allFields)((i=n.validation)==null?void 0:i.matchField)!==e||t.errors[n.key]===void 0||D(n,t.values[n.key],C(t),t.validators)||(delete t.errors[n.key],r.push(n.key));return r}function Et(t){let e=new Set(t.visibleSteps.flatMap(i=>i.fields.map(n=>n.key))),r=Object.keys(t.errors).filter(i=>!e.has(i));for(let i of r)delete t.errors[i];return r}function C(t){return v(t.definition)?O(t):Rt(t,p({},t.values))}function Rt(t,e){for(let r of [...t.skipped.values()].flat())delete e[r];return e}function T(t,e=false){var s;let r=t.errors,i={};if(t.isMultiStep)for(let o of t.visibleSteps)t.skipped.has(o.id)||Object.assign(i,R(o,C(t),t.validators));else Object.assign(i,z((s=t.definition.fields)!=null?s:[],t.values,t.validators));t.errors=i;let n=e&&kt(t,i);return t.notify({fieldKeys:E(r,i),structureChanged:n}),i}function It(t,e){let r=t.visibility.fieldByKey.get(e);if(!r||!t.visibility.isFieldVisible(e,t.values))return null;let i=t.errors[e],n=D(r,t.values[e],C(t),t.validators);n?t.errors[e]=n:delete t.errors[e];let s=Object.is(i,t.errors[e])?[]:[e];return t.notify({fieldKeys:s}),n}function O(t){let e={},r=t.visibility.getVisibleFieldKeys(t.values,t.currentStepIndex);for(let i of r)t.values[i]!==void 0&&(e[i]=t.values[i]);return Rt(t,e)}function Vt(t,e){let r=t.values,i=t.errors,n=Object.keys(t.fieldLoading);t.validationEpoch++,t.operationEpoch++,t.submitting=false,t.values=I(t.visibility.allFields,e),t.errors={},t.topLevelErrors=[],t.fieldLoading={},t.stepValidating=false,t.currentStepIndex=0,t.skipped.clear(),t.visibility.reconcileHidden(t.values,t.errors);let s=E(r,t.values),o=new Set([...s,...E(i,t.errors),...n]);t.notify({fieldKeys:o,structureChanged:true,valuesChanged:s.size>0});}function Mt(t){var i,n;let e=(n=(i=t.currentStep)==null?void 0:i.fields)!=null?n:[],r=p({},t.values);for(let{key:s}of e)delete r[s];return Object.assign(r,I(e))}function Y(t){return nt(t.definition,Mt(t))[t.currentStepIndex+1]}function N(t){let e=t.currentStep,r=e.fields.map(({key:i})=>i);t.values=Mt(t);for(let i of r)delete t.errors[i];return t.skipped.set(e.id,r),t.visibility.reconcileHidden(t.values,t.errors),t.notify({fieldKeys:r,structureChanged:true,valuesChanged:true}),r}function K(t){let e=t.currentStep;e&&t.skipped.delete(e.id);}function At(t,e){return t.currentStepIndex=e,t.notify({structureChanged:true}),N(t)}async function X(t,e,r=false){if(t.submitting||t.stepValidating)return false;r||K(t);let i=p({},t.lifecycle),n=false,s=t.validationEpoch,o=++t.operationEpoch,l=O(t),u=j(t,l),a=()=>s!==t.validationEpoch||o!==t.operationEpoch||u();t.submitting=true,t.notify();try{if(a()||Object.keys(T(t,!0)).length>0||a())return !1;if(i.onBeforeSubmit){let d=await i.onBeforeSubmit(l);if(a()||d===!1)return !1}if(a())return !1;if(i.onBeforeSubmit){if(Object.keys(T(t,!0)).length>0||a())return !1;l=O(t);}return n=!0,await e(l,[...t.skipped.keys()]),o!==t.operationEpoch||await _("afterSubmit",()=>{var d;return (d=i.onAfterSubmit)==null?void 0:d.call(i,l)}),!0}catch(d){if(!n&&a())return false;throw d}finally{o===t.operationEpoch&&(t.submitting=false,t.notify());}}function lt(t){let e=t.currentStep;if(!e)return true;let r=R(e,C(t),t.validators);if(Object.keys(r).length===0)return false;let i=p({},t.errors);return Object.assign(t.errors,r),t.notify({fieldKeys:E(i,t.errors)}),true}function Q(t,e){t.validationEpoch++,t.currentStepIndex=e,t.notify({structureChanged:true});}function Dt(t){return t.isLastStep?false:(Q(t,t.currentStepIndex+1),true)}function Ct(t){return t.isMultiStep?(K(t),!lt(t)&&Dt(t)):false}function Ot(t){return !t.isMultiStep||!Y(t)?false:(N(t),Dt(t))}async function $t(t,e){if(!t.isMultiStep||t.stepValidating||t.submitting)return false;if(Y(t))return Z(t,"next","skip");let r=t.currentStep;if(!e||!r)return false;let i=t.values,n=p({},t.errors),s=false,o=N(t),l=t.values,u=j(t,p({},l));try{return await X(t,async(a,d)=>{await e(a,d),s=!0;},!0)}finally{!s&&t.values===l&&!u()&&(t.values=i,t.errors=p(p({},n),t.errors),t.skipped.delete(r.id),t.notify({fieldKeys:o,structureChanged:true,valuesChanged:true}));}}async function Z(t,e,r=e){let i=r==="skip";if(!t.isMultiStep||t.stepValidating||t.submitting||!i&&(e==="next"?t.isLastStep:t.isFirstStep))return false;let n=t.currentStep,s=e==="next"&&!i;if(s&&K(t),s&&lt(t))return false;let o=p({},t.lifecycle),l=t.onStepValidate,u=false,d=t.currentStepIndex+(e==="next"?1:-1),c=i?Y(t):t.visibleSteps[d];if(!c)return false;let f={fromStepId:n.id,toStepId:c.id,direction:e,reason:r,values:p({},C(t))},b=t.validationEpoch,F=++t.operationEpoch,y=j(t,f.values),$=()=>{var g;return b!==t.validationEpoch||F!==t.operationEpoch||((g=t.currentStep)==null?void 0:g.id)!==n.id||y()};t.stepValidating=true,t.notify();try{if($())return !1;if(s&&l){let g=await l(n.id,f.values);if($())return !1;if(g&&Object.keys(g).length>0)return W(t,g),!1}if(o.onBeforeStepChange){let g=await o.onBeforeStepChange(f);if($()||g===!1)return !1}return $()||s&&lt(t)?!1:(u=!0,i&&N(t),Q(t,d),await _("afterStepChange",()=>{var g;return (g=o.onAfterStepChange)==null?void 0:g.call(o,f)}),!0)}catch(g){if(!u&&$())return false;throw g}finally{F===t.operationEpoch&&(t.stepValidating=false,t.notify());}}function Lt(t){t.isMultiStep&&!t.isFirstStep&&Q(t,t.currentStepIndex-1);}function jt(t,e){if(!t.isMultiStep)return;let r=t.visibility.findStepIndex(e,t.values,t.currentStepIndex);r===t.currentStepIndex?t.notify():r!==null&&Q(t,r);}var J=class{constructor(){this.version=0;this.listeners=new Set;this.fieldVersions=new Map;this.fieldListeners=new Map;this.structureVersion=0;this.structureListeners=new Set;}subscribe(e){return this.listeners.add(e),()=>this.listeners.delete(e)}getSnapshot(){return this.version}subscribeField(e,r){var n;let i=(n=this.fieldListeners.get(e))!=null?n:new Set;return i.add(r),this.fieldListeners.set(e,i),()=>{i.delete(r),i.size===0&&this.fieldListeners.delete(e);}}getFieldSnapshot(e){var r;return (r=this.fieldVersions.get(e))!=null?r:0}subscribeStructure(e){return this.structureListeners.add(e),()=>this.structureListeners.delete(e)}getStructureSnapshot(){return this.structureVersion}notify({fieldKeys:e=[],structureChanged:r=false}={}){for(let i of new Set(e))this.notifyField(i);r&&this.notifyStructure(),this.version++;for(let i of this.listeners)i();}notifyField(e){var r,i;this.fieldVersions.set(e,((r=this.fieldVersions.get(e))!=null?r:0)+1);for(let n of (i=this.fieldListeners.get(e))!=null?i:[])n();}notifyStructure(){this.structureVersion++;for(let e of this.structureListeners)e();}};function ut(t,e){var r;for(let i of S(t)){let n=(r=e.get(i))!=null?r:new Set;n.add(t),e.set(i,n);}}var U=class{constructor(e){this.definition=e;this.routeFields=new Set;this.fieldDependents=new Map;this.stepDependents=new Map;this.stepByFieldKey=new Map;this.visibilityDirty=true;this.canGoNextDirty=true;this.cache={visibleSteps:[],currentStep:null,visibleFields:[],canGoNext:false};var r;this.allFields=w(e),this.fieldByKey=new Map(this.allFields.map(i=>[i.key,i])),this.path=new G(e);for(let i of (r=e.steps)!=null?r:[])for(let n of i.fields)this.stepByFieldKey.set(n.key,i);this.buildIndexes();}markChanged(e,r){(e||r)&&this.path.invalidate(),e?(this.visibilityDirty=true,this.canGoNextDirty=true):r&&(this.canGoNextDirty=true);}state(e,r){return this.recomputeVisibility(e,r),this.cache}getCanGoNext(e,r,i){if(this.recomputeVisibility(e,r),!this.canGoNextDirty)return this.cache.canGoNext;this.canGoNextDirty=false;let n=this.cache.currentStep,s=n?R(n,this.conditionValues(e),i):{};return this.cache.canGoNext=this.isMultiStep&&Object.keys(s).length===0,this.cache.canGoNext}findStepIndex(e,r,i){let s=this.state(r,i).visibleSteps.findIndex(o=>o.fields.some(l=>l.key===e));return s===-1?null:s}affectsStructure(e,r){return this.routeFields.has(e)||this.fieldDependents.has(e)||this.stepDependents.has(e)||r.size>0}cascadeHiddenFields(e,r,i){return this.path.invalidate(),this.hasRoutes?this.reconcileHidden(r,i):this.drainQueue(new Set([e]),r,i)}reconcileHidden(e,r){this.path.invalidate();let i=new Set([...this.fieldDependents.keys(),...this.stepDependents.keys()]);return this.drainQueue(i,e,r)}drainQueue(e,r,i){let n=[...e],s=new Set(n),o=new Set,l=u=>{delete r[u],delete i[u],this.path.invalidate(),o.add(u),!s.has(u)&&(s.add(u),n.push(u));};for(let u=0;u<n.length;u++){let a=n[u];s.delete(a),this.clearHiddenFields(this.fieldDependents.get(a),r,l),this.clearHiddenSteps(this.stepDependents.get(a),r,l);}return o}isFieldVisible(e,r){let i=this.fieldByKey.get(e);if(!i||!h(i,this.conditionValues(r)))return false;let n=this.stepByFieldKey.get(e);return !n||this.path.steps(r).includes(n)}getVisibleFieldKeys(e,r){var s;if(this.hasRoutes)return new Set(Object.keys(this.path.values(e)));let i=new Set,n=this.isMultiStep?this.state(e,r).visibleSteps.flatMap(o=>o.fields):(s=this.definition.fields)!=null?s:[];for(let o of n)h(o,this.conditionValues(e))&&i.add(o.key);return i}conditionValues(e){return this.hasRoutes?this.path.values(e):e}get hasRoutes(){return this.path.enabled}get isMultiStep(){var e;return ((e=this.definition.steps)!=null?e:[]).length>0}buildIndexes(){var e,r;for(let i of this.allFields)ut(i,this.fieldDependents);for(let i of (e=this.definition.steps)!=null?e:[]){ut(i,this.stepDependents);for(let n of (r=i.routes)!=null?r:[])for(let s of S(n))this.routeFields.add(s);}}recomputeVisibility(e,r){var s,o,l;if(!this.visibilityDirty)return;if(this.visibilityDirty=false,!this.isMultiStep){this.cache.visibleSteps=[],this.cache.currentStep=null,this.cache.visibleFields=((s=this.definition.fields)!=null?s:[]).filter(u=>h(u,this.conditionValues(e)));return}this.cache.visibleSteps=this.path.steps(e);let i=this.cache.visibleSteps;this.cache.currentStep=(l=(o=i[r])!=null?o:i[i.length-1])!=null?l:null;let n=this.cache.currentStep;this.cache.visibleFields=n?n.fields.filter(u=>h(u,this.conditionValues(e))):[];}clearHiddenFields(e,r,i){for(let n of e!=null?e:[]){let s=this.stepByFieldKey.get(n.key);this.hasRoutes&&s&&!this.path.steps(r).includes(s)||h(n,this.conditionValues(r))||r[n.key]===void 0||i(n.key);}}clearHiddenSteps(e,r,i){for(let n of e!=null?e:[])if(!x(n,this.conditionValues(r)))for(let s of n.fields)r[s.key]!==void 0&&i(s.key);}};var dt=new WeakSet,tt=class{constructor(e,r,i){this.definition=e;this.errors={};this.topLevelErrors=[];this.currentStepIndex=0;this.fieldLoading={};this.stepValidating=false;this.submitting=false;this.skipped=new Map;this.validationEpoch=0;this.operationEpoch=0;this.subscriptions=new J;var o;let n=A(e),s=n.length?n:V(e);if(s.length)throw new Error(s.join(`
4
+ `));this.lifecycle=i!=null?i:{},this.validators=(o=i==null?void 0:i.validators)!=null?o:{},this.visibility=new U(e),dt.has(this.lifecycle)||this.warnAboutDefinition(),this.values=I(this.visibility.allFields,r),this.visibility.reconcileHidden(this.values,this.errors);}get onStepValidate(){return this.lifecycle.onStepValidate}get isMultiStep(){var e;return ((e=this.definition.steps)!=null?e:[]).length>0}get visibleSteps(){return this.visibility.state(this.values,this.currentStepIndex).visibleSteps}get currentStep(){return this.visibility.state(this.values,this.currentStepIndex).currentStep}get visibleFields(){return this.visibility.state(this.values,this.currentStepIndex).visibleFields}get isFirstStep(){return this.currentStepIndex===0}get isLastStep(){return this.currentStepIndex>=this.visibleSteps.length-1}get canGoNext(){return this.visibility.getCanGoNext(this.values,this.currentStepIndex,this.validators)}get progress(){let e=this.visibleSteps.length;return {current:Math.min(this.currentStepIndex+1,e),total:e}}subscribe(e){return this.subscriptions.subscribe(e)}getSnapshot(){return this.subscriptions.getSnapshot()}subscribeField(e,r){return this.subscriptions.subscribeField(e,r)}getFieldSnapshot(e){return this.subscriptions.getFieldSnapshot(e)}subscribeStructure(e){return this.subscriptions.subscribeStructure(e)}getStructureSnapshot(){return this.subscriptions.getStructureSnapshot()}setValue(e,r){let i=this.isMultiStep?this.visibleSteps:null,n=!Object.is(this.values[e],r),s=this.errors[e]!==void 0;this.values[e]=r,delete this.errors[e];for(let[c,f]of this.skipped)n&&f.includes(e)&&this.skipped.delete(c);let o=this.visibility.cascadeHiddenFields(e,this.values,this.errors),l=xt(this.internals,e),u=new Set([...o,...l]);(n||s)&&u.add(e);let a=n||o.size>0,d=a&&this.visibility.affectsStructure(e,o);if(i&&d){this.visibility.markChanged(true,true),this.currentStepIndex=gt(i,this.visibleSteps,this.currentStepIndex);for(let c of Et(this.internals))u.add(c);}this.notify({fieldKeys:u,structureChanged:d,valuesChanged:a});}setErrors(e){W(this.internals,e,true);}setFieldLoading(e,r){let i=this.fieldLoading[e]===true;r?this.fieldLoading[e]=true:delete this.fieldLoading[e],this.notify({fieldKeys:i===r?[]:[e]});}cancelPendingActions(){this.validationEpoch++,this.operationEpoch++,this.stepValidating=false,this.submitting=false,this.notify();}nextStep(){return Ct(this.internals)}nextStepAsync(e="next"){return Z(this.internals,"next",e)}prevStepAsync(){return Z(this.internals,"back")}skipStep(){return Ot(this.internals)}skipStepAsync(e){return $t(this.internals,e)}isStepSkipped(e){return this.skipped.has(e)}submitAsync(e){return X(this.internals,e)}prevStep(){Lt(this.internals);}validate(){return T(this.internals)}validateField(e){return It(this.internals,e)}getSubmitValues(){return O(this.internals)}goToStepWithField(e){jt(this.internals,e);}reset(e){Vt(this.internals,e);}warnAboutDefinition(){let e=L(this.definition);for(let{key:r,validation:i}of this.visibility.allFields){let n=i==null?void 0:i.validator;n&&!this.validators[n]&&e.push(`Field "${r}" references unknown validator "${n}".`);}for(let r of e)console.warn(`[FormEngine] ${r}`);}get internals(){return this}notify(e={}){this.visibility.markChanged(!!e.structureChanged,!!e.valuesChanged),this.subscriptions.notify(e);}};var P=1e4,Ut=254,te=1e3,ee=/^[^\s@]+@[^\s@.]+(?:\.[^\s@.]+)+$/,re=/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/,Tt="\\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\\d|3[01])",ie=new RegExp(`^${Tt}$`),ne=new RegExp(`^${Tt}T(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d(?:\\.\\d{1,3})?)?(?:Z|[+-](?:[01]\\d|2[0-3]):[0-5]\\d)?$`),se=new Set(["text","phone","password","textarea","autocomplete"]),oe=new Map([["email",[ee,m.email,Ut]],["date",[ie,m.date,P]],["datetime",[ne,m.datetime,P]]]);function Nt(t,e){if(t.type!=="number"||typeof e!="string"||!re.test(e))return e;let r=Number(e);return Number.isFinite(r)?r:e}function Kt(t,e){return !t.options||t.options.some(r=>r.value===e)}function Pt(t,e){var i,n,s,o;let r=Math.min(P,(n=(i=t.validation)==null?void 0:i.maxLength)!=null?n:P);return e.length<=r?null:(o=(s=t.validation)==null?void 0:s.maxLengthMessage)!=null?o:k("maxLength",{max:r})}function ae(t,e){var i,n;return Array.isArray(e)?e.length>((n=(i=t.options)==null?void 0:i.length)!=null?n:te)?m.option:e.every(s=>typeof s=="string"&&s.length<=P&&Kt(t,s))?null:m.option:m.options}function et(t,e,r){return typeof e=="string"?Pt(t,e):r}function le(t,e,[r,i,n]){var s;return typeof e!="string"?i:(s=Pt(t,e))!=null?s:e.length<=n&&r.test(e)?null:i}function ue(t,e){return typeof e=="boolean"||typeof e=="number"&&Number.isFinite(e)?null:et(t,e,m.value)}function de(t,e){return e==null||e===""||t.type==="multiselect"&&Array.isArray(e)&&!e.length}function Bt(t,e){var i;if(de(t,e))return null;if(at.has(t.type))return typeof e=="boolean"?null:m.boolean;if(t.type==="number")return typeof e=="number"&&Number.isFinite(e)?null:m.number;if(t.type==="multiselect")return ae(t,e);if(t.type==="file")return et(t,e,m.file);let r=oe.get(t.type);return r?le(t,e,r):t.type==="select"||t.type==="radio"?(i=et(t,e,m.option))!=null?i:Kt(t,e)?null:m.option:se.has(t.type)?et(t,e,m.string):ue(t,e)}var ce=()=>null,ct=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);function pe(t,e){let r=e!==null&&typeof e=="object"&&!Array.isArray(e)?e:{},i={};for(let n of t)ct(r,n.key)&&(i[n.key]=Nt(n,r[n.key]));return i}function fe(t,e){let r=new Map;for(let i of t){let n=ct(e,i.key)?Bt(i,e[i.key]):null;n&&r.set(i.key,n);}return r}function he(t,e){var n;let r=s=>s.map(o=>e.has(o.key)?rt(p({},o),{validation:void 0}):o),i=(n=t.steps)==null?void 0:n.map(s=>rt(p({},s),{fields:r(s.fields)}));return p(p(p({},t),t.fields&&{fields:r(t.fields)}),i&&{steps:i})}function me(t){let e={};for(let{validation:r}of t)r!=null&&r.validator&&(e[r.validator]=ce);return e}function ge(t,e){if(!Array.isArray(e))return;let r=new Set(e);for(let i=0;i<t.visibleSteps.length;i++){let n=t.visibleSteps[i];n.skip&&r.has(n.id)&&At(t,i);}}function ye(t,e,r={}){let i=w(t),n=pe(i,e),s=fe(i,n),o={validators:me(i)};dt.add(o);let l=new tt(he(t,s),n,o);ge(l,r.skippedSteps);let u=l.validate(),a=l.getSubmitValues();for(let[d,c]of s)ct(a,d)&&(u[d]=c);return {values:a,errors:u}}exports.checkDefinition=Qt;exports.validateSubmission=ye;return exports;})({});//# sourceMappingURL=server.iife.js.map
5
5
  //# sourceMappingURL=server.iife.js.map