import { t as e } from "../chunks/ui.module-DHVTOqZ0.js";
import { c as t, d as n } from "../chunks/input-CCdBtl-C.js";
import { useImperativeHandle as r, useRef as i, useState as a } from "react";
import { cn as o } from "@ryuzaki13/react-foundation-lib/utils";
import { jsx as s, jsxs as c } from "react/jsx-runtime";
import { XIcon as ee } from "lucide-react";
//#region src/tag-input/lib/joinTagInputAriaIds.ts
function l(...e) {
	return e.filter(Boolean).join(" ") || void 0;
}
//#endregion
//#region src/tag-input/lib/tagInputDefaults.ts
function te(e) {
	return e.trim();
}
function ne(e) {
	return e;
}
function u(e) {
	return `Удалить тег «${e}»`;
}
var d = {
	control: "AoFB3K",
	tagList: "PvaUEA",
	tag: "FZ-xEM",
	tagText: "-RCqfG",
	removeButton: "ggY643",
	input: "hiZq-y"
}, f = /[,\r\n]/, p = /[,\r\n]+/;
function m({ ref: m, value: h, onChange: g, label: _, description: v, placeholder: y, size: b, disabled: x, readOnly: S = !1, error: C, onClearError: w, maxTags: T, normalizeTag: E = te, getTagKey: D = ne, addOnBlur: O = !0, getRemoveButtonAriaLabel: re = u, tokensAriaLabel: ie = "Добавленные теги", inputClassName: ae, className: oe, id: se, name: k, form: A, required: ce, maxLength: j, onKeyDown: M, onPaste: N, onBlur: P, onCompositionStart: F, onCompositionEnd: I, "aria-describedby": L, "aria-labelledby": R, ...z }) {
	let B = i(null), V = i(null), H = i(!1), [U, W] = a(""), [le, G] = a(""), { controlId: K, labelId: q, descriptionId: ue, errorId: de, describedBy: fe } = n({
		id: se,
		hasLabel: _ != null,
		hasDescription: !!v,
		hasError: !!C
	}), J = T === void 0 ? Infinity : Math.max(0, Math.floor(T)), Y = x || S, X = l(fe, L), pe = l(q, R);
	r(m, () => {
		let e = B.current;
		if (!e) throw Error("TagInput: внутренний input недоступен при создании imperative handle.");
		return e;
	}, []);
	let Z = (e) => {
		if (Y) return;
		w?.();
		let t = [...h], n = new Set(t.map((e) => D(e))), r = [], i = 0, a = !1;
		for (let o of e) {
			let e = E(o);
			if (!e) continue;
			if (j !== void 0 && e.length > j) {
				G(`Тег «${e}» длиннее допустимого значения.`);
				continue;
			}
			let s = D(e);
			if (n.has(s)) {
				i += 1;
				continue;
			}
			if (t.length >= J) {
				a = !0;
				break;
			}
			n.add(s), t.push(e), r.push(e);
		}
		r.length > 0 ? (g(t), G(r.length === 1 ? `Добавлен тег «${r[0]}».` : `Добавлено тегов: ${r.length}.`)) : a ? G(`Достигнуто максимальное количество тегов: ${J}.`) : i > 0 && G("Такой тег уже добавлен.");
	}, Q = () => U ? (Z(U.split(p)), W(""), !0) : !1, $ = (e) => {
		if (Y) return;
		let t = h[e];
		t !== void 0 && (g(h.filter((t, n) => n !== e)), w?.(), G(`Удалён тег «${t}».`), B.current?.focus());
	}, me = (e) => {
		if (M?.(e), !(e.defaultPrevented || e.nativeEvent.isComposing || H.current || Y)) {
			if (e.key === "Enter" || e.key === ",") {
				(Q() || e.key === ",") && e.preventDefault();
				return;
			}
			e.key === "Backspace" && U.length === 0 && h.length > 0 && (e.preventDefault(), $(h.length - 1));
		}
	}, he = (e) => {
		if (N?.(e), e.defaultPrevented || Y) return;
		let t = e.clipboardData.getData("text");
		if (!f.test(t)) return;
		e.preventDefault();
		let n = e.currentTarget.selectionStart ?? U.length, r = e.currentTarget.selectionEnd ?? n, i = `${U.slice(0, n)}${t}${U.slice(r)}`;
		Z(i.split(p)), W("");
	};
	return /* @__PURE__ */ c(t, {
		label: _,
		description: v,
		disabled: x,
		className: oe,
		size: b,
		error: C,
		controlId: K,
		labelId: q,
		descriptionId: ue,
		errorId: de,
		children: [/* @__PURE__ */ c("div", {
			ref: V,
			className: o(e.uiControl, C && e.invalid, d.control),
			"data-disabled": x ? "" : void 0,
			"data-readonly": S ? "" : void 0,
			"data-invalid": C ? "" : void 0,
			onClick: () => B.current?.focus(),
			children: [
				h.length > 0 && /* @__PURE__ */ s("ul", {
					className: d.tagList,
					"aria-label": ie,
					children: h.map((e, t) => /* @__PURE__ */ c("li", {
						className: d.tag,
						children: [/* @__PURE__ */ s("span", {
							className: d.tagText,
							children: e
						}), !Y && /* @__PURE__ */ s("button", {
							type: "button",
							className: d.removeButton,
							onClick: (e) => {
								e.stopPropagation(), $(t);
							},
							"aria-label": re(e, t),
							children: /* @__PURE__ */ s(ee, { "aria-hidden": "true" })
						})]
					}, `${D(e)}-${t}`))
				}),
				/* @__PURE__ */ s("input", {
					...z,
					ref: B,
					id: K,
					form: A,
					disabled: x,
					readOnly: S,
					required: ce && h.length === 0,
					maxLength: j,
					value: U,
					placeholder: h.length === 0 ? y : void 0,
					"aria-invalid": !!C || void 0,
					"aria-labelledby": pe,
					"aria-describedby": X,
					className: o(d.input, ae),
					onChange: (e) => {
						let t = e.target.value, n = f.test(t);
						if (H.current) W(t);
						else if (n) {
							let e = t.split(p), n = f.test(t.at(-1) ?? "") ? "" : e.pop() ?? "";
							Z(e), W(n);
						} else W(t);
						(H.current || !n) && w?.();
					},
					onKeyDown: me,
					onPaste: he,
					onCompositionStart: (e) => {
						H.current = !0, F?.(e);
					},
					onCompositionEnd: (e) => {
						H.current = !1, I?.(e);
					},
					onBlur: (e) => {
						P?.(e);
						let t = e.relatedTarget, n = t instanceof Node && V.current?.contains(t);
						!e.defaultPrevented && !n && O && !Y && Q();
					}
				}),
				k && h.map((e, t) => /* @__PURE__ */ s("input", {
					type: "hidden",
					name: k,
					value: e,
					form: A,
					disabled: x
				}, `${D(e)}-${t}`))
			]
		}), /* @__PURE__ */ s("span", {
			className: "visuallyHidden",
			"aria-live": "polite",
			"aria-atomic": "true",
			children: le
		})]
	});
}
//#endregion
export { m as TagInput };

//# sourceMappingURL=index.js.map