import { LogService as e } from "../internal/services/log.service.js";
import { typeSSR as t } from "../internal/controllers/type-ssr.controller.js";
import { useStyles as n } from "../internal/styles/index.js";
import { __decorate as r } from "../_virtual/_@oxc-project_runtime@0.133.0/helpers/esm/decorate.js";
import i from "./format-relative-time.js";
import { LitElement as a, html as o } from "lit";
import { property as s } from "lit/decorators/property.js";
//#region src/format-relative-time/format-relative-time.ts
var c = {
	second: 1e3,
	minute: 6e4,
	hour: 36e5,
	day: 864e5,
	week: 6048e5,
	month: 2592e6,
	year: 31536e6
}, l = [
	{
		unit: "second",
		max: 60
	},
	{
		unit: "minute",
		max: 60
	},
	{
		unit: "hour",
		max: 24
	},
	{
		unit: "day",
		max: 7
	},
	{
		unit: "week",
		max: 4
	},
	{
		unit: "month",
		max: 12
	},
	{
		unit: "year",
		max: Infinity
	}
], u = class extends a {
	constructor(...e) {
		super(...e), this.numeric = "always", this.formatStyle = "long", this.unit = "auto", this.sync = !1;
	}
	static {
		this.styles = n([i]);
	}
	static {
		this.metadata = {
			tag: "nve-format-relative-time",
			version: "2.0.2"
		};
	}
	#e;
	get #t() {
		return this.date ?? this.textContent?.trim() ?? "";
	}
	get #n() {
		return this.locale ?? (globalThis.document?.documentElement?.lang || void 0);
	}
	#r(e) {
		let t = Math.abs(e), n = e < 0 ? -1 : 1;
		for (let { unit: e, max: r } of l) {
			let i = Math.round(t / c[e]);
			if (i < r) return {
				value: n * i,
				unit: e
			};
		}
		return {
			value: n * Math.round(t / c.year),
			unit: "year"
		};
	}
	#i(e, t) {
		return Math.round(e / c[t]);
	}
	#a(e) {
		return this.unit === "auto" ? this.#r(e) : {
			unit: this.unit,
			value: this.#i(e, this.unit)
		};
	}
	get #o() {
		let t = this.#t;
		if (!t) return "";
		let n = new Date(t);
		if (isNaN(n.getTime())) return e.warn(`format-relative-time: invalid date value "${t}"`), t;
		let { value: r, unit: i } = this.#a(n.getTime() - Date.now());
		try {
			return new Intl.RelativeTimeFormat(this.#n, {
				numeric: this.numeric,
				style: this.formatStyle
			}).format(r, i);
		} catch (n) {
			return e.warn(`format-relative-time: ${n.message}`), t;
		}
	}
	#s() {
		let e = this.#t;
		if (!e) return 6e4;
		let t = Math.abs(new Date(e).getTime() - Date.now());
		return t < 6e4 ? 1e4 : t < 36e5 ? 3e4 : t < 864e5 ? 3e5 : 36e5;
	}
	#c() {
		this.#l(), this.sync && (this.#e = setTimeout(() => {
			this.requestUpdate(), this.#c();
		}, this.#s()));
	}
	#l() {
		this.#e !== void 0 && (clearTimeout(this.#e), this.#e = void 0);
	}
	connectedCallback() {
		super.connectedCallback(), this.sync && this.#c();
	}
	disconnectedCallback() {
		this.#l(), super.disconnectedCallback();
	}
	updated(e) {
		e.has("sync") && (this.sync ? this.#c() : this.#l());
	}
	render() {
		return o`<time internal-host datetime="${this.#t}">${this.#o}<slot @slotchange="${this.#u}" hidden></slot></time>`;
	}
	#u() {
		this.requestUpdate();
	}
};
r([s({ type: String })], u.prototype, "date", void 0), r([s({ type: String })], u.prototype, "locale", void 0), r([s({ type: String })], u.prototype, "numeric", void 0), r([s({
	type: String,
	attribute: "format-style"
})], u.prototype, "formatStyle", void 0), r([s({ type: String })], u.prototype, "unit", void 0), r([s({ type: Boolean })], u.prototype, "sync", void 0), u = r([t()], u);
//#endregion
export { u as FormatRelativeTime };

//# sourceMappingURL=format-relative-time2.js.map