import{Controller}from"stimulus"exportdefaultclassextendsController{statictargets=["time"]connect(){this.timeAgo()}timeAgo(){for(varindexinthis.timeTargets){consttimeTarget=this.timeTargets[index];if(typeoftimeTarget==="object"){timeTarget.innerHTML=this.setTimer(timeTarget.dataset.date);}}// update time every minutesetTimeout(this.timeAgo,60000);}template(t,n){returnthis.templates[t]&&this.templates[t].replace(/%d/i,Math.abs(Math.round(n)));}setTimer(time){if(!time)return;time=time.replace(/\.\d+/,"");// remove millisecondstime=time.replace(/-/,"/").replace(/-/,"/");time=time.replace(/T/,"").replace(/Z/," UTC");time=time.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2");// -04:00 -> -0400time=newDate(time*1000||time);returnthis.templates.prefix+(this.seconds(time)<45&&this.template("seconds",this.seconds(time))||this.seconds(time)<90&&this.template("minute",1)||this.minutes(time)<45&&this.template("minutes",this.minutes(time))||this.minutes(time)<90&&this.template("hour",1)||this.hours(time)<24&&this.template("hours",this.hours(time))||this.hours(time)<42&&this.template("day",1)||this.days(time)<30&&this.template("days",this.days(time))||this.days(time)<45&&this.template("month",1)||this.days(time)<365&&this.template("months",this.days(time)/30)||this.years(time)<1.5&&this.template("year",1)||this.template("years",this.years(time)))+this.templates.suffix;}getcurrentDate(){returnnewDate()}seconds(time){return((this.currentDate.getTime()-time)*.001)>>0}minutes(time){returnthis.seconds(time)/60}hours(time){returnthis.minutes(time)/60}days(time){returnthis.hours(time)/24}years(time){returnthis.days(time)/365}gettemplates(){return{prefix:"",suffix:" ago",seconds:"Less than a minute",minute:"About a minute",minutes:"%d minutes",hour:"About an hour",hours:"About %d hours",day:"A day",days:"%d days",month:"About a month",months:"%d months",year:"About a year",years:"%d years"}}}