import { t as e } from "./ui.module-BisoTevy.js";
import { c as t } from "./input-BCJfUdK_.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-BAQ9NeHe.js";
import { n as f, r as p } from "./useODataSelectBase-CWBR3WCn.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 C({ 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 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(t) {
	let { label: f, description: p, disabled: _, 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 } = t, L = D, R = ee(null), [z, Se] = te(!1), B = F ? "search-single" : "display", V = E === void 0 ? void 0 : O(E), H = V === void 0 ? void 0 : T.find((e) => O(e) === V) ?? E, { query: U, setQuery: W } = l({
		open: z,
		query: me,
		defaultQuery: he,
		onQuery: ge,
		triggerMode: B
	}), Ce = h((e) => ie({
		option: e,
		getOptionLabel: k,
		getOptionCode: A,
		getOptionGroup: M
	}), [
		A,
		M,
		k
	]), G = n({
		options: T,
		query: U,
		enabled: F && pe,
		getSearchText: Ce
	}), K = g(() => M ? ae(G, M) : void 0, [M, G]), { activeIndex: we, context: Te, floatingStyles: Ee, getFloatingProps: De, setReference: Oe, setFloating: ke, setOptionRef: Ae, close: q, openList: je, toggleOpen: Me, selectOption: Ne, selectActiveOption: Pe, handleReferenceKeyDown: Fe, handleFloatingKeyDown: Ie, getOptionId: Le, getActiveOptionId: J } = a({
		options: G,
		selectedIndex: V === void 0 ? -1 : G.findIndex((e) => O(e) === V),
		open: z,
		onOpenChange: Se,
		getOptionDisabled: j,
		onSelect: L,
		disabled: _,
		placement: xe,
		triggerMode: B
	}), Y = G.length > 0, X = H === void 0 ? void 0 : k(H), Z = H === void 0 ? void 0 : P ? P(H) : X, Re = s(X), ze = H === void 0 ? void 0 : s(A?.(H)), Be = s(Z) ?? Re ?? ze ?? (V === void 0 ? "" : String(V)), Q = F && (z || U.length > 0), Ve = Q ? U : H === void 0 ? "" : Be, He = H !== void 0 && Z !== void 0 && s(Z) === void 0 && !Q, $ = u({
		mode: B,
		open: z,
		currentQuery: U,
		hasDisplayValue: H !== void 0,
		inputRef: R,
		setQuery: W,
		openList: je,
		close: q,
		toggleOpen: Me
	}), Ue = (e) => {
		R.current = e;
	}, We = () => {
		I && L(void 0), W(""), q();
	}, Ge = (t, n, r) => {
		let i = O(n), a = V !== void 0 && i === V, o = r === we, s = j?.(n) ?? !1, c = {
			active: o,
			selected: a,
			disabled: s
		};
		return /* @__PURE__ */ b("div", {
			id: Le(t, r),
			ref: (e) => Ae(r, e),
			role: "option",
			"aria-selected": a,
			"aria-disabled": s || void 0,
			"aria-label": oe?.(n),
			className: v(e.uiPopupOption, s && e.disabled, o && e.uiPopupOptionActive, a && e.selected, se?.(n, c)),
			onClick: () => Ne(n),
			children: N ? N(n, c) : /* @__PURE__ */ x(y, { children: [
				b(a ? ne : "span", { className: e.uiPopupOptionIcon }),
				/* @__PURE__ */ b("div", {
					className: e.uiOptionText,
					children: k(n)
				}),
				A && /* @__PURE__ */ b("div", {
					className: e.uiOptionCode,
					children: A(n)
				})
			] })
		}, i);
	};
	return /* @__PURE__ */ b(c, {
		label: f,
		description: p,
		disabled: _,
		placeholder: S,
		size: C,
		className: ce,
		children: ({ controlId: t, labelId: n, describedBy: a }) => {
			let s = `${t}-listbox`;
			return /* @__PURE__ */ x(y, { children: [/* @__PURE__ */ b(r, {
				ref: Ue,
				rootRef: Oe,
				id: t,
				type: "text",
				disabled: _,
				readOnly: !F,
				autoComplete: "off",
				role: "combobox",
				value: Ve,
				placeholder: H === void 0 || Q ? S : void 0,
				"aria-labelledby": n,
				"aria-describedby": a,
				"aria-haspopup": "listbox",
				"aria-expanded": z,
				"aria-controls": z ? s : void 0,
				"aria-autocomplete": F ? "list" : "none",
				"aria-activedescendant": z ? J(s) : void 0,
				inputClassName: v(He && w.inputWithOverlay, le, "textOverflow"),
				overlay: He ? /* @__PURE__ */ b("div", {
					className: w.valueOverlay,
					children: Z
				}) : void 0,
				endAdornment: /* @__PURE__ */ b(o, {
					open: z,
					disabled: _,
					onToggleMouseDown: $.handleToggleMouseDown,
					onToggleClick: $.handleToggleClick,
					children: I && H !== 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(), We();
						},
						"aria-label": "Очистить выбор",
						children: /* @__PURE__ */ b(re, {})
					}) : null
				}),
				onChange: (e) => {
					F && $.handleTriggerInputChange(e.target.value);
				},
				onClick: $.handleTriggerClick,
				onFocus: (e) => {
					$.handleTriggerFocus(e.currentTarget);
				},
				onKeyDown: (e) => {
					Fe(e), !e.defaultPrevented && $.handleTriggerKeyDown({
						event: e,
						onActivateWhenOpen: Pe,
						enableSpaceActivation: !F
					});
				}
			}), /* @__PURE__ */ b(i, {
				open: z,
				context: Te,
				floatingStyles: Ee,
				listId: s,
				labelId: n,
				descriptionId: a,
				activeOptionId: Y ? J(s) : void 0,
				setFloating: ke,
				getFloatingProps: De,
				onKeyDown: Ie,
				className: v(e.uiPopupOptions, ue),
				maxWidth: de,
				header: _e,
				children: /* @__PURE__ */ b("div", {
					className: v(fe, "h100 scrollable"),
					children: Y ? K ? K.map((t, n) => {
						let r = t.group ? `${s}-group-${n}` : void 0, i = t.items.map(({ option: e, index: t }) => Ge(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}`);
					}) : G.map((e, t) => Ge(s, e, t)) : /* @__PURE__ */ b(d, {
						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: e, segment: n, model: r, size: i, label: a, description: o, disabled: s, value: c, dependencies: l, onChange: u, clearable: d }) => {
	let f = C({
		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 = E(f.selectedItem ?? f.options[0], f.textKey, f.codeKey);
	return /* @__PURE__ */ b(T, {
		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 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-kZQRRj2e.js.map