import { n as l, C as h } from "../chunks/lib-cortex-element.CVMmyPMC.js";
import { c as p } from "../chunks/component.styles.CRO4Odto.js";
import { c as v } from "../chunks/custom-element.L4WJXn1j.js";
import { L as d } from "../chunks/i18n.BABe9hN4.js";
import { i as f, x as y } from "../chunks/lit-element.jLBm65_O.js";
import { r as c } from "../chunks/state.CSDxrqLd.js";
const T = f`:host{display:contents}.relative-time{color:var(--cx-relative-time-color,inherit);font-size:var(--cx-relative-time-font-size,inherit);font-weight:var(--cx-relative-time-font-weight,inherit)}`;
var x = Object.defineProperty, w = Object.getOwnPropertyDescriptor, a = (o, t, i, s) => {
  for (var n = s > 1 ? void 0 : s ? w(t, i) : t, r = o.length - 1, u; r >= 0; r--)
    (u = o[r]) && (n = (s ? u(t, i, n) : u(n)) || n);
  return s && n && x(t, i, n), n;
};
const I = [
  { max: 46e3, unit: "second", value: 1e3 },
  // max 46 seconds
  { max: 276e4, unit: "minute", value: 6e4 },
  // max 46 minutes
  { max: 72e6, unit: "hour", value: 36e5 },
  // max 20 hours
  { max: 5184e5, unit: "day", value: 864e5 },
  // max 6 days
  { max: 24192e5, unit: "week", value: 6048e5 },
  // max 28 days
  { max: 28512e6, unit: "month", value: 2592e6 },
  // max 11 months
  { max: 1 / 0, unit: "year", value: 31536e6 }
];
let e = class extends h {
  constructor() {
    super(...arguments), this.localize = new d(this), this.isoTime = "", this.relativeTime = "", this.date = /* @__PURE__ */ new Date(), this.format = "long", this.numeric = "auto", this.sync = !1, this.syncInterval = -1;
  }
  connectedCallback() {
    super.connectedCallback(), this.sync && this.syncInterval > 0 && (this.updateTimeout = window.setTimeout(
      () => this.requestUpdate(),
      this.syncInterval
    ));
  }
  disconnectedCallback() {
    super.disconnectedCallback(), this.updateTimeout && clearTimeout(this.updateTimeout), this.updateTimeout = null;
  }
  render() {
    const o = /* @__PURE__ */ new Date(), t = new Date(this.date);
    if (isNaN(t.getMilliseconds()))
      return this.relativeTime = "", this.isoTime = "", "";
    const i = t.getTime() - o.getTime(), { unit: s, value: n } = I.find(
      (r) => Math.abs(i) < r.max
    );
    return this.isoTime = t.toISOString(), this.relativeTime = this.localize.relativeTime(
      Math.round(i / n),
      s,
      {
        numeric: this.numeric,
        style: this.format
      }
    ), this.updateTimeout && clearTimeout(this.updateTimeout), this.updateTimeout = null, this.sync && (this.syncInterval < 0 && (s === "minute" ? this.syncInterval = m("second") : s === "hour" ? this.syncInterval = m("minute") : s === "day" ? this.syncInterval = m("hour") : this.syncInterval = m("day")), this.updateTimeout = window.setTimeout(
      () => this.requestUpdate(),
      this.syncInterval
    )), y`<time class="relative-time" datetime=${this.isoTime}>${this.relativeTime}</time>`;
  }
};
e.styles = [p, T];
a([
  c()
], e.prototype, "isoTime", 2);
a([
  c()
], e.prototype, "relativeTime", 2);
a([
  l()
], e.prototype, "date", 2);
a([
  l()
], e.prototype, "format", 2);
a([
  l()
], e.prototype, "numeric", 2);
a([
  l({ type: Boolean })
], e.prototype, "sync", 2);
a([
  l({ attribute: "sync-interval", reflect: !0, type: Number })
], e.prototype, "syncInterval", 2);
e = a([
  v("cx-relative-time")
], e);
function m(o) {
  const i = { day: 864e5, hour: 36e5, minute: 6e4, second: 1e3 }[o];
  return i - Date.now() % i;
}
export {
  e as default
};
