/* * This file is part of the TYPO3 CMS project. * * It is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License, either version 2 * of the License, or any later version. * * For the full copyright and license information, please read the * LICENSE.txt file that was distributed with this source code. * * The TYPO3 project - inspiring people to share! */ import{LitElement as h,html as p,nothing as f}from"lit";import{property as c,state as T,customElement as D}from"lit/decorators.js";import{DateTime as u}from"luxon";var m=function(n,t,e,i){var o=arguments.length,a=o<3?t:i===null?i=Object.getOwnPropertyDescriptor(t,e):i,r;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")a=Reflect.decorate(n,t,e,i);else for(var l=n.length-1;l>=0;l--)(r=n[l])&&(a=(o<3?r(a):o>3?r(t,e,a):r(t,e))||a);return o>3&&a&&Object.defineProperty(t,e,a),a},d;(function(n){n.Relative="relative",n.Absolute="absolute"})(d||(d={}));let s=class extends h{constructor(){super(...arguments),this.datetime="",this.mode=d.Absolute,this.format="date",this.thresholdDays=0,this.relativeShowSeconds=!1,this.displayTime="",this.updateTimeoutId=null}disconnectedCallback(){super.disconnectedCallback(),this.clearUpdateTimeout()}willUpdate(t){(t.has("datetime")||t.has("mode")||t.has("format")||t.has("relativeShowSeconds"))&&(this.updateDisplayTime(),this.clearUpdateTimeout(),this.startUpdateTimeout())}createRenderRoot(){return this}render(){const t=this.parseDateTime(this.datetime),e=t?.toISO()||this.datetime,i=t&&this.mode===d.Relative?this.formatAbsoluteDateTime(this.getLocalizedDateTime(t)):"",o=i?`${this.displayTime||this.datetime}. ${i}`:null;return p``}clearUpdateTimeout(){this.updateTimeoutId!==null&&(window.clearTimeout(this.updateTimeoutId),this.updateTimeoutId=null)}startUpdateTimeout(){if(this.mode===d.Relative){const t=this.parseDateTime(this.datetime),e=t?this.calculateNextStateChange(t):null,i=e?Math.abs(e.diffNow("milliseconds").milliseconds):1e3;this.updateTimeoutId=window.setTimeout(()=>{this.updateTimeoutId=null,this.updateDisplayTime(),this.startUpdateTimeout()},i)}}calculateNextStateChange(t){const e=["years","months","days","hours","minutes","seconds"],i=t.diffNow(e);for(const o of e){const a=i.get(o);if(a!==0){const r=a<0?-1:1;return t.minus({[o]:a+r})}}return t.plus({seconds:1})}updateDisplayTime(){const t=this.parseDateTime(this.datetime);t?this.displayTime=this.mode===d.Absolute?this.formatAbsoluteTime(t):this.formatRelativeTime(t):this.displayTime=this.datetime}formatAbsoluteTime(t){const e=this.getLocalizedDateTime(t),i=this.getConfiguredDateFormats();return this.format==="date"&&i?e.toFormat(i.formats.date):this.format==="datetime"&&i?e.toFormat(i.formats.datetime):this.format?e.toFormat(this.format):i?e.toFormat(i.formats.date):e.toLocaleString(u.DATE_SHORT)}parseDateTime(t){if(!t)return null;const i=this.getConfiguredDateFormats()?.timezone,o=Number(t);if(!isNaN(o)&&o>0){const r=i?{zone:i}:{},l=u.fromSeconds(o,r);if(l.isValid)return l}const a=u.fromISO(t);return a.isValid?a:null}getLocalizedDateTime(t){let e=document.documentElement.lang||"en";e==="ch"&&(e="zh");const o=this.getConfiguredDateFormats()?.timezone;let a=t.setLocale(e);return o&&(a=a.setZone(o)),a}formatRelativeTime(t){const e=this.getLocalizedDateTime(t);if(this.thresholdDays>0&&Math.abs(u.now().diff(e,"days").days)>this.thresholdDays)return this.formatAbsoluteDate(e);if(!this.relativeShowSeconds){const o=e.diffNow("seconds").seconds;if(Math.abs(o)<60)return e.toRelative({unit:"minutes",rounding:"expand"})||this.formatAbsoluteDateTime(e)}return e.toRelative()||this.formatAbsoluteDateTime(e)}formatAbsoluteDate(t){const e=this.getLocalizedDateTime(t),i=this.getConfiguredDateFormats();return i?e.toFormat(i.formats.date):e.toLocaleString(u.DATE_SHORT)}formatAbsoluteDateTime(t){const e=this.getLocalizedDateTime(t),i=this.getConfiguredDateFormats();return i?e.toFormat(i.formats.datetime):e.toLocaleString(u.DATETIME_SHORT)}getConfiguredDateFormats(){try{return(typeof opener?.top?.TYPO3<"u"?opener.top:top).TYPO3.settings.DateConfiguration}catch{return null}}};m([c({type:String})],s.prototype,"datetime",void 0),m([c()],s.prototype,"mode",void 0),m([c()],s.prototype,"format",void 0),m([c({type:Number,attribute:"threshold-days"})],s.prototype,"thresholdDays",void 0),m([c({type:Boolean,attribute:"relative-show-seconds"})],s.prototype,"relativeShowSeconds",void 0),m([T()],s.prototype,"displayTime",void 0),s=m([D("typo3-backend-datetime")],s);export{d as DateTimeDisplayMode,s as DateTimeElement};