import { t as e } from "../chunks/ui.module-DHVTOqZ0.js";
import { s as t } from "../chunks/input-CCdBtl-C.js";
import { t as n } from "../chunks/check-box-sfGjYczQ.js";
import { c as r, d as i, f as a, i as o, l as s, n as c, p as l, r as u, t as d, u as f } from "../chunks/picker-CrRdwdA5.js";
import { r as p } from "../chunks/useODataSelectBase-Bq36Wp1U.js";
import { o as m, s as h } from "../chunks/multi-select-Bs1GU5MD.js";
import { useCallback as g, useLayoutEffect as _, useMemo as v, useRef as y, useState as ee } from "react";
import { flattenODataDependentServices as b, sortODataDependentSegmentItemsByChains as x, useODataCollection as S, useODataCollectionChains as C, useODataCollectionModel as w } from "@ryuzaki13/react-foundation-api/odata";
import { cn as T } from "@ryuzaki13/react-foundation-lib/utils";
import { Fragment as E, jsx as D, jsxs as O } from "react/jsx-runtime";
import { ChevronRightIcon as k, XIcon as te } from "lucide-react";
import { compareStrings as A } from "@ryuzaki13/react-foundation-lib/string-comparison";
//#region src/tree-select/helpers.ts
function ne(e) {
	return e ? { [e.codeKey]: [e.value] } : {};
}
//#endregion
//#region src/tree-select/model/buildODataTreeNodes.ts
function re(e, t, n) {
	return e ? `${e}/${t}:${n}` : `${t}:${n}`;
}
function ie(e, t, n) {
	let r = n[t];
	return r && e[r] || e[t];
}
function j(e) {
	return [...new Set(e.filter(Boolean))].join(" ");
}
function M(e) {
	return {
		id: re(e.parentId, e.codeKey, e.value),
		codeKey: e.codeKey,
		value: e.value,
		label: e.label,
		code: e.code,
		searchText: j([
			e.value,
			e.label,
			e.code
		]),
		sortCode: e.sortCode,
		children: [],
		childByBranchKey: /* @__PURE__ */ new Map()
	};
}
function N(e, t) {
	e.sort((e, n) => {
		let r = t ? e.sortCode : e.label, i = t ? n.sortCode : n.label;
		return A(r, i) || A(e.sortCode, n.sortCode);
	});
	for (let n of e) N(n.children, t);
}
function ae(e) {
	return {
		id: e.id,
		codeKey: e.codeKey,
		value: e.value,
		label: e.label,
		code: e.code,
		searchText: e.searchText,
		children: e.children.length > 0 ? e.children.map(ae) : void 0
	};
}
function oe({ items: e, orderedCodeKeys: t, keyPairsMap: n, hiddenCodeKeys: r, textValueCodeKeys: i, allowEmptyCodeKeys: a, sortByCode: o = !0 }) {
	if (t.length === 0 || e.length === 0) return [];
	let s = [], c = /* @__PURE__ */ new Map();
	for (let o of e) {
		let e;
		for (let l of t) {
			let t = String(o[l] ?? "");
			if (!t && !a?.has(l)) break;
			let u = String(ie(o, l, n) ?? ""), d = i?.has(l) ? u : t, f = `${l}:${d}`, p = e ? e.childByBranchKey : c, m = p.get(f);
			m || (m = M({
				parentId: e?.id,
				codeKey: l,
				value: d,
				label: u,
				code: r.has(l) ? void 0 : t,
				sortCode: t
			}), p.set(f, m), e ? e.children.push(m) : s.push(m)), e = m;
		}
	}
	return N(s, o), s.map(ae);
}
//#endregion
//#region src/tree-select/model/resolveOrderedTreeSegmentItems.ts
function se(e, t, n) {
	return x(e, t, n);
}
//#endregion
//#region src/tree-select/model/useODataTreeData.ts
function ce(e, t, n) {
	return b([{
		odata: e,
		segments: t,
		model: n
	}]);
}
function le(e) {
	return e[0]?.id;
}
function P({ odata: e, segments: t, model: n, segmentOrder: r, allowEmptyCodeKeys: i }) {
	let a = `${e.service}.${e.target}`, o = v(() => ce(e, t, n), [
		e,
		t,
		n
	]), s = C([e]), c = v(() => se(o, s, r), [
		o,
		s,
		r
	]), l = v(() => c.map((e) => e.id), [c]), u = v(() => new Set(i), [i]), d = le(c) ?? Object.keys(t)[0], f = w({
		codeKey: d,
		...n
	}), p = S({
		odata: e,
		model: f
	}), { hiddenCodeKeys: m, textValueCodeKeys: h } = v(() => {
		let t = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set();
		for (let r of c) (r.segment.hideCode ?? e.hideCode) && t.add(r.id), r.segment.selectText && n.add(r.id);
		return {
			hiddenCodeKeys: t,
			textValueCodeKeys: n
		};
	}, [e.hideCode, c]), g = v(() => oe({
		items: p.data?.items ?? [],
		orderedCodeKeys: l,
		keyPairsMap: p.data?.keyPairsMap ?? {},
		hiddenCodeKeys: m,
		textValueCodeKeys: h,
		allowEmptyCodeKeys: u,
		sortByCode: e.sortByCode
	}), [
		u,
		p.data?.items,
		p.data?.keyPairsMap,
		m,
		e.sortByCode,
		l,
		h
	]), _ = c[0]?.segment.placeholder ?? "Выберите значение", y = !!s[a]?.length;
	return {
		nodes: g,
		orderedSegmentItems: c,
		orderedCodeKeys: l,
		isLoading: p.isLoading,
		isError: p.isError,
		placeholder: _,
		hasResolvedChain: y
	};
}
//#endregion
//#region src/tree-select/model/treeUtils.ts
function F(e, t) {
	return `${e}::${t}`;
}
function ue(e) {
	let t = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), c = 0, l = (e, u) => {
		t.set(e.id, e), n.set(e.id, u), i.set(e.id, c++);
		let d = F(e.codeKey, e.value);
		a.set(d, e.id);
		let f = o.get(d);
		f ? f.push(e.id) : o.set(d, [e.id]);
		let p = (e.children ?? []).map((e) => e.id);
		r.set(e.id, p);
		for (let t of e.children ?? []) l(t, e.id);
		s.set(e.id, e.disabled === !0 || p.some((e) => s.get(e) === !0));
	};
	for (let t of e) l(t);
	return {
		rootIds: e.map((e) => e.id),
		nodeById: t,
		parentById: n,
		childrenById: r,
		preorderIndexById: i,
		nodeIdByCodeValue: a,
		nodeIdsByCodeValue: o,
		subtreeContainsDisabledById: s
	};
}
function I(e, t) {
	let n = t.nodeById.get(e);
	return n ? t.nodeIdsByCodeValue.get(F(n.codeKey, n.value)) ?? [] : [];
}
function de(e, t) {
	if (e) return t.nodeIdByCodeValue.get(F(e.codeKey, e.value));
}
function L(e, t) {
	let n = /* @__PURE__ */ new Set();
	if (!e) return n;
	for (let [r, i] of Object.entries(e)) for (let e of i) for (let i of t.nodeIdsByCodeValue.get(F(r, e)) ?? []) n.add(i);
	return W(n, t);
}
function R(e, t) {
	let n = [...e].sort((e, n) => (t.preorderIndexById.get(e) ?? 2 ** 53 - 1) - (t.preorderIndexById.get(n) ?? 2 ** 53 - 1)), r = {}, i = /* @__PURE__ */ new Set();
	for (let e of n) {
		let n = t.nodeById.get(e);
		if (!n) continue;
		let a = F(n.codeKey, n.value);
		i.has(a) || (r[n.codeKey] = r[n.codeKey] ?? [], r[n.codeKey].push(n.value), i.add(a));
	}
	return r;
}
function fe(e, t) {
	let n = [], r = t.parentById.get(e);
	for (; r;) n.push(r), r = t.parentById.get(r);
	return n;
}
function z(e, t, n) {
	let r = n.parentById.get(e);
	for (; r;) {
		if (r === t) return !0;
		r = n.parentById.get(r);
	}
	return !1;
}
function B(e, t) {
	let n = I(e, t);
	return !n.length || n.some((e) => {
		let n = t.nodeById.get(e);
		return !n || n.selectionBehavior === "descendants" || t.subtreeContainsDisabledById.get(e) === !0;
	}) ? !1 : !n.some((e, r) => n.some((n, i) => r !== i && (z(e, n, t) || z(n, e, t))));
}
function pe(e, t) {
	return B(e, t) ? I(e, t) : [e];
}
function V(e, t, n) {
	for (let r of [...e]) z(r, t, n) && e.delete(r);
}
function H(e, t, n) {
	let r = [t];
	for (let t = 0; t < r.length; t += 1) {
		let i = r[t];
		if (i) {
			r.push(...n.childrenById.get(i) ?? []);
			for (let t of I(i, n)) e.delete(t);
		}
	}
}
function U(e, t, n) {
	if (t.has(e)) return !0;
	if (n.nodeById.get(e)?.disabled) return !1;
	let r = n.childrenById.get(e) ?? [];
	return r.length !== 0 && r.every((e) => U(e, t, n));
}
function W(e, t) {
	let n = new Set(e);
	for (let e of [...n]) if (t.nodeById.get(e)?.selectionBehavior === "descendants") {
		n.delete(e);
		for (let r of J(t, t.childrenById.get(e) ?? [])) n.add(r);
	}
	for (let e of [...n]) fe(e, t).some((e) => n.has(e)) && n.delete(e);
	let r = [...t.nodeById.keys()].sort((e, n) => (t.preorderIndexById.get(n) ?? 0) - (t.preorderIndexById.get(e) ?? 0));
	for (let e of r) {
		let r = t.childrenById.get(e) ?? [];
		if (!(r.length === 0 || n.has(e) || t.nodeById.get(e)?.disabled || t.nodeById.get(e)?.selectionBehavior === "descendants") && r.every((e) => U(e, n, t))) {
			let r = I(e, t);
			if (B(e, t) && r.every((e) => {
				let r = t.nodeById.get(e);
				return r && !r.disabled && r.selectionBehavior !== "descendants" && U(e, n, t);
			})) for (let e of r) V(n, e, t), n.add(e);
		}
	}
	return n;
}
function G(e, t, n) {
	let r = n.parentById.get(t);
	for (; r;) {
		if (e.has(r)) {
			for (let t of I(r, n)) if (e.has(t)) {
				e.delete(t);
				for (let r of J(n, n.childrenById.get(t) ?? [])) e.add(r);
			}
			return !0;
		}
		r = n.parentById.get(r);
	}
	return !1;
}
function K(e, t, n) {
	return t.has(e) ? !0 : fe(e, n).some((e) => t.has(e));
}
function q(e, t, n, r) {
	let i = n.childrenById.get(e) ?? [];
	if (t.has(e)) return r.selectedIds.add(e), {
		selected: !0,
		partial: !1
	};
	if (i.length === 0) return {
		selected: !1,
		partial: !1
	};
	let a = i.map((e) => q(e, t, n, r)), o = a.every((e) => e.selected && !e.partial), s = a.some((e) => e.selected || e.partial);
	return o ? (r.selectedIds.add(e), {
		selected: !0,
		partial: !1
	}) : s ? (r.partialIds.add(e), {
		selected: !1,
		partial: !0
	}) : {
		selected: !1,
		partial: !1
	};
}
function me(e, t) {
	let n = {
		selectedIds: /* @__PURE__ */ new Set(),
		partialIds: /* @__PURE__ */ new Set()
	};
	for (let r of t.rootIds) q(r, e, t, n);
	return n;
}
function J(e, t = e.rootIds) {
	let n = (t) => {
		let r = e.nodeById.get(t), i = (e.childrenById.get(t) ?? []).map(n), a = i.flatMap((e) => e.selectedIds), o = r?.disabled === !0 || i.some((e) => e.containsDisabled);
		if (!r || r.disabled) return {
			containsDisabled: !0,
			selectedIds: a
		};
		if (r.selectionBehavior === "descendants") return {
			containsDisabled: o,
			selectedIds: a
		};
		let s = !B(t, e);
		return o || s ? {
			containsDisabled: !0,
			selectedIds: a
		} : {
			containsDisabled: !1,
			selectedIds: [t]
		};
	};
	return new Set(t.flatMap((e) => n(e).selectedIds));
}
function he(e, t, n) {
	let r = L(e, n);
	if (U(t, r, n)) return H(r, t, n), R(W(r, n), n);
	let i = (e) => {
		if (r.has(e)) {
			H(r, e, n);
			return;
		}
		if (U(e, r, n)) {
			H(r, e, n);
			return;
		}
		if (K(e, r, n)) {
			G(r, e, n) && (K(e, r, n) || U(e, r, n) ? i(e) : H(r, e, n));
			return;
		}
		for (let t of pe(e, n)) V(r, t, n), r.add(t);
	}, a = [...J(n, [t])], o = a.length === 0 && (K(t, r, n) || U(t, r, n)) ? [t] : a, s = o.length > 0 && o.every((e) => K(e, r, n) || U(e, r, n));
	for (let e of o) {
		let t = K(e, r, n) || U(e, r, n);
		(s && t || !s && !t) && i(e);
	}
	return R(W(r, n), n);
}
function ge(e, t, n) {
	let r = t.parentById.get(e);
	for (; r;) n.add(r), r = t.parentById.get(r);
}
function _e(e, t) {
	let n = /* @__PURE__ */ new Set();
	for (let r of e) ge(r, t, n);
	return n;
}
function ve(e, t, n) {
	let r = /* @__PURE__ */ new Set();
	for (let [i, a] of e.childrenById) {
		if (a.length === 0) continue;
		let o = e.nodeById.get(i);
		o && (n.get(i) ?? t.has(o.codeKey)) && r.add(i);
	}
	return r;
}
function Y(e, t, n) {
	let r = (e.children ?? []).map((e) => Y(e, t, n)).filter((e) => !!e);
	return !e.searchText.toLowerCase().includes(t) && r.length === 0 ? null : (r.length > 0 && n.add(e.id), r.length === 0 ? e : {
		...e,
		children: r
	});
}
function ye(e, t) {
	let n = t.trim().toLowerCase();
	if (!n) return {
		nodes: [...e],
		expandedIds: /* @__PURE__ */ new Set()
	};
	let r = /* @__PURE__ */ new Set();
	return {
		nodes: e.map((e) => Y(e, n, r)).filter((e) => !!e),
		expandedIds: r
	};
}
function be(e, t) {
	let n = [], r = (e, i, a) => {
		let o = (e.children?.length ?? 0) > 0, s = o && t.has(e.id);
		if (n.push({
			node: e,
			level: i,
			parentId: a,
			hasChildren: o,
			isExpanded: s,
			isLeaf: !o
		}), s) for (let t of e.children ?? []) r(t, i + 1, e.id);
	};
	for (let t of e) r(t, 0);
	return n;
}
//#endregion
//#region src/tree-select/model/buildTreeColumnsLayoutDescriptor.ts
function xe(e) {
	let t = [], n = 0;
	for (; n < e.length;) {
		let r = n + 1;
		for (; r < e.length && e[r]?.level !== 0;) r += 1;
		t.push({
			startIndex: n,
			endIndexExclusive: r
		}), n = r;
	}
	let r = t.map(({ startIndex: e, endIndexExclusive: t }) => `${e}:${t}`).join("|");
	return {
		itemCount: e.length,
		groups: t,
		signature: `${e.length}|${r}`
	};
}
//#endregion
//#region src/tree-select/model/resolveBalancedTreeColumnsLayout.ts
var Se = 16, Ce = 48, we = 224, X = 10, Te = 32, Ee = 6, De = 3;
function Z(e, t) {
	return Number.isFinite(e) && e > 0 ? e : t;
}
function Oe(e) {
	let t = e.endIndexExclusive - e.startIndex;
	return Math.min(De, t);
}
function ke(e, t) {
	let n = Math.max(1, t), r = [], i = 1, a = 1, o = 0;
	for (let t of e.groups) {
		let e = Oe(t), s = n - a + 1;
		a > 1 && e > s && (o += s, i += 1, a = 1);
		for (let e = t.startIndex; e < t.endIndexExclusive; e += 1) r[e] = {
			column: i,
			row: a
		}, a += 1, a > n && (i += 1, a = 1);
	}
	return {
		placements: r,
		usedColumnCount: r.length ? Math.max(...r.map((e) => e.column)) : 1,
		intentionalGapCount: o
	};
}
function Ae({ descriptor: e, availableWidth: t, availableHeight: n, viewportWidth: r, viewportHeight: i, referenceWidth: a }) {
	let o = Math.max(0, Math.floor(Number.isFinite(e.itemCount) ? e.itemCount : 0)), s = Z(t, 240), c = Z(n, 80), l = Z(r, s + 16), u = Z(i, c), d = Math.max(1, Math.min(s, Math.max(1, l - 16))), f = Math.max(1, Math.min(c, u - 16)), p = Math.max(1, Z(a, 1)), m = Math.min(d, p), h = Math.max(1, Math.floor((d - Se + X) / 234)), g = Math.max(1, Math.min(Ee, h, o || 1)), _ = e.groups.reduce((e, t) => Math.max(e, Oe(t)), 1);
	if (o === 0) return {
		columnCount: 1,
		rowCount: 1,
		width: m,
		minWidth: m,
		maxHeight: f,
		placements: []
	};
	let v, y = Infinity;
	for (let t = 1; t <= g; t += 1) {
		let n = Math.max(_, Math.ceil(o / t)), r = ke(e, n);
		for (; r.usedColumnCount > t && n < o;) n += 1, r = ke(e, n);
		let i = r.usedColumnCount, a = Math.max(m, Se + i * we + (i - 1) * X), s = Ce + n * Te, c = a / d, l = s / f, u = (i * n - o) / o, p = r.intentionalGapCount / o, h = Math.max(c, l) + .25 * Math.abs(c - l) + .01 * u + .02 * p + .04 * (i - 1);
		h < y && (y = h, v = {
			columnCount: i,
			rowCount: n,
			width: Math.min(d, a),
			minWidth: m,
			maxHeight: f,
			placements: r.placements
		});
	}
	return v ?? {
		columnCount: 1,
		rowCount: o,
		width: m,
		minWidth: m,
		maxHeight: f,
		placements: ke(e, o).placements
	};
}
var Q = {
	treeRow: "eMqOwq",
	treeRowSelected: "JKstO-",
	treeRowActive: "hKgr8e",
	treeRowDisabled: "KjPZjd",
	treeNodeButton: "_6EH37F",
	treePopupHeader: "UBll8Q",
	treeColumns: "-y3RZP",
	treeColumnRow: "ayLJ50",
	treeColumnsPopupLayout: "_430vL2",
	treeColumnRootLabel: "FkzTJI",
	treeColumnRootCode: "e8fE4t",
	treeColumnCheckBox: "_2MFkGF",
	treeNodeOptionContent: "SWQUvb",
	treeIndent: "o5NWnX",
	treeExpander: "ibSd-S",
	treeExpanderPlaceholder: "lXezvv",
	treeExpanderIcon: "FMlLGX",
	treeExpanderIconExpanded: "d2Zt7U",
	treeSelection: "_8PN4hs",
	treeSelectionChecked: "_4JEoen",
	treeSelectionPartial: "GS8eEC",
	treeSelectionIcon: "T8gVno",
	treeSummary: "o6stdX",
	inputWithOverlay: "I5zvhe",
	valueOverlay: "KQ0PBI",
	treeActions: "_7hAQQA",
	treeClearButton: "XeUHNc"
};
//#endregion
//#region src/tree-select/TreeNodeContent.tsx
function je({ node: e, level: t, highlight: r, hasChildren: i, isExpanded: a, selected: o, partial: s, selectionMode: c, optionsLayout: l = "tree", onToggleExpand: u, onToggleSelection: d }) {
	let f = l === "tree", m = l === "columns" && t === 0;
	return /* @__PURE__ */ O(E, { children: [
		/* @__PURE__ */ D("span", {
			className: Q.treeIndent,
			style: { "--tree-level": t },
			"aria-hidden": "true"
		}),
		f && i ? /* @__PURE__ */ D("button", {
			type: "button",
			className: Q.treeExpander,
			onClick: (e) => {
				e.stopPropagation(), u?.();
			},
			"aria-label": a ? "Свернуть ветку" : "Развернуть ветку",
			"data-ui": "tree-select-expander",
			"data-action": a ? "collapse-tree-select-node" : "expand-tree-select-node",
			children: /* @__PURE__ */ D(k, { className: T(Q.treeExpanderIcon, a && Q.treeExpanderIconExpanded) })
		}) : f ? /* @__PURE__ */ D("span", {
			className: Q.treeExpanderPlaceholder,
			"aria-hidden": "true"
		}) : null,
		c === "multi" || l === "columns" ? /* @__PURE__ */ D("span", {
			className: Q.treeColumnCheckBox,
			onMouseDown: (e) => e.stopPropagation(),
			onClick: (e) => e.stopPropagation(),
			children: /* @__PURE__ */ D(n, {
				value: o,
				indeterminate: s,
				disabled: e.disabled,
				"aria-label": `Выбрать ${e.label}`,
				onChange: () => d?.()
			})
		}) : null,
		/* @__PURE__ */ D("div", {
			className: Q.treeNodeOptionContent,
			children: /* @__PURE__ */ D(p, {
				label: e.label,
				code: e.code,
				highlight: r,
				labelClassName: m ? Q.treeColumnRootLabel : void 0,
				codeClassName: m ? Q.treeColumnRootCode : void 0
			})
		})
	] });
}
//#endregion
//#region src/tree-select/TreePickerBase.tsx
function Me({ label: n, description: c, disabled: p, placeholder: m, size: h, nodes: b, selectedIds: x, partialIds: S, selectionMode: C, optionsLayout: w = "tree", defaultExpandedCodeKeys: k, unavailableNodeIds: A, open: ne, onOpenChange: re, bulkActions: ie, triggerMode: j = "display", selectedSummary: M, selectedSummaryText: N, query: ae, defaultQuery: oe, onQuery: se, onNodeActivate: ce, onClearSelection: le, isLoading: P, error: F }) {
	let I = y(null), de = y(null), [L, R] = ee(!1), fe = ne !== void 0, z = fe ? ne : L, [B, pe] = ee(() => /* @__PURE__ */ new Map()), V = j === "search" ? C === "multi" ? "search-multi" : "search-single" : "display", H = v(() => ue(b), [b]), U = v(() => `${m} <${H.nodeById.size}>`, [m, H]), W = v(() => new Set(k), [k]), { query: G, setQuery: q, resetQueryOnClose: me } = u({
		query: ae,
		defaultQuery: oe,
		onQuery: se,
		triggerMode: V
	}), J = (e) => {
		z && !e && me && q(""), fe || R(e), re?.(e);
	}, { nodes: he, expandedIds: ge } = v(() => ye(b, G), [b, G]), Y = v(() => _e(x, H), [x, H]), Se = v(() => ve(H, W, B), [
		W,
		B,
		H
	]), Ce = v(() => new Set([...H.childrenById].filter(([, e]) => e.length > 0).map(([e]) => e)), [H]), we = v(() => w === "columns" ? Ce : /* @__PURE__ */ new Set([
		...Se,
		...ge,
		...Y
	]), [
		Ce,
		Se,
		w,
		ge,
		Y
	]), X = v(() => be(he, we), [he, we]), Te = v(() => X.findIndex((e) => x.has(e.node.id)), [x, X]), Ee = v(() => xe(X), [X]), De = g((e) => {
		let t = Ae({
			...e,
			descriptor: Ee
		}), n = {};
		return t.placements.forEach((e, t) => {
			n[`--tree-option-${t}-column`] = String(e.column), n[`--tree-option-${t}-row`] = String(e.row);
		}), {
			width: `${t.width}px`,
			minWidth: `${t.minWidth}px`,
			maxWidth: `${t.width}px`,
			maxHeight: `${t.maxHeight}px`,
			"--tree-column-count": String(t.columnCount),
			"--tree-row-count": String(t.rowCount),
			...n
		};
	}, [Ee]), { activeIndex: Z, context: Oe, floatingStyles: ke, getFloatingProps: Me, setReference: Ne, setFloating: Pe, setOptionRef: Fe, close: Ie, openList: Le, toggleOpen: Re, selectOption: ze, handleReferenceKeyDown: Be, handleFloatingKeyDown: Ve, getOptionId: He, getActiveOptionId: Ue } = o({
		options: X,
		selectedIndex: Te,
		open: z,
		onOpenChange: J,
		onSelect: (e) => ce(e.node),
		getOptionDisabled: (e) => e.node.disabled === !0 || A?.has(e.node.id) === !0,
		disabled: p || P,
		closeOnSelect: C === "single",
		allowOpenWithoutOptions: !0,
		triggerMode: V,
		placementStrategy: w === "columns" ? "auto" : "flip",
		resolveFloatingSize: w === "columns" ? De : void 0
	}), We = Oe.update, Ge = x.size > 0, Ke = j === "search" && (z || G.length > 0), qe = Ke ? G : N ?? "", Je = j === "display" && !!M, Ye = V === "search-multi" && !!M, Xe = V === "search-multi" && !Ge || V === "search-single" && (!N || Ke) || V === "display" && !Ge, $ = d({
		mode: V,
		open: z,
		currentQuery: G,
		hasDisplayValue: !!N,
		inputRef: I,
		setQuery: q,
		openList: Le,
		close: Ie,
		toggleOpen: Re
	}), Ze = w === "columns" && C === "multi" && ie, Qe = j === "display", $e = Ze || Qe ? /* @__PURE__ */ O("div", {
		className: Q.treePopupHeader,
		children: [Ze ? /* @__PURE__ */ D(i, {
			onSelectAll: ie.onSelectAll,
			onDeselectAll: ie.onDeselectAll,
			selectAllButtonRef: de
		}) : null, Qe ? /* @__PURE__ */ D(t, {
			value: G,
			onChange: q,
			onClear: () => q(""),
			placeholder: "Поиск по дереву",
			onKeyDown: (e) => {
				e.key !== "Escape" && e.key !== "Tab" && e.stopPropagation();
			},
			onClick: (e) => {
				e.stopPropagation();
			}
		}) : null]
	}) : void 0;
	_(() => {
		z && w === "columns" && We();
	}, [
		Ee.signature,
		z,
		w,
		We
	]);
	let et = (e) => {
		I.current = e;
	}, tt = () => {
		le?.(), q(""), C === "single" && Ie();
	}, nt = (e) => {
		e.hasChildren && pe((t) => {
			let n = new Map(t), r = t.get(e.node.id) ?? W.has(e.node.codeKey);
			return n.set(e.node.id, !r), n;
		});
	};
	return /* @__PURE__ */ D(l, {
		label: n,
		description: c,
		disabled: p,
		size: h,
		children: ({ controlId: t, labelId: i, describedBy: o }) => {
			let c = `${t}-listbox`, l = typeof n == "string" ? n : m;
			return /* @__PURE__ */ O(E, { children: [/* @__PURE__ */ D(r, {
				ref: et,
				rootRef: Ne,
				id: t,
				type: "text",
				role: "combobox",
				autoComplete: "off",
				isLoading: P,
				disabled: p,
				readOnly: j !== "search",
				value: j === "display" && Je ? "" : qe,
				placeholder: Xe ? U : void 0,
				"aria-labelledby": i,
				"aria-label": i ? void 0 : m,
				"aria-describedby": o,
				"aria-haspopup": w === "columns" ? "dialog" : "listbox",
				"aria-expanded": z,
				"aria-controls": z ? c : void 0,
				"aria-autocomplete": w !== "columns" && j === "search" ? "list" : "none",
				"aria-activedescendant": w !== "columns" && z && X.length > 0 ? Ue(c) : void 0,
				inputClassName: T(Ge && Q.treeSummary, Je && Q.inputWithOverlay),
				overlay: Je ? /* @__PURE__ */ D("div", {
					className: Q.valueOverlay,
					children: M
				}) : Ye ? M : void 0,
				endAdornment: /* @__PURE__ */ D(s, {
					open: z,
					disabled: p || P,
					onToggleMouseDown: $.handleToggleMouseDown,
					onToggleClick: $.handleToggleClick,
					children: Ge && le ? /* @__PURE__ */ D("button", {
						type: "button",
						className: Q.treeClearButton,
						onMouseDown: (e) => {
							e.preventDefault(), e.stopPropagation();
						},
						onClick: (e) => {
							e.preventDefault(), e.stopPropagation(), tt();
						},
						"aria-label": "Очистить выбор",
						children: /* @__PURE__ */ D(te, {})
					}) : null
				}),
				onChange: (e) => {
					j === "search" && $.handleTriggerInputChange(e.target.value);
				},
				onClick: $.handleTriggerClick,
				onFocus: (e) => {
					$.handleTriggerFocus(e.currentTarget);
				},
				onKeyDown: (e) => {
					if (w === "columns" && z && e.key === "Tab" && !e.shiftKey && de.current) {
						e.preventDefault(), de.current.focus();
						return;
					}
					Be(e), !e.defaultPrevented && $.handleTriggerKeyDown({
						event: e,
						onActivateWhenOpen: () => {
							let e = Z >= 0 ? X[Z] : void 0;
							e && ze(e);
						},
						enableSpaceActivation: j === "display"
					});
				}
			}), /* @__PURE__ */ D(a, {
				open: z,
				context: Oe,
				floatingStyles: ke,
				listId: c,
				labelId: i,
				popupAriaLabel: l,
				descriptionId: o,
				activeOptionId: w !== "columns" && X.length > 0 ? Ue(c) : void 0,
				ariaMultiselectable: w !== "columns" && C === "multi",
				popupRole: w === "columns" ? "dialog" : "listbox",
				setFloating: Pe,
				getFloatingProps: Me,
				onKeyDown: Ve,
				className: e.uiPopupOptions,
				layoutClassName: w === "columns" ? Q.treeColumnsPopupLayout : void 0,
				bodyClassName: "scrollable",
				header: $e,
				children: /* @__PURE__ */ D("div", {
					className: T(w === "columns" && Q.treeColumns),
					children: X.length === 0 ? /* @__PURE__ */ D(f, {
						errorState: F,
						emptyState: !F && !P ? "Нет данных" : void 0
					}) : X.map((e, t) => {
						let n = C === "multi" ? K(e.node.id, x, H) : x.has(e.node.id), r = C === "multi" && !n && S.has(e.node.id), i = t === Z, a = e.node.disabled === !0 || A?.has(e.node.id) === !0;
						return /* @__PURE__ */ D("div", {
							"data-ui": "tree-select-option",
							id: He(c, t),
							ref: (e) => Fe(t, e),
							role: w === "columns" ? void 0 : "option",
							"aria-selected": w === "columns" ? void 0 : n,
							"aria-disabled": a || void 0,
							className: T(Q.treeRow, w === "columns" && Q.treeColumnRow, a && Q.treeRowDisabled, i && Q.treeRowActive, n && Q.treeRowSelected),
							style: w === "columns" ? {
								"--tree-option-column": `var(--tree-option-${t}-column, auto)`,
								"--tree-option-row": `var(--tree-option-${t}-row, auto)`
							} : void 0,
							onMouseDown: (e) => {
								e.preventDefault();
							},
							onClick: () => ze(e),
							children: /* @__PURE__ */ D("div", {
								className: Q.treeNodeButton,
								children: /* @__PURE__ */ D(je, {
									node: a ? {
										...e.node,
										disabled: !0
									} : e.node,
									level: e.level,
									highlight: G,
									hasChildren: e.hasChildren,
									isExpanded: e.isExpanded,
									selected: n,
									partial: r,
									selectionMode: C,
									optionsLayout: w,
									onToggleExpand: () => nt(e),
									onToggleSelection: () => ze(e)
								})
							})
						}, e.node.id);
					})
				})
			})] });
		}
	});
}
//#endregion
//#region src/tree-select/TreeMultiSelect.tsx
function Ne(e, t) {
	let n = R(e, t), r = Object.values(n).reduce((e, t) => e + t.length, 0);
	if (r !== 0) {
		if (r === 1) {
			let n = [...e].sort((e, n) => (t.preorderIndexById.get(e) ?? 2 ** 53 - 1) - (t.preorderIndexById.get(n) ?? 2 ** 53 - 1))[0], r = t.nodeById.get(n);
			return r ? r.label : void 0;
		}
		return m(r);
	}
}
function Pe(e, t, n) {
	let r = L(e, n), i = L(t, n);
	return r.size === i.size && [...r].every((e) => i.has(e));
}
function Fe({ label: e, description: t, disabled: n, placeholder: r = "Выберите значения", size: i, nodes: a, value: o, onChange: s, query: l, defaultQuery: u, onQuery: d, isLoading: f, error: p, optionsLayout: m = "tree", defaultExpandedCodeKeys: g }) {
	let [_, y] = ee(!1), b = v(() => ue(a), [a]), { draftValue: x, setDraftValue: S, prepareOpen: C } = c({
		value: o,
		open: _,
		onCommit: s,
		isEqual: (e, t) => Pe(e, t, b)
	}), w = _ ? x : o, T = v(() => L(w, b), [w, b]), E = v(() => me(T, b), [T, b]), O = v(() => new Set([...b.nodeById.keys()].filter((e) => b.nodeById.get(e)?.disabled !== !0 && !K(e, T, b) && !E.selectedIds.has(e) && J(b, [e]).size === 0)), [
		T,
		E.selectedIds,
		b
	]), k = v(() => Ne(T, b), [T, b]);
	return /* @__PURE__ */ D(Me, {
		label: e,
		description: t,
		disabled: n,
		placeholder: r,
		size: i,
		nodes: a,
		selectedIds: E.selectedIds,
		partialIds: E.partialIds,
		selectionMode: "multi",
		optionsLayout: m,
		defaultExpandedCodeKeys: g,
		unavailableNodeIds: O,
		open: _,
		onOpenChange: (e) => {
			e && !_ && C(), y(e);
		},
		bulkActions: {
			onSelectAll: () => {
				S(R(J(b), b));
			},
			onDeselectAll: () => S({})
		},
		triggerMode: "search",
		selectedSummary: k ? /* @__PURE__ */ D(h, { value: k }) : void 0,
		query: l,
		defaultQuery: u,
		onQuery: d,
		onNodeActivate: (e) => {
			S((t) => he(t, e.id, b));
		},
		onClearSelection: () => {
			S({}), _ || s({});
		},
		isLoading: f,
		error: p
	});
}
//#endregion
//#region src/tree-select/ODataTreeMultiSelect.tsx
function Ie({ label: e, description: t, disabled: n, size: r, placeholder: i, query: a, defaultQuery: o, onQuery: s, optionsLayout: c, value: l, onChange: u, odata: d, segments: f, model: p, defaultExpandedCodeKeys: m, segmentOrder: h, allowEmptyCodeKeys: g, transformNodes: _ }) {
	let y = P({
		odata: d,
		segments: f,
		model: p,
		segmentOrder: h,
		allowEmptyCodeKeys: g
	}), ee = v(() => _?.(y.nodes, { orderedCodeKeys: y.orderedCodeKeys }) ?? y.nodes, [
		_,
		y.nodes,
		y.orderedCodeKeys
	]);
	return /* @__PURE__ */ D(Fe, {
		label: e,
		description: t,
		disabled: n || y.isLoading,
		size: r,
		placeholder: i ?? y.placeholder,
		nodes: ee,
		value: l,
		onChange: u,
		query: a,
		defaultQuery: o,
		onQuery: s,
		optionsLayout: c,
		defaultExpandedCodeKeys: m,
		isLoading: y.isLoading,
		error: y.isError ? "Ошибка загрузки" : void 0
	});
}
//#endregion
//#region src/tree-select/TreeSelect.tsx
function Le(e) {
	if (e) return e.code && e.code !== e.label ? `${e.label} · ${e.code}` : e.label;
}
function Re({ label: e, description: t, disabled: n, placeholder: r = "Выберите значение", size: i, nodes: a, value: o, onChange: s, query: c, defaultQuery: l, onQuery: u, isLoading: d, error: f, clearable: p = !1, defaultExpandedCodeKeys: m }) {
	let h = v(() => ue(a), [a]), g = de(o, h), _ = g ? h.nodeById.get(g) : void 0;
	return /* @__PURE__ */ D(Me, {
		label: e,
		description: t,
		disabled: n,
		placeholder: r,
		size: i,
		nodes: a,
		selectedIds: g ? /* @__PURE__ */ new Set([g]) : /* @__PURE__ */ new Set(),
		partialIds: /* @__PURE__ */ new Set(),
		selectionMode: "single",
		defaultExpandedCodeKeys: m,
		triggerMode: "search",
		selectedSummaryText: Le(_),
		query: c,
		defaultQuery: l,
		onQuery: u,
		onNodeActivate: (e) => s({
			codeKey: e.codeKey,
			value: e.value
		}),
		onClearSelection: p ? () => s(void 0) : void 0,
		isLoading: d,
		error: f
	});
}
//#endregion
//#region src/tree-select/ODataTreeSelect.tsx
function ze({ label: e, description: t, disabled: n, size: r, placeholder: i, query: a, defaultQuery: o, onQuery: s, value: c, onChange: l, odata: u, segments: d, model: f, clearable: p, defaultExpandedCodeKeys: m }) {
	let h = P({
		odata: u,
		segments: d,
		model: f
	});
	return /* @__PURE__ */ D(Re, {
		label: e,
		description: t,
		disabled: n || h.isLoading,
		size: r,
		placeholder: i ?? h.placeholder,
		nodes: h.nodes,
		value: c,
		onChange: l,
		query: a,
		defaultQuery: o,
		onQuery: s,
		defaultExpandedCodeKeys: m,
		isLoading: h.isLoading,
		error: h.isError ? "Ошибка загрузки" : void 0,
		clearable: p
	});
}
//#endregion
export { Ie as ODataTreeMultiSelect, ze as ODataTreeSelect, Fe as TreeMultiSelect, Re as TreeSelect, ne as treeSelectValueToSelectedFilters };

//# sourceMappingURL=index.js.map