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

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