import { t as e } from "../chunks/ui.module-BisoTevy.js";
import { i as t, l as n, r, s as i } from "../chunks/input-Bp6_Ucgr.js";
import { useCallback as a, useMemo as o, useRef as s, useState as c } from "react";
import { cn as l } from "@ryuzaki13/react-foundation-lib/utils";
import { jsx as u } from "react/jsx-runtime";
import { dateToIndexedSegmentValues as d, indexedSegmentsToDate as f, parseDateSegmentMask as p } from "@ryuzaki13/react-foundation-lib/date-segments";
var m = {
	segmentInput: "t1iysX",
	separator: "_-4oX-p"
};
//#endregion
//#region src/input-date-time/maskParser.ts
function h(e) {
	return p(e).map((e) => e.kind === "editable" ? {
		...e,
		type: e.id
	} : e);
}
function g(e, t) {
	let n = d(e, t), r = /* @__PURE__ */ new Map();
	return t.forEach((e, t) => {
		e.kind === "editable" && r.set(e.type, n.get(t) ?? "");
	}), r;
}
function _(e, t) {
	let n = /* @__PURE__ */ new Map();
	return e.forEach((e, r) => {
		e.kind === "editable" && n.set(r, t.get(e.type) ?? "");
	}), f(e, n);
}
//#endregion
//#region src/input-date-time/useMaskedSegments.ts
function v(e, t) {
	let n = g(e, t), r = /* @__PURE__ */ new Map();
	return t.forEach((e, t) => {
		e.kind === "editable" && r.set(t, n.get(e.type) ?? "");
	}), r;
}
function y(e) {
	let t = [];
	return e.forEach((e, n) => {
		e.kind === "editable" && t.push(n);
	}), t;
}
function b({ mask: e, value: t, onChange: n, onClearError: r, disabled: i }) {
	let l = o(() => h(e), [e]), u = o(() => y(l), [l]), d = o(() => v(t, l), [t, l]), [f, p] = c(null), [m, g] = c(() => t?.getTime()), b = t?.getTime();
	b !== m && (g(b), p(null));
	let x = f ?? d, S = s(/* @__PURE__ */ new Map()), C = s(0), [w, T] = c(!1), E = a((e) => {
		let t = /* @__PURE__ */ new Map();
		l.forEach((n, r) => {
			n.kind === "editable" && t.set(n.type, e.get(r) ?? "");
		});
		let r = _(l, t);
		r !== null && n(r);
	}, [l, n]), D = a((e, t = !0) => {
		let n = S.current.get(e);
		n && (n.focus(), t && requestAnimationFrame(() => n.select()));
	}, []), O = a((e) => {
		let t = u.indexOf(e);
		return t < u.length - 1 ? u[t + 1] : null;
	}, [u]), k = a((e) => {
		let t = u.indexOf(e);
		return t > 0 ? u[t - 1] : null;
	}, [u]), A = a((e, t) => e.padStart(t.length, "0"), []);
	return {
		segments: l,
		segmentValues: x,
		isFocused: w,
		registerRef: a((e) => (t) => {
			t ? S.current.set(e, t) : S.current.delete(e);
		}, []),
		handleInput: a((e) => (t) => {
			if (i) return;
			let n = l[e], a = t.target.value.replace(/\D/g, "").slice(0, n.length), o = new Map(x);
			if (o.set(e, a), p(o), r?.(), a.length === n.length) {
				let t = O(e);
				t !== null && requestAnimationFrame(() => D(t));
			}
			E(o);
		}, [
			i,
			l,
			x,
			r,
			O,
			D,
			E
		]),
		handleKeyDown: a((e) => (t) => {
			if (i) return;
			let n = l[e], r = x.get(e) ?? "", a = t.currentTarget;
			switch (t.key) {
				case "ArrowRight":
					if (a.selectionStart === r.length || r === "") {
						t.preventDefault();
						let n = O(e);
						n !== null && D(n);
					}
					break;
				case "ArrowLeft":
					if (a.selectionStart === 0 || r === "") {
						t.preventDefault();
						let n = k(e);
						n !== null && D(n);
					}
					break;
				case "ArrowUp": {
					t.preventDefault();
					let i = r === "" ? n.min : parseInt(r, 10) + 1;
					i > n.max && (i = n.min);
					let o = A(i.toString(), n), s = new Map(x);
					s.set(e, o), p(s), E(s), requestAnimationFrame(() => a.select());
					break;
				}
				case "ArrowDown": {
					t.preventDefault();
					let i = r === "" ? n.max : parseInt(r, 10) - 1;
					i < n.min && (i = n.max);
					let o = A(i.toString(), n), s = new Map(x);
					s.set(e, o), p(s), E(s), requestAnimationFrame(() => a.select());
					break;
				}
				case "Backspace":
					if (r === "") {
						t.preventDefault();
						let n = k(e);
						n !== null && D(n);
					}
					break;
				case "Delete": {
					t.preventDefault();
					let n = new Map(x);
					n.set(e, ""), p(n), E(n);
					break;
				}
			}
		}, [
			i,
			l,
			x,
			O,
			k,
			D,
			A,
			E
		]),
		handleFocus: a(() => (e) => {
			e.target.select();
		}, []),
		handleContainerFocus: a(() => {
			clearTimeout(C.current), T(!0);
		}, []),
		handleContainerBlur: a(() => {
			C.current = window.setTimeout(() => {
				T(!1), p(null);
			}, 0);
		}, []),
		focusFirstEmpty: a(() => {
			let e = u.find((e) => !(x.get(e) ?? "")) ?? u[u.length - 1];
			e !== void 0 && D(e);
		}, [
			u,
			x,
			D
		])
	};
}
//#endregion
//#region src/input-date-time/InputDateTime.tsx
function x({ mask: a, value: o, onChange: s, label: c, description: d, disabled: f, size: p, className: h, error: g, onClearError: _, onClear: v, "aria-label": y }) {
	let { segments: x, segmentValues: S, registerRef: C, handleInput: w, handleKeyDown: T, handleFocus: E, handleContainerFocus: D, handleContainerBlur: O, focusFirstEmpty: k } = b({
		mask: a,
		value: o,
		onChange: s,
		onClearError: _,
		disabled: f
	}), { controlId: A, labelId: j, descriptionId: M, errorId: N, describedBy: P } = n({
		hasLabel: c != null,
		hasDescription: !!d,
		hasError: !!g
	});
	return /* @__PURE__ */ u(i, {
		label: c,
		description: d,
		disabled: f,
		className: h,
		size: p,
		error: g,
		labelId: j,
		descriptionId: M,
		errorId: N,
		children: /* @__PURE__ */ u(t, {
			endAdornment: v ? /* @__PURE__ */ u(r, {
				onClick: v,
				disabled: f
			}) : void 0,
			endAdornmentWidth: v ? "var(--control-height)" : void 0,
			children: ({ controlClassName: t }) => /* @__PURE__ */ u("div", {
				id: A,
				className: l(e.uiInputControlFake, t),
				"data-invalid": g ? "" : void 0,
				"data-disabled": f || void 0,
				role: "group",
				"aria-invalid": !!g || void 0,
				"aria-label": y || (typeof c == "string" ? c : void 0),
				"aria-labelledby": y ? void 0 : j,
				"aria-describedby": P,
				onFocus: D,
				onBlur: O,
				onClick: (e) => {
					e.target === e.currentTarget && k();
				},
				children: x.map((e, t) => e.kind === "literal" ? /* @__PURE__ */ u("span", {
					className: m.separator,
					"data-disabled": f || void 0,
					children: e.text
				}, t) : /* @__PURE__ */ u("input", {
					ref: C(t),
					type: "text",
					inputMode: "numeric",
					className: m.segmentInput,
					style: { width: `${e.length + .5}ch` },
					value: S.get(t) ?? "",
					placeholder: e.placeholder,
					maxLength: e.length,
					disabled: f,
					"aria-label": e.type,
					autoComplete: "off",
					onChange: w(t),
					onKeyDown: T(t),
					onFocus: E()
				}, t))
			})
		})
	});
}
function S({ mask: e = "YYYY.MM.DD", ...t }) {
	return /* @__PURE__ */ u(x, {
		mask: e,
		...t
	});
}
function C({ mask: e = "hh:mm", ...t }) {
	return /* @__PURE__ */ u(x, {
		mask: e,
		...t
	});
}
function w({ mask: e = "YYYY.MM.DD hh:mm", ...t }) {
	return /* @__PURE__ */ u(x, {
		mask: e,
		...t
	});
}
//#endregion
export { S as InputDate, w as InputDateTime, C as InputTime };

//# sourceMappingURL=index.js.map