import { t as e } from "./ui.module-DHVTOqZ0.js";
import { Text as t } from "../text/index.js";
import { t as n } from "./check-box-sfGjYczQ.js";
import { a as r, c as i, d as a, f as o, i as s, l as c, n as l, p as u, r as d, t as f, u as p } from "./picker-CrRdwdA5.js";
import { t as ee } from "./separator-B72Aq-6a.js";
import { n as m, r as h, t as g } from "./useODataSelectBase-Bq36Wp1U.js";
import { forwardRef as _, useCallback as te, useMemo as v, useRef as ne, useState as y } from "react";
import { flattenODataDependentServices as re, useODataCollectionModel as ie } from "@ryuzaki13/react-foundation-api/odata";
import { cn as ae } from "@ryuzaki13/react-foundation-lib/utils";
import { Fragment as oe, jsx as b, jsxs as x } from "react/jsx-runtime";
import { XIcon as se } from "lucide-react";
import { formatRussianPlural as ce } from "@ryuzaki13/react-foundation-lib/formatters";
//#region src/multi-select/ui/MultiSelectOption.tsx
function le({ item: e, textKey: t, codeKey: n, highlight: r }) {
	return /* @__PURE__ */ b(h, {
		label: e[t],
		code: n && e[n],
		highlight: r
	});
}
var S = {
	multiSelect: "_9oSdQi",
	multiSelectTokens: "iobHig",
	multiSelectToken: "yatXso"
};
//#endregion
//#region src/multi-select/ui/MultiSelectToken.tsx
function C({ value: e }) {
	return e ? /* @__PURE__ */ b("div", {
		className: S.multiSelectTokens,
		children: /* @__PURE__ */ b("div", {
			className: S.multiSelectToken,
			children: e
		})
	}) : null;
}
//#endregion
//#region src/multi-select/ui/defaultRenderers.tsx
var w = {
	one: "элемент",
	few: "элемента",
	many: "элементов"
};
function T(e) {
	return ce(e, w);
}
function E(e, t, n) {
	if (n) return n;
	let r = e[0];
	return r ? Object.keys(r).find((e) => e !== t) ?? t : t;
}
function ue(e, t, n) {
	if (e.length === 0) return;
	if (e.length > 1) return T(e.length);
	let r = E(e, t, n);
	return e[0]?.[r] ?? e[0]?.[t];
}
function de(e) {
	return function(t) {
		return /* @__PURE__ */ b(C, { value: ue(t.selectedItems, e.codeKey, e.textKey) });
	};
}
function fe(e) {
	return /* @__PURE__ */ b(a, {
		onSelectAll: e.selectAll,
		onDeselectAll: e.deselectAll
	});
}
function pe(e) {
	return function(t, n) {
		return /* @__PURE__ */ b(le, {
			item: t,
			selected: n.selected,
			highlight: n.highlightQuery,
			textKey: E([t], e.codeKey, e.textKey),
			codeKey: e.hideCode ? void 0 : e.codeKey
		});
	};
}
//#endregion
//#region src/multi-select/ui/MultiSelectOptionSkeleton.tsx
function D({ text: n, isError: r }) {
	return /* @__PURE__ */ x("div", {
		className: e.uiPopupOption,
		children: [/* @__PURE__ */ b("span", { className: e.uiPopupOptionIcon }), /* @__PURE__ */ b(t, {
			color: r ? "error" : "primary",
			children: n
		})]
	});
}
//#endregion
//#region src/multi-select/ui/MultiSelect.tsx
function me({ isNoData: e, error: t, children: n }) {
	return e ? /* @__PURE__ */ b(p, { emptyState: /* @__PURE__ */ b(D, { text: "Нет данных" }) }) : t ? /* @__PURE__ */ b(p, { errorState: /* @__PURE__ */ b(D, {
		isError: !0,
		text: "Ошибка загрузки"
	}) }) : /* @__PURE__ */ b(oe, { children: n });
}
var he = [];
function O(e, t) {
	return e[t];
}
function ge(e, t, n) {
	return Array.from(/* @__PURE__ */ new Set([
		e[n],
		e[t],
		...Object.values(e)
	]));
}
function _e(e, t, n) {
	if (e.length !== t.length) return !1;
	let r = new Set(t.map((e) => O(e, n)));
	return e.every((e) => r.has(O(e, n)));
}
function ve({ entries: t, listId: r, activeIndex: i, selectedKeys: a, codeKey: o, query: s, highlightQuery: c, getOptionDisabled: l, getOptionId: u, setOptionRef: d, toggleOption: f, selectOnlyOption: p, renderItem: ee }) {
	return t.map(({ item: t, index: m }) => {
		let h = m === i, g = a.has(O(t, o)), _ = l?.(t) ?? !1;
		return /* @__PURE__ */ x("div", {
			id: u(r, m),
			ref: (e) => d(m, e),
			role: "option",
			"aria-selected": g,
			"aria-disabled": _ || void 0,
			className: ae(e.uiSelectOption, _ && e.disabled, g && e.selected, h && e.uiPopupOptionActive),
			onMouseDown: (e) => {
				e.preventDefault();
			},
			children: [/* @__PURE__ */ b("div", {
				onClick: (e) => {
					e.stopPropagation();
				},
				children: /* @__PURE__ */ b(n, {
					value: g,
					disabled: _,
					onChange: () => {
						_ || f(t);
					},
					onClick: (e) => {
						e.stopPropagation();
					},
					onMouseDown: (e) => {
						e.stopPropagation();
					}
				})
			}), /* @__PURE__ */ b("div", {
				className: "overflowHidden",
				onClick: () => {
					_ || p(t);
				},
				children: ee(t, {
					selected: g,
					active: h,
					disabled: _,
					query: s,
					highlightQuery: c
				})
			})]
		}, `${O(t, o)}-${m}`);
	});
}
var k = _(({ codeKey: t, textKey: n, hideCode: a, items: p, value: m, onChange: h, label: g, description: _, placeholder: re, size: ie, query: ae, defaultQuery: ce, highlightQuery: le, onQuery: C, defaultFilter: w = !0, onOpen: T, onClose: ue, getOptionDisabled: D, disabled: k, error: A, isLoading: j, renderToken: M, renderToolbar: N, renderItem: P }, F) => {
	let I = ne(null), [L, ye] = y(!1), R = "search-multi", z = m ?? he, be = v(() => new Set(z.map((e) => O(e, t))), [t, z]), xe = v(() => p.filter((e) => !be.has(O(e, t))), [
		t,
		be,
		p
	]), B = v(() => E([...z, ...p], t, n), [
		t,
		z,
		p,
		n
	]), { query: V, setQuery: Se, resetQueryOnClose: Ce } = d({
		query: ae,
		defaultQuery: ce,
		onQuery: C,
		triggerMode: R
	}), we = (e) => {
		L && !e && Ce && Se(""), ye(e);
	}, H = r({
		options: z,
		query: V,
		enabled: w,
		getSearchText: (e) => ge(e, t, B)
	}), U = r({
		options: xe,
		query: V,
		enabled: w,
		getSearchText: (e) => ge(e, t, B)
	}), W = v(() => [...H, ...U], [U, H]), Te = H.length > 0 ? 0 : -1, { draftValue: G, setDraftValue: K, prepareOpen: Ee } = l({
		value: z,
		open: L,
		onCommit: h,
		onOpen: T,
		onClose: ue,
		isEqual: (e, n) => _e(e, n, t)
	}), q = v(() => new Set(G.map((e) => O(e, t))), [t, G]), De = v(() => ({
		selectedItems: G,
		committedSelectedItems: z,
		selectedKeys: q,
		open: L
	}), [
		z,
		G,
		L,
		q
	]), J = te((e) => D?.(e, De) ?? !1, [D, De]), { activeIndex: Y, context: Oe, floatingStyles: ke, getFloatingProps: Ae, setReference: je, setFloating: Me, setOptionRef: Ne, close: Pe, openList: Fe, toggleOpen: Ie, selectActiveOption: Le, handleReferenceKeyDown: Re, handleFloatingKeyDown: ze, getOptionId: Be, getActiveOptionId: Ve } = s({
		options: W,
		selectedIndex: Te,
		open: L,
		onOpenChange: we,
		onSelect: void 0,
		disabled: k,
		getOptionDisabled: J,
		closeOnSelect: !1,
		allowOpenWithoutOptions: !0,
		triggerMode: R
	}), He = le ?? V, X = (e) => {
		let n = O(e, t);
		K((r) => r.some((e) => O(e, t) === n) ? r.filter((e) => O(e, t) !== n) : J(e) ? r : [...r, e]);
	}, Ue = (e) => {
		if (I.current = e, typeof F == "function") {
			F(e);
			return;
		}
		F && (F.current = e);
	}, We = () => {
		K([]), L || h([]);
	}, Ge = () => {
		K((e) => {
			let n = [...e], r = new Set(e.map((e) => O(e, t)));
			for (let e of W) {
				let i = O(e, t);
				!r.has(i) && !J(e) && (r.add(i), n.push(e));
			}
			return n;
		});
	}, Ke = () => {
		K([]);
	}, qe = (e) => {
		J(e) || (K([e]), Pe());
	}, Je = L ? G : z, Z = Je.length > 0, Ye = {
		selectedItems: Je,
		committedSelectedItems: z,
		availableItems: U,
		query: V,
		open: L,
		clearSelection: We,
		selectAll: Ge,
		deselectAll: Ke
	}, Xe = M === void 0 ? de({
		codeKey: t,
		textKey: B
	}) : M, Ze = N === void 0 ? fe : N, Qe = P ?? pe({
		codeKey: t,
		textKey: B,
		hideCode: a
	}), $e = typeof Xe == "function" ? Xe(Ye) : null, et = typeof Ze == "function" ? Ze(Ye) : null, Q = H.map((e, t) => ({
		item: e,
		index: t
	})), tt = U.map((e, t) => ({
		item: e,
		index: t + Q.length
	})), nt = !j && !A && W.length === 0, $ = f({
		mode: R,
		open: L,
		currentQuery: V,
		hasDisplayValue: !1,
		inputRef: I,
		setQuery: Se,
		openList: Fe,
		close: Pe,
		toggleOpen: Ie,
		onBeforeOpen: () => {
			L || Ee();
		}
	});
	return /* @__PURE__ */ b(u, {
		label: g,
		description: _,
		disabled: k,
		size: ie,
		className: S.multiSelect,
		children: ({ controlId: n, labelId: r, describedBy: a }) => {
			let s = `${n}-listbox`;
			return /* @__PURE__ */ x(oe, { children: [/* @__PURE__ */ b(i, {
				ref: Ue,
				rootRef: je,
				id: n,
				type: "text",
				role: "combobox",
				autoComplete: "off",
				isLoading: j,
				disabled: k,
				value: V,
				placeholder: Z ? void 0 : `${re ?? g ?? "Выберите значения"} <${p.length}>`,
				"aria-labelledby": r,
				"aria-describedby": a,
				"aria-haspopup": "listbox",
				"aria-expanded": L,
				"aria-controls": L ? s : void 0,
				"aria-autocomplete": "list",
				"aria-activedescendant": L ? Ve(s) : void 0,
				rootClassName: "flex alignItemsCenter",
				endAdornment: /* @__PURE__ */ b(c, {
					open: L,
					disabled: k,
					onToggleMouseDown: $.handleToggleMouseDown,
					onToggleClick: $.handleToggleClick,
					children: /* @__PURE__ */ b("button", {
						type: "button",
						disabled: !Z,
						className: e.uiClearButton,
						"data-ui": "multi-select-clear-button",
						"data-action": "clear-multi-select",
						onClick: We,
						"aria-label": "Очистить все",
						children: /* @__PURE__ */ b(se, {})
					})
				}),
				overlay: $e,
				onChange: (e) => {
					$.handleTriggerInputChange(e.target.value);
				},
				onClick: $.handleTriggerClick,
				onFocus: (e) => {
					$.handleTriggerFocus(e.currentTarget);
				},
				onKeyDown: (e) => {
					Re(e), !e.defaultPrevented && $.handleTriggerKeyDown({
						event: e,
						onActivateWhenOpen: () => {
							if (Y >= 0) {
								let e = W[Y];
								if (e) {
									X(e);
									return;
								}
							}
							Le();
						},
						enableClosedArrowDownOpen: !0,
						suppressClosedArrowUp: !0
					});
				}
			}), /* @__PURE__ */ b(o, {
				open: L,
				context: Oe,
				floatingStyles: ke,
				listId: s,
				labelId: r,
				descriptionId: a,
				activeOptionId: L ? Ve(s) : void 0,
				setFloating: Me,
				getFloatingProps: Ae,
				onKeyDown: ze,
				initialFocus: -1,
				returnFocus: !1,
				tabIndex: -1,
				className: e.uiPopupOptions,
				header: et,
				children: /* @__PURE__ */ b(me, {
					isNoData: nt,
					error: A,
					children: /* @__PURE__ */ x("div", {
						className: "scrollable h100",
						children: [
							/* @__PURE__ */ b(ve, {
								entries: Q,
								listId: s,
								activeIndex: Y,
								selectedKeys: q,
								codeKey: t,
								query: V,
								highlightQuery: He,
								getOptionDisabled: J,
								getOptionId: Be,
								setOptionRef: Ne,
								toggleOption: X,
								selectOnlyOption: qe,
								renderItem: Qe
							}),
							Q.length > 0 && xe.length > 0 && /* @__PURE__ */ b(ee, { className: "marginBlockSm" }),
							/* @__PURE__ */ b(ve, {
								entries: tt,
								listId: s,
								activeIndex: Y,
								selectedKeys: q,
								codeKey: t,
								query: V,
								highlightQuery: He,
								getOptionDisabled: J,
								getOptionId: Be,
								setOptionRef: Ne,
								toggleOption: X,
								selectOnlyOption: qe,
								renderItem: Qe
							})
						]
					})
				})
			})] });
		}
	});
});
k.displayName = "MultiSelect";
//#endregion
//#region src/multi-select/model/useODataMultiSelect.ts
function A({ odata: e, segment: t, model: n, value: r, dependencies: i, onChange: a }) {
	let o = m({
		odata: e,
		segment: t,
		model: n,
		value: r,
		dependencies: i
	}), s = (e) => {
		a(g(e, o.selectionKey));
	};
	return {
		...o,
		handleChange: s
	};
}
//#endregion
//#region src/multi-select/ui/ODataMultiSelect.tsx
var j = ({ odata: e, segment: t, model: n, label: r, description: i, disabled: a, value: o, dependencies: s, onChange: c, size: l }) => {
	let u = A({
		odata: e,
		segment: t,
		model: ie(n),
		value: o,
		dependencies: s,
		onChange: c
	});
	return /* @__PURE__ */ b(k, {
		label: r,
		description: i,
		size: l,
		codeKey: u.codeKey,
		textKey: u.textKey,
		hideCode: t.hideCode ?? e.hideCode,
		items: u.filteredItems,
		value: u.selectedItems,
		onChange: u.handleChange,
		onQuery: u.setQuery,
		query: u.query,
		defaultFilter: !1,
		highlightQuery: u.debouncedQuery,
		isLoading: u.isLoading,
		disabled: a,
		placeholder: u.placeholder,
		error: u.isError ? "Ошибка загрузки" : void 0
	});
}, M = ({ item: e, values: t, onChange: n, width: r = 15 }) => {
	let [i, a] = y({}), o = t ?? i, s = `calc(${r}em + var(--width-add))`, c = te((r) => {
		let i = {
			...o,
			[e.id]: r
		};
		t || a(i), n?.(i);
	}, [
		e.id,
		n,
		o,
		t
	]);
	return /* @__PURE__ */ b("div", {
		style: {
			maxWidth: s,
			minWidth: s
		},
		children: /* @__PURE__ */ b(j, {
			odata: e.odata,
			segment: e.segment,
			model: e.model,
			dependencies: o,
			value: o[e.id],
			onChange: c
		})
	});
}, N = ({ width: e = 15, odata: t, segments: n, model: r, values: i, onChange: a }) => {
	let [o, s] = y({}), c = i ?? o;
	return re([{
		odata: t,
		segments: n,
		model: r
	}]).map((t) => /* @__PURE__ */ b(M, {
		item: t,
		values: c,
		onChange: (e) => {
			i || s(e), a?.(e);
		},
		width: e
	}, t.id));
};
//#endregion
//#region src/multi-select/ui/SerializableMultiSelect.tsx
function P(e, t) {
	return e[t];
}
function F(e, t, n) {
	let r = e[t];
	if (typeof r == "string") return r;
	let i = P(e, n);
	return typeof i == "string" ? i : String(i);
}
function I({ label: e, description: t, disabled: n, placeholder: r, size: i, options: a, value: o, onChange: s, optionKey: c, optionLabel: l, renderCode: u }) {
	let d = a.map((e) => ({
		key: String(P(e, c)),
		text: F(e, l, c)
	})), f = new Set((o ?? []).map((e) => String(e))), p = d.filter((e) => f.has(e.key));
	return /* @__PURE__ */ b(k, {
		label: e,
		description: t,
		disabled: n,
		placeholder: r,
		size: i,
		items: d,
		codeKey: "key",
		textKey: "text",
		hideCode: !u || void 0,
		value: p,
		onChange: (e) => s(e.map((e) => e.key))
	});
}
//#endregion
export { k as a, j as i, N as n, T as o, M as r, C as s, I as t };

//# sourceMappingURL=multi-select-Bs1GU5MD.js.map