(()=>{var l={};function r(e){var r,a,t=e.target.closest(".cf-control-group");return t&&(e=e.target.closest("form"),a=e.closest(".cf").dataset.id,(l.hasOwnProperty(a)?l[a]:(e=e.querySelectorAll("*[data-calc]"),r=[],e.forEach(function(e){for(var a,t=e.dataset.calc,l=/{field.(.*?)}/g;null!==(a=l.exec(t));)a.index===l.lastIndex&&l.lastIndex++,r.includes(a[1])||r.push(a[1])}),l[a]=r)).includes(t.dataset.name))}function n(e,a){for(var t=new exprEval.Parser,l=0;l<a.length;l++){var r=(e=>{for(var a={},t=0;t<e.elements.length;t++){var l,r,n,c=e.elements[t];c.name&&!c.disabled&&"button"!=c.type&&"file"!=c.type&&"reset"!=c.type&&"submit"!=c.type&&-1!=c.name.indexOf("cf[")&&(l=c.closest(".cf-control-group"))&&(r=c.dataset.calcValue||c.value,"checkbox"!=c.type&&"radio"!=c.type||(r=c.checked?r:0),"select-one"==c.type&&(r=c.options[c.selectedIndex].dataset.calcValue),n=l.dataset.key,r=((e,a)=>("string"==typeof(e=a&&a.inputmask&&"function"==typeof a.inputmask.unmaskedvalue&&""!==(a=a.inputmask.unmaskedvalue())?a:e)&&(e=-1!==e.indexOf(",")&&-1===e.indexOf(".")?e.trim().replace(",","."):e.trim().replace(/,/g,"")),a=parseFloat(e),isNaN(a)?0:a))(r,c),a.hasOwnProperty(n)&&(c=a[n].field_value,r+=c),a[n]={field_value:r,field_name:l.dataset.name})}return a})(e),n=a[l],c={};if(f=n.dataset.calc){for(var o=0,s=Object.entries(r);o<s.length;o++){var d=s[o],i=d[0],d=d[1],u=new RegExp("{field."+d.field_name+"}","gi"),f=f.replace(u,"{field"+i+"}");c["field"+i]=d.field_value}f=(f=f.replace(/{/g,"")).replace(/}/g,"");var p,v,m,y,g,r=void 0;try{r=t.evaluate(f,c)}catch(e){console.error("Convert Forms failed to evalute calculation formula:",f,"- Make sure you're usign the correct syntax: {field.FIELDNAME}")}isNaN(r)||(p=n.dataset.thousand_separator||null,g=n.dataset.decimal_separator||null,y=n.dataset.precision||2,v=n.dataset.prefix||null,m=n.dataset.suffix||null,r=y=parseFloat(r).toFixed(y),g=r=g&&"."!=g?r.replace(".",g):r,p&&(g=g.replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1"+p)),v&&(g=v+g),m&&(g+=m),"INPUT"===n.nodeName?(ConvertForms.Helper.setValue(n,g),n.dataset.calcValue=y):n.innerHTML=g)}}}ConvertForms.Helper.onReady(function(e){e.forEach(function(e){var a=e.querySelector("form"),t=a.querySelectorAll("*[data-calc]");0<t.length&&(a.addEventListener("input",function(e){r(e)&&n(a,t)}),a.addEventListener("change",function(e){r(e)&&n(a,t)}),n(a,t))})})})(document);

