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

//# sourceMappingURL=index.js.map