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

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