import{G as C}from"./GraphinaApexChartBase.js";class _ extends C{constructor(){super(),this.observer={}}setUpChartsHandler(){this.chartHandlers={counter:r=>this.observeChartElement(r,"counter")}}afterManualLoad(r,a,t){this.startCounterAnimation(a,!1,r,t)}afterDynamicLoad(r,a,t){this.startCounterAnimation(a,!0,r,t)}async renderChart(r,a){var t,s;try{jQuery(document).find(`.graphina-${a}-loader`).hide();const e=jQuery(`[data-element_id="${a}"]`).data("extra_data");(((s=(t=r.opts)==null?void 0:t.chart)==null?void 0:s.type)!=="counter"||e!=null&&e.show_counter_chart)&&await r.render()}catch(i){console.warn(i)}}async setupChart(r,a){try{const t=r.data("element_id"),s=r.data("chart_options"),i=r.data("responsive_options"),e=r.data("extra_data"),c=r.data("settings");if(!s||!t){console.error(`Missing required data attributes for ${a} chart.`);return}this.applyLegendTooltip(s,e,a),this.applyXAxisFormatter(s,e),this.applyDataLabelFormatter(s,e);const h=this.getChartOptions(s,a,e,t);if(e!=null&&e.show_counter_chart){this.mainChart[t]&&this.mainChart[t].destroy();const n=new ApexCharts(jQuery(r)[0],h);await this.renderChart(n,t),this.mainChart[t]=n,this.afterRenderChart(n,t,e)}if(e.chart_data_option===!0){const n=await this.getDynamicData(c,e,a,t);this.processDynamicData(n,t,e),this.afterDynamicLoad(n,t,e)}else this.afterManualLoad([],t,e)}catch(t){console.error(`Error initializing ${a} chart:`,t)}}afterRenderChart(r,a,t){t.color!==""&&(document.querySelector(`.count_number-pre-postfix-${a}`).style.color=t.color),t.headingColor!==""&&(document.querySelector(`.counter-title-${a}`).style.color=t.headingColor),t.subHeadingColor!==""&&(document.querySelector(`.counter-description-${a}`).style.color=t.subHeadingColor),t.show_counter_chart||r.destroy()}startCounterAnimation(r,a=!1,t=[],s){const i=document.querySelector(`.count_number-${r}`);let e=parseFloat(i.getAttribute("data-start")),c=parseFloat(i.getAttribute("data-end")),h=parseInt(i.getAttribute("data-speed"),10),n=parseInt(i.getAttribute("data-decimals"),10)||0;if(a){c=t.extra.end;try{document.querySelector(`.counter-title-${r}`).innerHTML=t.extra.title}catch(u){console.warn(u)}}const o=h||2e3,m=(c-e)/(o/50);let d=e;function p(u,y=""){const l=u.toString().split(".");return l[0]=l[0].replace(/\B(?=(\d{3})+(?!\d))/g,y),l.join(".")}function f(){d<c?(d=Math.min(d+m,c),i.textContent=p(d.toFixed(n),s.seperator),requestAnimationFrame(f)):i.textContent=p(c.toFixed(n),s.seperator)}f()}setFieldsForCounter(r,a,t,s){const i=`[data-setting="${s.graphina_prefix}${t}_element_column_no"]`,e=parent.document.querySelector(i);if(!e)return;e.innerHTML="";const c=a.extra.columns,h=s.element_column_no;c.forEach(n=>{const o=Array.isArray(h)?h.includes(n):h===n;e.append(new Option(n,n,o,o))})}processDynamicData(r,a,t){r.extra.category=["element1"]}getChartOptions(r,a,t,s){return r}async renderChart(r,a){var t,s;try{jQuery(document).find(`.graphina-${a}-loader`).hide();const e=jQuery(`[data-element_id="${a}"]`).data("extra_data");(((s=(t=r.opts)==null?void 0:t.chart)==null?void 0:s.type)!=="counter"||e!=null&&e.show_counter_chart)&&await r.render()}catch(i){console.warn(i)}}async setupChart(r,a){try{const t=r.data("element_id"),s=r.data("chart_options"),i=r.data("responsive_options"),e=r.data("extra_data"),c=r.data("settings");if(!s||!t){console.error(`Missing required data attributes for ${a} chart.`);return}this.applyLegendTooltip(s,e,a),this.applyXAxisFormatter(s,e),this.applyDataLabelFormatter(s,e);const h=this.getChartOptions(s,a,e,t),n=new ApexCharts(jQuery(r)[0],h);if(e!=null&&e.show_counter_chart&&(this.mainChart[t]&&this.mainChart[t].destroy(),await this.renderChart(n,t),this.afterRenderChart(n,t,e),this.mainChart[t]=n),this.afterManualLoad([],t,e),e.chart_data_option===!0){const o=await this.getDynamicData(c,e,a,t);this.processDynamicData(o,t,e),o.extra!==void 0&&(e!=null&&e.show_counter_chart)?(this.mainChart[t]&&(n.updateOptions({series:o.extra.series,labels:o.extra.category}),n.updateSeries(o.extra.series,!0)),o.extra.series.length<=0&&(this.mainChart[t]&&n.destroy(),jQuery(r).hide(),jQuery(`.graphina-${t}-notext`).show())):(o.extra.series.length<=0&&(this.mainChart[t]&&n.destroy(),jQuery(r).hide(),jQuery(`.graphina-${t}-notext`).show()),this.mainChart[t]&&(n.updateOptions({series:[],labels:[]}),n.updateSeries([],!0)))}e.can_chart_reload_ajax&&setInterval(async()=>{try{const o=await this.getDynamicData(c,e,a,t);o!=null&&o.extra?(this.afterDynamicLoad(o,t,e),this.mainChart[t]&&(n.updateOptions({series:o.extra.series,labels:o.extra.category}),n.updateSeries(o.extra.series,!0))):console.warn(`No data returned for ${a} chart with ID ${t}.`)}catch{console.warn(`Error fetching dynamic data for ${a} chart with ID ${t}:`)}},e.interval_data_refresh*1e3)}catch(t){console.error(`Error initializing ${a} chart:`,t)}}}window.graphinaCounterChart=new _;
//# sourceMappingURL=counter.js.map
