!function(){"use strict";const t=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return e||(e=Math.random().toString(36).substr(2,5)),`${t}.${e}`};window.FormieConditions=class{constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.$form=t.$form,this.form=this.$form.form,this.conditionsStore=new WeakMap,this.initFieldConditions(this.$form),this.form.registerEvent("repeater:initRow",(t=>{this.initFieldConditions(t.$row)}))}initFieldConditions(e){e.querySelectorAll("[data-field-conditions]").forEach((e=>{const i=this.getFieldConditions(e),o=e.getAttribute("data-field-type");("group"===o||"repeater"===o)&&(i.nestedFieldConditions=e.querySelectorAll("[data-field-conditions]")),this.conditionsStore.set(e,i),this.form.addEventListener(e,t("onFormieEvaluateConditions"),this.evaluateConditions.bind(this)),e.dispatchEvent(new CustomEvent("onFormieEvaluateConditions",{bubbles:!0,detail:{conditions:this,init:!0}}))})),this.form.formTheme&&this.form.formTheme.updateFormHash()}getFieldConditions(e){const i=this.parseJsonConditions(e);if(!i||!i.conditions.length)return;const o=[];return i.conditions.forEach((i=>{let n=this.$form.querySelectorAll(`[name="${i.field}"]`);const s=this.$form.querySelectorAll(`[name="${i.field}[]"]`);s.length&&(n=s),n&&n.length&&(i.$targets=n,o.push(i),n.forEach((i=>{const o=this.getEventType(i);this.form.addEventListener(i,t(o),(()=>e.dispatchEvent(new CustomEvent("onFormieEvaluateConditions",{bubbles:!0,detail:{conditions:this}}))))})))})),{showRule:i.showRule,conditionRule:i.conditionRule,isNested:i.isNested||!1,conditions:o}}evaluateConditions(t){const e=t.target,i=!!t.detail&&t.detail.init,o=this.conditionsStore.get(e);if(!o)return;const{showRule:n,conditionRule:s,conditions:r,isNested:a,nestedFieldConditions:l}=o,d={};if(a&&!i){const t=e.closest('[data-field-type="group"], [data-field-type="repeater"]');if(t&&t.conditionallyHidden)return}r.forEach(((t,e)=>{const{condition:i,value:o,$targets:n,field:s}=t;n.forEach((t=>{let n=!1;const r={},a=t.tagName.toLowerCase(),l=t.getAttribute("type")?t.getAttribute("type").toLowerCase():"",c=`${s}_${e}`;d[c]||(d[c]=[]),"date"===l&&(r.isDate=!0);const u=null==o?"":String(o);if("agree"===t.getAttribute("data-fui-input-type")){if("hidden"===l)return;n=this.testCondition(i,"0"!=u,t.checked),d[c].push(n)}else"checkbox"===l||"radio"===l?(n=this.testCondition(i,u,t.value)&&t.checked,d[c].push(n)):"select"===a&&t.hasAttribute("multiple")?Array.from(t.options).forEach((t=>{n=this.testCondition(i,u,t.value)&&t.selected,d[c].push(n)})):(n=this.testCondition(i,u,t.value,r),d[c].push(n))}))}));const c=[];Object.values(d).forEach((t=>{c.push(t.includes(!0))}));let u=!1;c.length&&(u="all"===s?c.every((t=>!0===t)):c.includes(!0)),u&&"show"!==n||!u&&"show"===n?(e.conditionallyHidden=!0,e.setAttribute("data-conditionally-hidden",!0),e.querySelectorAll("input, textarea, select").forEach((t=>{t.setAttribute("disabled",!0)}))):(e.conditionallyHidden=!1,e.removeAttribute("data-conditionally-hidden"),e.querySelectorAll("input, textarea, select").forEach((t=>{t.removeAttribute("disabled")}))),this.updateRowVisibility(e),e.dispatchEvent(new CustomEvent("onAfterFormieEvaluateConditions",{bubbles:!0,detail:{conditions:this,init:i}})),l&&!i&&l.forEach((t=>{this.evaluateConditions({target:t})}))}parseJsonConditions(t){const e=t.getAttribute("data-field-conditions");if(e)try{return JSON.parse(e)}catch(t){console.error(`Unable to parse JSON conditions: ${t}`)}return!1}getEventType(t){const e=t.closest("[data-field-type]"),i=e?.getAttribute("data-field-type"),o=t.tagName.toLowerCase(),n=t.getAttribute("type")?t.getAttribute("type").toLowerCase():"";return"select"===o||"date"===n?"change":"number"===n?"input":"checkbox"===n||"radio"===n?"click":i&&"calculations"===i?"input":"keyup"}testCondition(t,e,i){let o=!1;return(arguments.length>3&&void 0!==arguments[3]?arguments[3]:{}).isDate&&(e=new Date(e).valueOf(),i=new Date(i).valueOf()),"="===t?o=e===i:"!="===t?o=e!==i:">"===t?o=parseFloat(i,10)>parseFloat(e,10):"<"===t?o=parseFloat(i,10)<parseFloat(e,10):"contains"===t?o=i.includes(e):"startsWith"===t?o=i.startsWith(e):"endsWith"===t&&(o=i.endsWith(e)),o}querySelectorAllRegex(t,e,i){const o=[];for(const n of t.querySelectorAll(`[${i}]`))e.test(n.getAttribute(i))&&o.push(n);return o}updateRowVisibility(t){const e=t.closest("[data-fui-field-count]");if(e){const t=e.querySelectorAll("[data-field-handle]:not([data-conditionally-hidden])"),i=Array.from(t).filter((t=>t.closest("[data-fui-field-count]")===e));e.setAttribute("data-fui-field-count",i.length),e.classList.contains("fui-row")&&(0===i.length?e.classList.add("fui-row-empty"):e.classList.remove("fui-row-empty"))}}}}();