import { t as e } from "./clsx.js";
import { useInternalStatus as t } from "../ui/hooks/useInternalStatus.js";
import { FormControl as n } from "../ui/forms/FormControl.js";
import { useValue as r } from "../ui/hooks/useValue.js";
import { useCallback as i, useEffect as a, useId as o, useRef as s, useState as c } from "react";
import { Icon as l } from "@clubmed/trident-icons";
import { jsx as u, jsxs as d } from "react/jsx-runtime";
//#region ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/constants.js
var f = 365.2425, p = 6e4, m = 36e5, h = 3600 * 24;
h * 7, h * f / 12 * 3;
var g = Symbol.for("constructDateFrom");
//#endregion
//#region ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/constructFrom.js
function _(e, t) {
	return typeof e == "function" ? e(t) : e && typeof e == "object" && g in e ? e[g](t) : e instanceof Date ? new e.constructor(t) : new Date(t);
}
//#endregion
//#region ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/toDate.js
function v(e, t) {
	return _(t || e, e);
}
//#endregion
//#region ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/isDate.js
function y(e) {
	return e instanceof Date || typeof e == "object" && Object.prototype.toString.call(e) === "[object Date]";
}
//#endregion
//#region ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/isValid.js
function b(e) {
	return !(!y(e) && typeof e != "number" || isNaN(+v(e)));
}
//#endregion
//#region ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/differenceInMilliseconds.js
function x(e, t) {
	return v(e) - +v(t);
}
//#endregion
//#region ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/_lib/addLeadingZeros.js
function S(e, t) {
	return (e < 0 ? "-" : "") + Math.abs(e).toString().padStart(t, "0");
}
//#endregion
//#region ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/formatISO.js
function C(e, t) {
	let n = v(e, t?.in);
	if (isNaN(+n)) throw RangeError("Invalid time value");
	let r = t?.format ?? "extended", i = t?.representation ?? "complete", a = "", o = "", s = r === "extended" ? "-" : "", c = r === "extended" ? ":" : "";
	if (i !== "time") {
		let e = S(n.getDate(), 2), t = S(n.getMonth() + 1, 2);
		a = `${S(n.getFullYear(), 4)}${s}${t}${s}${e}`;
	}
	if (i !== "date") {
		let e = n.getTimezoneOffset();
		if (e !== 0) {
			let t = Math.abs(e), n = S(Math.trunc(t / 60), 2), r = S(t % 60, 2);
			o = `${e < 0 ? "+" : "-"}${n}:${r}`;
		} else o = "Z";
		let t = S(n.getHours(), 2), r = S(n.getMinutes(), 2), i = S(n.getSeconds(), 2), s = a === "" ? "" : "T", l = [
			t,
			r,
			i
		].join(c);
		a = `${a}${s}${l}${o}`;
	}
	return a;
}
//#endregion
//#region ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/parseISO.js
function w(e, t) {
	let n = () => _(t?.in, NaN), r = t?.additionalDigits ?? 2, i = k(e), a;
	if (i.date) {
		let e = A(i.date, r);
		a = j(e.restDateString, e.year);
	}
	if (!a || isNaN(+a)) return n();
	let o = +a, s = 0, c;
	if (i.time && (s = N(i.time), isNaN(s))) return n();
	if (i.timezone) {
		if (c = F(i.timezone), isNaN(c)) return n();
	} else {
		let e = new Date(o + s), n = v(0, t?.in);
		return n.setFullYear(e.getUTCFullYear(), e.getUTCMonth(), e.getUTCDate()), n.setHours(e.getUTCHours(), e.getUTCMinutes(), e.getUTCSeconds(), e.getUTCMilliseconds()), n;
	}
	return v(o + s + c, t?.in);
}
var T = {
	dateTimeDelimiter: /[T ]/,
	timeZoneDelimiter: /[Z ]/i,
	timezone: /([Z+-].*)$/
}, E = /^-?(?:(\d{3})|(\d{2})(?:-?(\d{2}))?|W(\d{2})(?:-?(\d{1}))?|)$/, D = /^(\d{2}(?:[.,]\d*)?)(?::?(\d{2}(?:[.,]\d*)?))?(?::?(\d{2}(?:[.,]\d*)?))?$/, O = /^([+-])(\d{2})(?::?(\d{2}))?$/;
function k(e) {
	let t = {}, n = e.split(T.dateTimeDelimiter), r;
	if (n.length > 2) return t;
	if (/:/.test(n[0]) ? r = n[0] : (t.date = n[0], r = n[1], T.timeZoneDelimiter.test(t.date) && (t.date = e.split(T.timeZoneDelimiter)[0], r = e.substr(t.date.length, e.length))), r) {
		let e = T.timezone.exec(r);
		e ? (t.time = r.replace(e[1], ""), t.timezone = e[1]) : t.time = r;
	}
	return t;
}
function A(e, t) {
	let n = RegExp("^(?:(\\d{4}|[+-]\\d{" + (4 + t) + "})|(\\d{2}|[+-]\\d{" + (2 + t) + "})$)"), r = e.match(n);
	if (!r) return {
		year: NaN,
		restDateString: ""
	};
	let i = r[1] ? parseInt(r[1]) : null, a = r[2] ? parseInt(r[2]) : null;
	return {
		year: a === null ? i : a * 100,
		restDateString: e.slice((r[1] || r[2]).length)
	};
}
function j(e, t) {
	if (t === null) return /* @__PURE__ */ new Date(NaN);
	let n = e.match(E);
	if (!n) return /* @__PURE__ */ new Date(NaN);
	let r = !!n[4], i = M(n[1]), a = M(n[2]) - 1, o = M(n[3]), s = M(n[4]), c = M(n[5]) - 1;
	if (r) return V(t, s, c) ? I(t, s, c) : /* @__PURE__ */ new Date(NaN);
	{
		let e = /* @__PURE__ */ new Date(0);
		return !z(t, a, o) || !B(t, i) ? /* @__PURE__ */ new Date(NaN) : (e.setUTCFullYear(t, a, Math.max(i, o)), e);
	}
}
function M(e) {
	return e ? parseInt(e) : 1;
}
function N(e) {
	let t = e.match(D);
	if (!t) return NaN;
	let n = P(t[1]), r = P(t[2]), i = P(t[3]);
	return H(n, r, i) ? n * m + r * p + i * 1e3 : NaN;
}
function P(e) {
	return e && parseFloat(e.replace(",", ".")) || 0;
}
function F(e) {
	if (e === "Z") return 0;
	let t = e.match(O);
	if (!t) return 0;
	let n = t[1] === "+" ? -1 : 1, r = parseInt(t[2]), i = t[3] && parseInt(t[3]) || 0;
	return U(r, i) ? n * (r * m + i * p) : NaN;
}
function I(e, t, n) {
	let r = /* @__PURE__ */ new Date(0);
	r.setUTCFullYear(e, 0, 4);
	let i = r.getUTCDay() || 7, a = (t - 1) * 7 + n + 1 - i;
	return r.setUTCDate(r.getUTCDate() + a), r;
}
var L = [
	31,
	null,
	31,
	30,
	31,
	30,
	31,
	31,
	30,
	31,
	30,
	31
];
function R(e) {
	return e % 400 == 0 || e % 4 == 0 && e % 100 != 0;
}
function z(e, t, n) {
	return t >= 0 && t <= 11 && n >= 1 && n <= (L[t] || (R(e) ? 29 : 28));
}
function B(e, t) {
	return t >= 1 && t <= (R(e) ? 366 : 365);
}
function V(e, t, n) {
	return t >= 1 && t <= 53 && n >= 0 && n <= 6;
}
function H(e, t, n) {
	return e === 24 ? t === 0 && n === 0 : n >= 0 && n < 60 && t >= 0 && t < 60 && e >= 0 && e < 25;
}
function U(e, t) {
	return t >= 0 && t <= 59;
}
//#endregion
//#region lib/ui/forms/DateField.tsx
var W = (e) => e ? typeof e == "string" ? e : b(new Date(e)) ? C(new Date(e), { representation: "date" }) : "" : "", G = (e, t, n) => {
	if (!e) return !0;
	let r = (e) => new Date(e.getFullYear(), e.getMonth(), e.getDate()), i = r(w(e));
	return !(t && i < r(t) || n && i > r(n));
}, K = (f) => {
	let p = o(), { id: m = p, name: h = m, label: g, description: _, openDatepicker: v, validationStatus: y = "default", icon: b = "CalendarDefault", iconType: S, errorMessage: C, disabled: T = !1, required: E = !1, hideRequiredStar: D, dataTestId: O = "DateField", value: k, min: A, max: j, onChange: M, className: N, ...P } = f, F = s(null), [I, L] = c(!1), R = t({
		isDisabled: T,
		validationStatus: y
	}), { setValue: z, value: B } = r({
		formatter: W,
		initialValue: k,
		onChange: (e, t) => M?.(e, t ? w(t) : null)
	}), V = W(A), H = W(j), U = (e) => {
		z(e.target.value, e.nativeEvent);
	}, K = () => {
		F.current?.showPicker();
	}, q = i(() => {
		let e = w(B);
		if (A && j) {
			z(Math.abs(x(e, A)) <= Math.abs(x(e, j)) ? V : H);
			return;
		}
		if (A) {
			z(V);
			return;
		}
		if (j) {
			z(H);
			return;
		}
	}, [
		j,
		A,
		H,
		V,
		z,
		B
	]);
	return a(() => {
		!G(B, A, j) && !I && q();
	}, [
		q,
		j,
		A,
		I,
		B
	]), /* @__PURE__ */ u(n, {
		id: m,
		label: g,
		description: _,
		dataName: "DateField",
		dataTestId: O,
		disabled: T,
		required: E,
		hideRequiredStar: D,
		validationStatus: y,
		errorMessage: C,
		className: N,
		children: /* @__PURE__ */ d("div", {
			className: "relative",
			role: "group",
			children: [/* @__PURE__ */ u("input", {
				...P,
				type: "date",
				className: e("text-b3 rounded-pill date-field min-h-[50px] w-full border px-20 py-12 ps-[52px] font-normal outline-none", {
					"border-middleGrey focus:border-black active:border-black": R === "default",
					"pe-[52px]": R === "error" || R === "success",
					"bg-pearl border-middleGrey": T,
					"bg-white text-black": !T,
					"border-red": !T && R === "error",
					"border-green": !T && R === "success"
				}),
				id: m,
				name: h,
				disabled: T,
				required: E,
				ref: F,
				value: B,
				min: V,
				max: H,
				onChange: U,
				onFocus: () => L(!0),
				onBlur: () => L(!1)
			}), /* @__PURE__ */ d("div", {
				className: e("pointer-events-none absolute inset-0 flex items-center justify-between px-20 py-12", {
					"text-grey": T,
					"text-red": !T && R === "error",
					"text-green": !T && R === "success"
				}),
				children: [/* @__PURE__ */ d("button", {
					className: "pointer-events-auto leading-none",
					onClick: K,
					children: [/* @__PURE__ */ u(l, {
						name: b,
						iconType: S,
						width: "24px"
					}), /* @__PURE__ */ u("span", {
						className: "sr-only",
						children: v
					})]
				}), /* @__PURE__ */ d("span", {
					className: "ms-auto flex gap-x-8",
					children: [R === "error" && /* @__PURE__ */ u(l, {
						name: "CrossDefault",
						width: "24px"
					}), R === "success" && /* @__PURE__ */ u(l, {
						name: "CheckDefault",
						width: "24px"
					})]
				})]
			})]
		})
	});
};
//#endregion
export { K as t };

//# sourceMappingURL=DateField.js.map