­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ (()=>{"use strict";function t(){}t.prototype.isSupported=function(t){return!1},t.prototype.observe=function(){},t.prototype.setOptions=function(t){},t.prototype.isPassed=function(){throw new Error("You must provide ConditionItem::isPassed function")},t.prototype.setList=function(t){this.list=t};const e=t,{CalculatedFormula:i}=JetFormBuilderAbstract;function n(){e.call(this),this.isSupported=function(t){return!!t?.field?.length},this.observe=function(){var t;const e=this.getInput();this.list._fields=null!==(t=this.list._fields)&&void 0!==t?t:[],e&&!this.list._fields.includes(this.field)&&(this.list._fields.push(this.field),e.watch((()=>this.list.onChangeRelated())))},this.getInput=function(){return this.list.root.getInput(this.field)},this.isPassed=function(){const t=this.getInput();return!!t&&t.checker.check(this,t)},this.setOptions=function(t){this.field=t.field,this.operator=t.operator,this.render_state=t.render_state,this.use_preset=t.use_preset;let e=t?.value;if(Array.isArray(e)||(e=e.split(",").map((t=>t.trim()))),this.use_preset)this.value=e;else{this.value={};for(const[t,n]of Object.entries(e)){const e=new i(this.list.root);e.observe(n),e.setResult=()=>{this.value[t]=""+e.calculate(),this.list.onChangeRelated()},e.setResult()}this.value=Object.values(this.value)}}}n.prototype=Object.create(e.prototype),n.prototype.field=null,n.prototype.value=null,n.prototype.operator=null,n.prototype.use_preset=null;const o=n;function s(){e.call(this),this.isSupported=function(t){var e;return null!==(e=t.or_operator)&&void 0!==e&&e}}s.prototype=Object.create(e.prototype);const r=s;function l(t,e){this.root=e,this.setConditions(t)}l.prototype={root:null,conditions:[],invalid:[],groups:[],onChangeRelated(){this.getResult()&&this.onMatchConditions()},onMatchConditions(){},observe(){for(const t of this.getConditions())t.observe()},setConditions(t){"string"==typeof t&&(t=JSON.parse(t)),this.conditions=t.map((t=>function(t,e){P.length||(P=J());for(const i of P){const n=new i;if(n.isSupported(t))return n.setList(e),n.setOptions(t),n}}(t,this))).filter((t=>t));const e={};let i=0;for(const t of this.getConditions()){var n;t instanceof r?i++:(e[i]=null!==(n=e[i])&&void 0!==n?n:[],e[i].push(t))}this.groups=Object.values(e)},getResult(){if(this.invalid=[],!this.groups.length)return!0;for(const t of this.groups)if(this.isValidGroup(t))return!0;return!1},isValidGroup(t){for(const e of t)if(!e.isPassed())return this.invalid.push(e),!1;return!0},getConditions(){return this.conditions}};const c=l;function a(t,e){c.call(this,t,e)}a.prototype=Object.create(c.prototype),a.prototype.block=null;const u=a,{doAction:h}=JetPlugins.hooks,{ReactiveVar:d}=JetFormBuilderAbstract,{validateInputsAll:p}=JetFormBuilderFunctions,f=new WeakSet;function m(t,e){this.node=t,t.jfbConditional=this,this.root=e,this.isObserved=!1,this.list=null,this.function=null,this.settings=null,this.page=null,this.multistep=null,this.comment=null,this.inputs=[],this.isRight=new d(null),this.isRight.make(),this.setConditions(),this.setInputs(),this.setFunction(),window?.JetFormBuilderSettings?.devmode||(delete this.node.dataset.jfbConditional,delete this.node.dataset.jfbFunc),h("jet.fb.conditional.init",this)}m.prototype={setConditions(){const{jfbConditional:t}=this.node.dataset;this.list=new u(t,this.root),this.list.block=this,this.list.onChangeRelated=()=>{this.isRight.current=this.list.getResult()}},setInputs(){this.inputs=Array.from(this.node.querySelectorAll("[data-jfb-sync]")).map((t=>t.jfbSync)).filter((t=>t))},insertComment(){this.settings?.dom&&(this.comment=document.createComment(""),this.node.parentElement.insertBefore(this.comment,this.node.nextSibling))},observe(){this.isObserved||(this.isObserved=!0,this.insertComment(),this.isRight.watch((()=>this.runFunction())),this.isRight.watch((()=>this.validateInputs())),this.list.observe())},runFunction(){const t=this.isRight.current;switch(this.function){case"show":this.showBlock(t);break;case"hide":this.showBlock(!t);break;case"disable":this.disableBlock(t);break;default:h("jet.fb.conditional.block.runFunction",this.function,t,this)}},validateInputs(){setTimeout((()=>{p(this.inputs,!0).then((()=>{})).catch((()=>{}))}))},showBlock(t){if(this.node.classList.remove("jet-form-builder--hidden"),this.settings?.dom){this.showBlockDom(t),t&&requestAnimationFrame((()=>this.reinitChildren()));const e=new CustomEvent("jet-form-builder/conditional-block/block-toggle-hidden-dom",{detail:{block:this.node,result:t}});document.dispatchEvent(e)}else this.node.style.display=t?"block":"none",t&&requestAnimationFrame((()=>this.reinitChildren()))},showBlockDom(t){const e=this.root.dataInputs;if(!t)return this.node.remove(),void this.reCalculateFields(e);this.comment.parentElement.insertBefore(this.node,this.comment),this.reCalculateFields(e)},disableBlock(t){this.node.disabled=t},setFunction(){let t;this.function=this.node.dataset.jfbFunc;try{t=JSON.parse(this.function)}catch(t){return}const[[e,i]]=Object.entries(t);this.function=e,this.settings=i},reinitChildren(){const t=this.root;this.node.querySelectorAll("[data-jfb-conditional][data-jfb-func]").forEach((e=>{if(!e.jfbConditional&&!f.has(e))try{const i=new m(e,t);i.observe(),i.isRight.current=i.list.getResult(),f.add(e)}catch(t){console&&console.warn&&console.warn("reinitChildren: init failed",t,e)}}))},reCalculateFields(t){const e=this.getAffectedFields(t),i=new Map;e.forEach((e=>{if(t[e]&&t[e].formula){const n=t[e].nodes?.[0];let o=!1;if(n){const t=n;if(!i.has(t)){const e=this.isFieldVisible(n),o=document.contains(n);i.set(t,e||o)}o=i.get(t)}if(o)try{t[e].reCalculateFormula()}catch(t){console.warn(`Error recalculating formula for field ${e}:`,t)}}}))},isFieldVisible(t){if(!t)return!1;if(!document.contains(t))return!1;const e=window.getComputedStyle(t);if("none"===e.display||"hidden"===e.visibility)return!1;let i=t.parentElement;for(;i&&i!==document.body;){const t=window.getComputedStyle(i);if("none"===t.display||"hidden"===t.visibility)return!1;i=i.parentElement}return!0},getAffectedFields(t){const e=[],i=Array.from(this.node.querySelectorAll("[data-jfb-sync]")),n=new Set;return i.forEach((t=>{const e=t.getAttribute("name");e&&n.add(e)})),Object.keys(t).forEach((o=>{const s=t[o];if(!s||!s.formula)return;const r=s.nodes?.[0];let l=!1;r&&i.includes(r)&&(l=!0),!l&&s.formula&&n.forEach((t=>{s.formula.includes(`%${t}%`)&&(l=!0)})),l&&e.push(o)})),e}};const b=m,{isEmpty:g}=JetFormBuilderFunctions;function y(){this.operators=this.getOperators()}y.prototype={isSupported:()=>!0,operators:{},getOperators:()=>({equal:(t,e)=>t===e[0],empty:t=>g(t),greater:(t,e)=>+t>+e[0],greater_or_eq:(t,e)=>+t>=+e[0],less:(t,e)=>+t<+e[0],less_or_eq:(t,e)=>+t<=+e[0],between:(t,e)=>!(!e?.length||null===t)&&e[0]<=+t&&+t<=e[1],one_of:(t,e)=>!!e?.length&&0<=e.indexOf(t),contain:(t,e)=>!!t&&0<=t.indexOf(e[0])}),check(t,e){const i=e.value.current,n=t.value;return this.checkRaw(t.operator,i,n)},checkRaw(t,e,i){if(this.operators.hasOwnProperty(t))return this.operators[t](e,i);if(0!==t.indexOf("not_"))return!1;const n=t.slice(4);return!!this.operators.hasOwnProperty(n)&&!this.operators[n](e,i)}};const v=y;function w(){v.call(this),this.operators.one_of=(t,e)=>!(!e?.length||!t?.length)&&t.some((t=>-1!==e.indexOf(t))),this.operators.contain=(t,e)=>!!t?.length&&t.some((t=>-1!==t.indexOf(e[0]))),this.isSupported=function(t){return t.isArray()}}w.prototype=Object.create(v.prototype);const C=w,{getTimestamp:k}=JetFormBuilderFunctions,{Min_In_Sec:j,Milli_In_Sec:F}=JetFormBuilderConst,O=(new Date).getTimezoneOffset();function _(){v.call(this),this.isSupported=function(t){const[e]=t.nodes;return["date","time","datetime-local"].includes(e.type)},this.check=function(t,e){const{time:i}=k(e.value.current),n=t.value.map((e=>{const{time:i,type:n}=k(e);return"number"===n&&t.use_preset?i*F+O*j:i}));return this.checkRaw(t.operator,i,n)}}_.prototype=Object.create(v.prototype);const R=_;function S(){e.call(this),this.isSupported=function(t){return"render_state"===t?.operator},this.getInput=function(){return this.list.root.getInput("_jfb_current_render_states")},this.observe=function(){this.getInput().watch((()=>this.list.onChangeRelated()))},this.setOptions=function(t){var e;this.render_state=null!==(e=t.render_state)&&void 0!==e?e:[]},this.isPassed=function(){const{value:t}=this.getInput();return!!t.current?.length&&this.render_state.some((e=>t.current.includes(e)))}}S.prototype=Object.create(e.prototype),S.prototype.render_state=[];const A=S;function B(){v.call(this),this.isSupported=function(t){return"calculated"===t.inputType},this.check=function(t,e){const i=e.calcValue,n=t.value;return this.checkRaw(t.operator,i,n)}}B.prototype=Object.create(v.prototype);const I=B,{applyFilters:E}=JetPlugins.hooks,J=()=>E("jet.fb.conditional.types",[A,r,o]);let P=[],q=[];function x(t,e){if(t.hasOwnProperty("jfbConditional"))return t.jfbConditional;const i=new b(t,e);return i.observe(),i.list.onChangeRelated(),i}function V(t){q.length||(q=E("jet.fb.conditional.checkers",[C,R,I,v]));for(const e of q){const i=new e;if(i.isSupported(t))return i}return null}var L;const{addAction:M}=JetPlugins.hooks;M("jet.fb.observe.after","jet-form-builder/conditional-block",(function(t){for(const e of t.rootNode.querySelectorAll("[data-jfb-conditional]"))x(e,t)}),20),M("jet.fb.input.makeReactive","jet-form-builder/conditional-block",(function(t){t.checker=V(t)})),M("jet.fb.conditional.block.runFunction","jet-form-builder/conditional-block",(function(t,e,i){"setCssClass"===t&&i.settings?.className&&i.node.classList.toggle(i.settings.className,e)})),window.JetFormBuilderAbstract={...null!==(L=window.JetFormBuilderAbstract)&&void 0!==L?L:{},ConditionItem:e,ConditionalBlock:b,createConditionalBlock:x,createChecker:V,ConditionsList:c}})();