import{r as t,d as i,B as s,h as e}from"./p-03169580.js";import{D as o}from"./p-bd7cdf38.js";import{l as n}from"./p-0b8dd2da.js";import{l}from"./p-795b799a.js";import"./p-bd8b186e.js";const r=t=>new Promise((i=>setTimeout(i,t)));async function a(t,i,s=500){return new Promise((async(e,o)=>{if(!i||typeof i!=="function")o("Callback function is required!");try{const t=await i();e(t)}catch(e){if(t>1){await r(s);a(t-1,i,s*2)}else{o(e)}}}))}var d=undefined&&undefined.__decorate||function(t,i,s,e){var o=arguments.length,n=o<3?i:e===null?e=Object.getOwnPropertyDescriptor(i,s):e,l;if(typeof Reflect==="object"&&typeof Reflect.decorate==="function")n=Reflect.decorate(t,i,s,e);else for(var r=t.length-1;r>=0;r--)if(l=t[r])n=(o<3?l(n):o>3?l(i,s,n):l(i,s))||n;return o>3&&n&&Object.defineProperty(i,s,n),n};let h=class{constructor(s){t(this,s);this.fireenjinFetch=i(this,"fireenjinFetch",7);this.data={};this.template={};this.partials=[];this.helpers={formatUSD:t=>{const i=new Intl.NumberFormat("en-US",{style:"currency",currency:"USD",minimumFractionDigits:2});return i.format(t?t:0)},logic:(t,i,s)=>l.apply(JSON.parse(i.replace('"@tempData"',s)),Object.assign(Object.assign({},t),{tempData:s}))};this.html="";this.currentPartials=[];this.currentHelpers=[]}async componentWillLoad(){var t,i;if(!(s===null||s===void 0?void 0:s.isBrowser))return;if(this.helpers&&((t=Object.keys(this.helpers))===null||t===void 0?void 0:t.length))this.setHelpers();if((i=this.partials)===null||i===void 0?void 0:i.length)this.setPartials();if(this.templateId)this.getTemplate(this.templateId);if(this.template)a(10,this.renderTemplate.bind(this))}getTemplate(t){this.fireenjinFetch.emit({endpoint:"findTemplate",name:this.name,params:{id:t}})}async fetchData(t){this.getTemplate((t===null||t===void 0?void 0:t.templateId)||this.templateId)}async unsetPartials(){for(const t of this.currentPartials){n.unregisterPartial(t)}this.currentPartials=[];this.partials=[]}async setPartials(t){try{n.registerPartial(null,"")}catch(t){}try{if((t===null||t===void 0?void 0:t.length)&&this.partials!==t){this.partials=t}for(const t of this.partials){try{if(this.currentPartials.includes(t===null||t===void 0?void 0:t.id))n.unregisterPartial(t===null||t===void 0?void 0:t.id);n.registerPartial(t===null||t===void 0?void 0:t.id,(t===null||t===void 0?void 0:t.html)||"");if(!this.currentPartials.includes(t===null||t===void 0?void 0:t.id))this.currentPartials=[...this.currentPartials,t===null||t===void 0?void 0:t.id]}catch(i){console.log(`Error setting partial ${t===null||t===void 0?void 0:t.id}.`)}}}catch(t){console.log("Error setting partials.")}}async setHelpers(t){try{if(this.helpers!==t){this.helpers=t}for(const[t,i]of Object.entries(this.helpers)){try{if(this.currentHelpers.includes(t))n.unregisterHelper(t);n.registerHelper(t,i);if(!this.currentHelpers.includes(t))this.currentHelpers=[...this.currentHelpers,t]}catch(i){console.log(`Error setting helper ${t}.`)}}}catch(t){console.log("Error setting helpers.")}}async renderTemplate(){var t,i;this.html=n.compile(((t=this.template)===null||t===void 0?void 0:t.html)?(i=this.template)===null||i===void 0?void 0:i.html:"")(this.data?this.data:{})}onSuccess(t){var i,s,e,o,n,l;if(((i=t===null||t===void 0?void 0:t.detail)===null||i===void 0?void 0:i.endpoint)==="findTemplate"&&((o=(e=(s=t.detail)===null||s===void 0?void 0:s.data)===null||e===void 0?void 0:e.template)===null||o===void 0?void 0:o.id)===this.templateId){this.template=((l=(n=t===null||t===void 0?void 0:t.detail)===null||n===void 0?void 0:n.data)===null||l===void 0?void 0:l.template)?t.detail.data.template:null}}onTemplateId(){this.fireenjinFetch.emit({endpoint:"findTemplate",params:{id:this.templateId}})}onData(){a(10,this.renderTemplate.bind(this))}onTemplate(){a(10,this.renderTemplate.bind(this))}render(){return e("div",{innerHTML:this.html||""})}static get watchers(){return{partials:["setPartials"],helpers:["setHelpers"],templateId:["onTemplateId"],data:["onData"],template:["onTemplate"]}}};d([o(1e3)],h.prototype,"setPartials",null);d([o(1e3)],h.prototype,"setHelpers",null);export{h as fireenjin_render_template};
//# sourceMappingURL=p-057765c8.entry.js.map