import { t as e } from "./ui.module-BisoTevy.js";
import { c as t } from "./input-Bp6_Ucgr.js";
import { a as n, c as r, f as i, i as a, l as o, o as s, p as c, r as l, t as u, u as d } from "./picker-e6IsrMUG.js";
import { n as f, r as p } from "./useODataSelectBase-CKe1AVMI.js";
import { Fragment as m, useCallback as h, useMemo as g, useRef as ee, useState as te } from "react";
import { useODataCollectionModel as _ } from "@ryuzaki13/react-foundation-api/odata";
import { cn as v } from "@ryuzaki13/react-foundation-lib/utils";
import { CheckIcon as ne, XIcon as re } from "lucide-react";
import { Fragment as y, jsx as b, jsxs as x } from "react/jsx-runtime";
//#region src/select/lib/getOptionSearchText.ts
function ie({ option: e, getOptionLabel: t, getOptionCode: n, getOptionGroup: r }) {
	return [
		s(t(e)),
		s(n?.(e)),
		s(r?.(e)?.label)
	].filter((e) => !!e).join(" ").toLowerCase();
}
function S(e, t, n) {
	return e.find((e) => e.value === t) ?? n;
}
//#endregion
//#region src/select/model/useODataSelect.ts
function ae({ odata: e, segment: t, model: n, value: r, dependencies: i, onChange: a }) {
	let o = f({
		odata: e,
		segment: t,
		model: n,
		value: r ? [r] : [],
		dependencies: i
	}), s = o.selectedItems[0], c = g(() => s ? [s, ...o.filteredItems] : o.filteredItems, [o.filteredItems, s]), l = (e) => {
		a(e?.[o.selectionKey]);
	};
	return {
		...o,
		selectedItem: s,
		options: c,
		handleChange: l
	};
}
//#endregion
//#region src/select/lib/createSelectOptionSections.ts
function oe(e, t) {
	let n = [];
	return e.forEach((e, r) => {
		let i = t(e), a = n.at(-1), o = a?.group?.key === i?.key;
		if (a && o) {
			a.items.push({
				option: e,
				index: r
			});
			return;
		}
		n.push({
			group: i,
			items: [{
				option: e,
				index: r
			}]
		});
	}), n;
}
var C = {
	inputWithOverlay: "aEEqH1",
	valueOverlay: "_4HQKa7"
};
//#endregion
//#region src/select/Select.tsx
function w(t) {
	let { label: f, description: p, disabled: _, placeholder: S, size: ae, options: w, value: T, onChange: E, getOptionKey: D, getOptionLabel: O, getOptionCode: k, getOptionDisabled: A, getOptionAriaLabel: se, getOptionGroup: j, getOptionClassName: ce, renderOption: M, renderValue: N, className: le, buttonClassName: ue, optionsClassName: de, optionsMaxWidth: fe, optionsContentClassName: pe, searchable: P = !1, defaultFilter: me = !0, query: he, defaultQuery: ge, onQuery: _e, renderPopupHeader: ve, emptyState: ye, loadingState: be, errorState: xe, placement: Se = "bottom-start", clearable: F = !1 } = t, I = E, L = ee(null), [R, Ce] = te(!1), z = P ? "search-single" : "display", B = T === void 0 ? void 0 : D(T), V = B === void 0 ? void 0 : w.find((e) => D(e) === B) ?? T, { query: H, setQuery: U } = l({
		open: R,
		query: he,
		defaultQuery: ge,
		onQuery: _e,
		triggerMode: z
	}), we = h((e) => ie({
		option: e,
		getOptionLabel: O,
		getOptionCode: k,
		getOptionGroup: j
	}), [
		k,
		j,
		O
	]), W = n({
		options: w,
		query: H,
		enabled: P && me,
		getSearchText: we
	}), G = g(() => j ? oe(W, j) : void 0, [j, W]), Te = B === void 0 ? -1 : W.findIndex((e) => D(e) === B), { activeIndex: Ee, context: De, floatingStyles: Oe, getFloatingProps: ke, setReference: Ae, setFloating: je, setOptionRef: Me, close: K, openList: Ne, toggleOpen: Pe, selectOption: Fe, selectActiveOption: Ie, handleReferenceKeyDown: Le, handleFloatingKeyDown: Re, getOptionId: ze, getActiveOptionId: q } = a({
		options: W,
		selectedIndex: Te,
		open: R,
		onOpenChange: Ce,
		getOptionDisabled: A,
		onSelect: I,
		disabled: _,
		placement: Se,
		triggerMode: z
	}), J = W.length > 0, Y = V === void 0 ? void 0 : O(V), X = V === void 0 ? void 0 : N ? N(V) : Y, Be = s(Y), Ve = V === void 0 ? void 0 : s(k?.(V)), He = s(X) ?? Be ?? Ve ?? (B === void 0 ? "" : String(B)), Z = P && (R || H.length > 0), Ue = Z ? H : V === void 0 ? "" : He, Q = V !== void 0 && X !== void 0 && s(X) === void 0 && !Z, $ = u({
		mode: z,
		open: R,
		currentQuery: H,
		hasDisplayValue: V !== void 0,
		inputRef: L,
		setQuery: U,
		openList: Ne,
		close: K,
		toggleOpen: Pe
	}), We = (e) => {
		L.current = e;
	}, Ge = () => {
		F && I(void 0), U(""), K();
	}, Ke = (t, n, r) => {
		let i = D(n), a = B !== void 0 && i === B, o = r === Ee, s = A?.(n) ?? !1, c = {
			active: o,
			selected: a,
			disabled: s
		};
		return /* @__PURE__ */ b("div", {
			id: ze(t, r),
			ref: (e) => Me(r, e),
			role: "option",
			"aria-selected": a,
			"aria-disabled": s || void 0,
			"aria-label": se?.(n),
			className: v(e.uiPopupOption, s && e.disabled, o && e.uiPopupOptionActive, a && e.selected, ce?.(n, c)),
			onClick: () => Fe(n),
			children: M ? M(n, c) : /* @__PURE__ */ x(y, { children: [
				b(a ? ne : "span", { className: e.uiPopupOptionIcon }),
				/* @__PURE__ */ b("div", {
					className: e.uiOptionText,
					children: O(n)
				}),
				k && /* @__PURE__ */ b("div", {
					className: e.uiOptionCode,
					children: k(n)
				})
			] })
		}, i);
	};
	return /* @__PURE__ */ b(c, {
		label: f,
		description: p,
		disabled: _,
		size: ae,
		className: le,
		children: ({ controlId: t, labelId: n, describedBy: a }) => {
			let s = `${t}-listbox`;
			return /* @__PURE__ */ x(y, { children: [/* @__PURE__ */ b(r, {
				ref: We,
				rootRef: Ae,
				id: t,
				type: "text",
				disabled: _,
				readOnly: !P,
				autoComplete: "off",
				role: "combobox",
				value: Ue,
				placeholder: V === void 0 || Z ? `${S} <${w.length}>` : void 0,
				"aria-labelledby": n,
				"aria-describedby": a,
				"aria-haspopup": "listbox",
				"aria-expanded": R,
				"aria-controls": R ? s : void 0,
				"aria-autocomplete": P ? "list" : "none",
				"aria-activedescendant": R ? q(s) : void 0,
				inputClassName: v(Q && C.inputWithOverlay, ue, "textOverflow"),
				overlay: Q ? /* @__PURE__ */ b("div", {
					className: C.valueOverlay,
					children: X
				}) : void 0,
				endAdornment: /* @__PURE__ */ b(o, {
					open: R,
					disabled: _,
					onToggleMouseDown: $.handleToggleMouseDown,
					onToggleClick: $.handleToggleClick,
					children: F && V !== void 0 && !_ ? /* @__PURE__ */ b("button", {
						type: "button",
						className: e.uiClearButton,
						"data-ui": "select-clear-button",
						"data-action": "clear-select",
						onMouseDown: (e) => {
							e.preventDefault(), e.stopPropagation();
						},
						onClick: (e) => {
							e.preventDefault(), e.stopPropagation(), Ge();
						},
						"aria-label": "Очистить выбор",
						children: /* @__PURE__ */ b(re, {})
					}) : null
				}),
				onChange: (e) => {
					P && $.handleTriggerInputChange(e.target.value);
				},
				onClick: $.handleTriggerClick,
				onFocus: (e) => {
					$.handleTriggerFocus(e.currentTarget);
				},
				onKeyDown: (e) => {
					Le(e), !e.defaultPrevented && $.handleTriggerKeyDown({
						event: e,
						onActivateWhenOpen: Ie,
						enableSpaceActivation: !P
					});
				}
			}), /* @__PURE__ */ b(i, {
				open: R,
				context: De,
				floatingStyles: Oe,
				listId: s,
				labelId: n,
				descriptionId: a,
				activeOptionId: J ? q(s) : void 0,
				setFloating: je,
				getFloatingProps: ke,
				onKeyDown: Re,
				className: v(e.uiPopupOptions, de),
				maxWidth: fe,
				header: ve,
				children: /* @__PURE__ */ b("div", {
					className: v(pe, "h100 scrollable"),
					children: J ? G ? G.map((t, n) => {
						let r = t.group ? `${s}-group-${n}` : void 0, i = t.items.map(({ option: e, index: t }) => Ke(s, e, t));
						return t.group ? /* @__PURE__ */ x("div", {
							role: "group",
							"aria-labelledby": r,
							children: [/* @__PURE__ */ b("div", {
								id: r,
								className: e.uiPopupGroupLabel,
								children: t.group.label
							}), i]
						}, `group-${String(t.group.key)}-${n}`) : /* @__PURE__ */ b(m, { children: i }, `options-${n}`);
					}) : W.map((e, t) => Ke(s, e, t)) : /* @__PURE__ */ b(d, {
						emptyState: ye,
						loadingState: be,
						errorState: xe
					})
				})
			})] });
		}
	});
}
//#endregion
//#region src/select/ODataSelect.tsx
function T(e, t, n) {
	return e ? t || Object.keys(e).find((e) => e !== n) || n : t || n;
}
var E = ({ odata: e, segment: n, model: r, size: i, label: a, description: o, disabled: s, value: c, dependencies: l, onChange: u, clearable: d }) => {
	let f = ae({
		odata: e,
		segment: n,
		model: _(r),
		value: c,
		dependencies: l,
		onChange: u
	});
	if (f.isLoading) return /* @__PURE__ */ b(t, {
		size: i,
		label: a,
		description: o
	});
	let m = n.hideCode ?? e.hideCode, h = T(f.selectedItem ?? f.options[0], f.textKey, f.codeKey);
	return /* @__PURE__ */ b(w, {
		size: i,
		label: a,
		description: o,
		options: f.options,
		value: f.selectedItem,
		onChange: f.handleChange,
		getOptionKey: (e) => e[f.codeKey],
		getOptionLabel: (e) => e[h],
		getOptionCode: m ? void 0 : (e) => e[f.codeKey],
		getOptionAriaLabel: (e) => m || e[h] === e[f.codeKey] ? String(e[h]) : `${e[h]} ${e[f.codeKey]}`,
		renderValue: (e) => /* @__PURE__ */ b(p, {
			label: e[h],
			code: m ? void 0 : e[f.codeKey]
		}),
		renderOption: (e) => /* @__PURE__ */ b(p, {
			label: e[h],
			code: m ? void 0 : e[f.codeKey],
			highlight: f.debouncedQuery
		}),
		searchable: !0,
		query: f.query,
		onQuery: f.setQuery,
		defaultFilter: !1,
		disabled: s || f.isLoading,
		placeholder: f.placeholder,
		loadingState: "Загрузка...",
		errorState: f.isError ? "Ошибка загрузки" : void 0,
		emptyState: !f.isLoading && !f.isError ? "Нет данных" : void 0,
		clearable: d
	});
};
//#endregion
//#region src/select/SerializableSelect.tsx
function D(e, t) {
	return e[t];
}
function O(e, t, n) {
	let r = e[t];
	if (typeof r == "string" || typeof r == "number") return r;
	let i = D(e, n);
	return typeof i == "string" || typeof i == "number" ? i : String(i);
}
function k({ label: e, description: t, disabled: n, placeholder: r, size: i, options: a, value: o, onChange: s, optionKey: c, optionLabel: l, renderCode: u, className: d, clearable: f }) {
	let p = o === void 0 ? void 0 : a.find((e) => D(e, c) === o);
	return /* @__PURE__ */ b(w, {
		label: e,
		description: t,
		disabled: n,
		placeholder: r,
		size: i,
		options: a,
		value: p,
		onChange: (e) => s(e ? D(e, c) : void 0),
		getOptionKey: (e) => D(e, c),
		getOptionLabel: (e) => O(e, l, c),
		getOptionCode: u ? (e) => D(e, c) : void 0,
		className: d,
		clearable: f
	});
}
//#endregion
export { S as i, E as n, w as r, k as t };

//# sourceMappingURL=select-BON0S9Dr.js.map