import{strftime as e,makeFormatter as t,makeRelativeFormat as a,isDayFirst as r,isThisYear as o,isYearSeparator as i}from"./utils.js";export default class RelativeTime{constructor(e,t){this.date=e,this.locale=t}toString(){const e=this.timeElapsed();if(e)return e;{const e=this.timeAhead();return e||`on ${this.formatDate()}`}}timeElapsed(){const e=(new Date).getTime()-this.date.getTime(),t=Math.round(e/1e3),a=Math.round(t/60),r=Math.round(a/60),o=Math.round(r/24);return e>=0&&o<30?this.timeAgoFromMs(e):null}timeAhead(){const e=this.date.getTime()-(new Date).getTime(),t=Math.round(e/1e3),a=Math.round(t/60),r=Math.round(a/60),o=Math.round(r/24);return e>=0&&o<30?this.timeUntil():null}timeAgo(){const e=(new Date).getTime()-this.date.getTime();return this.timeAgoFromMs(e)}timeAgoFromMs(e){const t=Math.round(e/1e3),a=Math.round(t/60),r=Math.round(a/60),o=Math.round(r/24),i=Math.round(o/30),n=Math.round(i/12);return e<0||t<10?formatRelativeTime(this.locale,0,"second"):t<45?formatRelativeTime(this.locale,-t,"second"):t<90||a<45?formatRelativeTime(this.locale,-a,"minute"):a<90||r<24?formatRelativeTime(this.locale,-r,"hour"):r<36||o<30?formatRelativeTime(this.locale,-o,"day"):i<18?formatRelativeTime(this.locale,-i,"month"):formatRelativeTime(this.locale,-n,"year")}microTimeAgo(){const e=(new Date).getTime()-this.date.getTime(),t=Math.round(e/1e3),a=Math.round(t/60),r=Math.round(a/60),o=Math.round(r/24),i=Math.round(o/30),n=Math.round(i/12);return a<1?"1m":a<60?`${a}m`:r<24?`${r}h`:o<365?`${o}d`:`${n}y`}timeUntil(){const e=this.date.getTime()-(new Date).getTime();return this.timeUntilFromMs(e)}timeUntilFromMs(e){const t=Math.round(e/1e3),a=Math.round(t/60),r=Math.round(a/60),o=Math.round(r/24),i=Math.round(o/30),n=Math.round(i/12);return i>=18||i>=12?formatRelativeTime(this.locale,n,"year"):o>=45||o>=30?formatRelativeTime(this.locale,i,"month"):r>=36||r>=24?formatRelativeTime(this.locale,o,"day"):a>=90||a>=45?formatRelativeTime(this.locale,r,"hour"):t>=90||t>=45?formatRelativeTime(this.locale,a,"minute"):formatRelativeTime(this.locale,t>=10?t:0,"second")}microTimeUntil(){const e=this.date.getTime()-(new Date).getTime(),t=Math.round(e/1e3),a=Math.round(t/60),r=Math.round(a/60),o=Math.round(r/24),i=Math.round(o/30),n=Math.round(i/12);return o>=365?`${n}y`:r>=24?`${o}d`:a>=60?`${r}h`:a>1?`${a}m`:"1m"}formatDate(){let t=r()?"%e %b":"%b %e";return o(this.date)||(t+=i()?", %Y":" %Y"),e(this.date,t)}formatTime(){const t=n();return t?t.format(this.date):e(this.date,"%l:%M%P")}}function formatRelativeTime(e,t,r){const o=a(e,{numeric:"auto"});return o?o.format(t,r):function formatEnRelativeTime(e,t){if(0===e)switch(t){case"year":case"quarter":case"month":case"week":return`this ${t}`;case"day":return"today";case"hour":case"minute":return`in 0 ${t}s`;case"second":return"now"}else if(1===e)switch(t){case"year":case"quarter":case"month":case"week":return`next ${t}`;case"day":return"tomorrow";case"hour":case"minute":case"second":return`in 1 ${t}`}else if(-1===e)switch(t){case"year":case"quarter":case"month":case"week":return`last ${t}`;case"day":return"yesterday";case"hour":case"minute":case"second":return`1 ${t} ago`}else if(e>1)switch(t){case"year":case"quarter":case"month":case"week":case"day":case"hour":case"minute":case"second":return`in ${e} ${t}s`}else if(e<-1)switch(t){case"year":case"quarter":case"month":case"week":case"day":case"hour":case"minute":case"second":return`${-e} ${t}s ago`}throw new RangeError(`Invalid unit argument for format() '${t}'`)}(t,r)}const n=t({hour:"numeric",minute:"2-digit"});