import { cn as e } from "../../../lib/utils.js";
import { Badge as t } from "../atoms/badge.js";
import { Fragment as n, useId as r, useRef as i, useState as a } from "react";
import { jsx as o, jsxs as s } from "react/jsx-runtime";
import { X as c } from "lucide-react";
//#region lib/components/ui/molecules/tags-input.tsx
var l = /[,\n\r]+/;
function u({ value: e, onRemove: n }) {
	return /* @__PURE__ */ s(t, {
		className: "gap-1 pr-1",
		variant: "secondary",
		children: [/* @__PURE__ */ o("span", {
			className: "max-w-[220px] truncate",
			children: e
		}), /* @__PURE__ */ o("button", {
			"aria-label": `Remove ${e}`,
			className: "ml-0.5 rounded text-muted-foreground hover:text-foreground",
			onClick: (e) => {
				e.stopPropagation(), n();
			},
			type: "button",
			children: /* @__PURE__ */ o(c, { className: "size-3" })
		})]
	});
}
function d(e, t) {
	return /* @__PURE__ */ o(u, {
		onRemove: t,
		value: e
	});
}
function f({ values: t, onChange: c, placeholder: u, addOnBlur: f = !0, validateValue: p, isDuplicate: m, renderValue: h, className: g, disabled: _ = !1, id: v, ...y }) {
	let b = r(), x = v ?? b, [S, C] = a(""), [w, T] = a(null), E = i(null), D = i(t);
	D.current = t;
	let O = h ?? d, k = (e, t) => {
		for (let n of t) {
			let t = m ? m(e, n) : e === n;
			if (t === !0) return "Already added";
			if (typeof t == "string" && t) return t;
		}
		return null;
	}, A = (e) => {
		let t = e.trim();
		if (!t) return;
		let n = D.current, r = p ? p(t, n) : {};
		if (r.error) {
			T(r.error);
			return;
		}
		let i = r.normalizedValue ?? t, a = k(i, n);
		if (a) {
			T(a);
			return;
		}
		c([...n, i]), C(""), T(null);
	}, j = (e) => {
		c(D.current.filter((t) => t !== e));
	}, M = (e) => {
		if ((e.key === "Enter" || e.key === ",") && !e.nativeEvent.isComposing) e.preventDefault(), A(S);
		else if (e.key === "Backspace" && S === "") {
			let e = D.current.at(-1);
			e !== void 0 && j(e);
		} else T(null);
	}, N = (e) => {
		let t = e.clipboardData.getData("text");
		if (!l.test(t)) return;
		e.preventDefault();
		let n = t.split(l).map((e) => e.trim()).filter(Boolean), r = [...D.current], i = null;
		for (let e of n) {
			let t = p ? p(e, r) : {};
			if (t.error) {
				i = t.error;
				break;
			}
			let n = t.normalizedValue ?? e, a = k(n, r);
			if (a) {
				i = a;
				break;
			}
			r = [...r, n];
		}
		r.length > D.current.length && c(r), C(""), T(i);
	};
	return /* @__PURE__ */ s("div", {
		className: "space-y-1",
		children: [/* @__PURE__ */ s("label", {
			className: e("flex min-h-10 w-full cursor-text flex-wrap items-center gap-1.5 rounded-md border border-input bg-transparent px-3 py-2 text-sm transition-[color,box-shadow] focus-within:border-ring focus-within:ring-[3px] focus-within:ring-ring/50", _ && "cursor-not-allowed opacity-50", g),
			htmlFor: x,
			ref: E,
			children: [t.map((e) => /* @__PURE__ */ o(n, { children: O(e, () => j(e)) }, e)), /* @__PURE__ */ o("input", {
				...y,
				className: "min-w-32 flex-1 bg-transparent outline-none placeholder:text-muted-foreground",
				disabled: _,
				id: x,
				onBlur: (e) => {
					let t = E.current?.contains(e.relatedTarget);
					f && S.trim() && !t && A(S);
				},
				onChange: (e) => C(e.target.value),
				onKeyDown: M,
				onPaste: N,
				placeholder: t.length === 0 ? u : "",
				type: "text",
				value: S
			})]
		}), w && /* @__PURE__ */ o("p", {
			className: "text-destructive text-sm",
			children: w
		})]
	});
}
//#endregion
export { f as TagsInput, u as TagsInputValue };
