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

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