import { Text as e } from "../text/index.js";
import { t } from "./ui.module-BisoTevy.js";
import { t as n } from "./check-box-Dd3YFuxd.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-e6IsrMUG.js";
import { t as ee } from "./separator-B72Aq-6a.js";
import { n as m, r as h, t as g } from "./useODataSelectBase-CKe1AVMI.js";
import { forwardRef as _, useCallback as te, useMemo as v, useRef as ne, useState as y } from "react";
import { flattenODataDependentServices as b, useODataCollectionModel as re } from "@ryuzaki13/react-foundation-api/odata";
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: n, highlight: r }) {
	return /* @__PURE__ */ x(h, {
		label: e[t],
		code: n && e[n],
		highlight: r
	});
}
var C = {
	multiSelect: "_9oSdQi",
	multiSelectTokens: "iobHig",
	multiSelectToken: "yatXso"
};
//#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} элементов`;
	let r = T(e, t, n);
	return e[0]?.[r] ?? e[0]?.[t];
}
function ce(e) {
	return function(t) {
		return /* @__PURE__ */ x(w, { value: E(t.selectedItems, e.codeKey, e.textKey) });
	};
}
function le(e) {
	return /* @__PURE__ */ x(a, {
		onSelectAll: e.selectAll,
		onDeselectAll: e.deselectAll
	});
}
function ue(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: n, isError: r }) {
	return /* @__PURE__ */ S("div", {
		className: t.uiPopupOption,
		children: [/* @__PURE__ */ x("span", { className: t.uiPopupOptionIcon }), /* @__PURE__ */ x(e, {
			color: r ? "error" : "primary",
			children: n
		})]
	});
}
//#endregion
//#region src/multi-select/ui/MultiSelect.tsx
function de({ isNoData: e, isLoading: t, error: n, children: r }) {
	return e ? /* @__PURE__ */ x(p, { emptyState: /* @__PURE__ */ x(D, { text: "Нет данных" }) }) : t ? /* @__PURE__ */ x(p, { loadingState: /* @__PURE__ */ x(D, { text: "Загрузка..." }) }) : n ? /* @__PURE__ */ x(p, { errorState: /* @__PURE__ */ x(D, {
		isError: !0,
		text: "Ошибка загрузки"
	}) }) : /* @__PURE__ */ x(oe, { children: r });
}
var fe = [];
function O(e, t) {
	return e[t];
}
function pe(e, t, n) {
	return Array.from(/* @__PURE__ */ new Set([
		e[n],
		e[t],
		...Object.values(e)
	]));
}
function me(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 he({ entries: e, 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 e.map(({ item: e, index: m }) => {
		let h = m === i, g = a.has(O(e, o)), _ = l?.(e) ?? !1;
		return /* @__PURE__ */ S("div", {
			id: u(r, m),
			ref: (e) => d(m, e),
			role: "option",
			"aria-selected": g,
			"aria-disabled": _ || void 0,
			className: ie(t.uiSelectOption, _ && t.disabled, g && t.selected, h && t.uiPopupOptionActive),
			onMouseDown: (e) => {
				e.preventDefault();
			},
			children: [/* @__PURE__ */ x("div", {
				onClick: (e) => {
					e.stopPropagation();
				},
				children: /* @__PURE__ */ x(n, {
					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, o)}-${m}`);
	});
}
var k = _(({ codeKey: e, textKey: n, hideCode: a, items: p, value: m, onChange: h, label: g, description: _, placeholder: b, size: re, query: ie, defaultQuery: se, highlightQuery: w, onQuery: E, defaultFilter: D = !0, onOpen: k, onClose: ge, getOptionDisabled: A, disabled: j, error: M, isLoading: N, renderToken: P, renderToolbar: F, renderItem: _e }, I) => {
	let ve = ne(null), [L, ye] = y(!1), R = "search-multi", z = m ?? fe, be = v(() => new Set(z.map((t) => O(t, e))), [e, z]), xe = v(() => p.filter((t) => !be.has(O(t, e))), [
		e,
		be,
		p
	]), B = v(() => T([...z, ...p], e, n), [
		e,
		z,
		p,
		n
	]), { query: V, setQuery: Se } = d({
		open: L,
		query: ie,
		defaultQuery: se,
		onQuery: E,
		triggerMode: R
	}), H = r({
		options: z,
		query: V,
		enabled: D,
		getSearchText: (t) => pe(t, e, B)
	}), U = r({
		options: xe,
		query: V,
		enabled: D,
		getSearchText: (t) => pe(t, e, B)
	}), W = v(() => [...H, ...U], [U, H]), Ce = H.length > 0 ? 0 : -1, { draftValue: G, setDraftValue: K, prepareOpen: we } = l({
		value: z,
		open: L,
		onCommit: h,
		onOpen: k,
		onClose: ge,
		isEqual: (t, n) => me(t, n, e)
	}), q = v(() => new Set(G.map((t) => O(t, e))), [e, G]), Te = v(() => ({
		selectedItems: G,
		committedSelectedItems: z,
		selectedKeys: q,
		open: L
	}), [
		z,
		G,
		L,
		q
	]), J = te((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 } = s({
		options: W,
		selectedIndex: Ce,
		open: L,
		onOpenChange: ye,
		onSelect: void 0,
		disabled: j,
		getOptionDisabled: J,
		closeOnSelect: !1,
		allowOpenWithoutOptions: !0,
		triggerMode: R
	}), Be = w ?? 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 || h([]);
	}, 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 ? ce({
		codeKey: e,
		textKey: B
	}) : P, Ye = F === void 0 ? le : F, Xe = _e ?? ue({
		codeKey: e,
		textKey: B,
		hideCode: a
	}), 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, $ = f({
		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: g,
		description: _,
		disabled: j,
		size: re,
		className: C.multiSelect,
		children: ({ controlId: n, labelId: r, describedBy: a }) => {
			let s = `${n}-listbox`;
			return /* @__PURE__ */ S(oe, { children: [/* @__PURE__ */ x(i, {
				ref: Ve,
				rootRef: ke,
				id: n,
				type: "text",
				role: "combobox",
				autoComplete: "off",
				disabled: j,
				value: V,
				placeholder: Ke ? void 0 : `${b} <${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 ? ze(s) : void 0,
				rootClassName: "flex alignItemsCenter",
				endAdornment: /* @__PURE__ */ S(c, {
					open: L,
					disabled: j,
					onToggleMouseDown: $.handleToggleMouseDown,
					onToggleClick: $.handleToggleClick,
					children: [Ze, /* @__PURE__ */ x("button", {
						type: "button",
						disabled: !Ke,
						className: t.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(o, {
				open: L,
				context: Ee,
				floatingStyles: De,
				listId: s,
				labelId: r,
				descriptionId: a,
				activeOptionId: L ? ze(s) : void 0,
				setFloating: Ae,
				getFloatingProps: Oe,
				onKeyDown: Le,
				initialFocus: -1,
				returnFocus: !1,
				tabIndex: -1,
				className: t.uiPopupOptions,
				header: Qe,
				children: /* @__PURE__ */ x(de, {
					isNoData: et,
					isLoading: N,
					error: M,
					children: /* @__PURE__ */ S("div", {
						className: "scrollable h100",
						children: [
							/* @__PURE__ */ x(he, {
								entries: Q,
								listId: s,
								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(ee, { className: "marginBlockSm" }),
							/* @__PURE__ */ x(he, {
								entries: $e,
								listId: s,
								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 ge({ 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 A = ({ odata: e, segment: t, model: n, label: r, description: i, disabled: a, value: o, dependencies: s, onChange: c, size: l }) => {
	let u = ge({
		odata: e,
		segment: t,
		model: re(n),
		value: o,
		dependencies: s,
		onChange: c
	});
	return /* @__PURE__ */ x(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,
		disabled: a || u.isLoading,
		placeholder: u.placeholder,
		error: u.isError ? "Ошибка загрузки" : void 0
	});
}, j = ({ 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__ */ x("div", {
		style: {
			maxWidth: s,
			minWidth: s
		},
		children: /* @__PURE__ */ x(A, {
			odata: e.odata,
			segment: e.segment,
			model: e.model,
			dependencies: o,
			value: o[e.id],
			onChange: c
		})
	});
}, M = ({ width: e = 15, odata: t, segments: n, model: r, values: i, onChange: a }) => {
	let [o, s] = y({}), c = i ?? o;
	return b([{
		odata: t,
		segments: n,
		model: r
	}]).map((t) => /* @__PURE__ */ x(j, {
		item: t,
		values: c,
		onChange: (e) => {
			i || s(e), a?.(e);
		},
		width: e
	}, t.id));
};
//#endregion
//#region src/multi-select/ui/SerializableMultiSelect.tsx
function N(e, t) {
	return e[t];
}
function P(e, t, n) {
	let r = e[t];
	if (typeof r == "string") return r;
	let i = N(e, n);
	return typeof i == "string" ? i : String(i);
}
function F({ 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(N(e, c)),
		text: P(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,
		value: p,
		onChange: (e) => s(e.map((e) => e.key))
	});
}
//#endregion
export { k as a, A as i, M as n, w as o, j as r, F as t };

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