import { HighlightText as e } from "../text/index.js";
import { t } from "./ui.module-BisoTevy.js";
import { useEffect as n, useMemo as r, useState as i } from "react";
import { useODataCollection as a } from "@ryuzaki13/react-foundation-api/odata";
import { cn as o } from "@ryuzaki13/react-foundation-lib/utils";
import { jsx as s, jsxs as c } from "react/jsx-runtime";
import { startsWithIgnoringZeros as l } from "@ryuzaki13/react-foundation-lib/formatters";
//#region src/select/SelectOptionContent.tsx
function u({ label: n, code: r, highlight: i }) {
	return /* @__PURE__ */ c("div", {
		className: t.uiOptionBase,
		children: [/* @__PURE__ */ s("div", {
			className: o(t.uiOptionText, "flexEllipsis"),
			children: /* @__PURE__ */ s(e, {
				text: n,
				highlight: i
			})
		}), r && /* @__PURE__ */ s("div", {
			className: t.uiOptionCode,
			children: /* @__PURE__ */ s(e, {
				text: r,
				highlight: i
			})
		})]
	});
}
//#endregion
//#region src/select/model/useODataSelectBase.ts
var d = "", f = "";
function p(e) {
	return e?.length ? [...new Set(e)].sort() : [];
}
function m(e, t, n) {
	let r = [];
	for (let i of Object.keys(t).sort()) {
		if (i === e) continue;
		let t = p(n?.[i]);
		t.length && r.push(`${i}:${t.join(f)}`);
	}
	return r.join(d);
}
function h(e) {
	let t = /* @__PURE__ */ new Map();
	if (!e) return t;
	for (let n of e.split(d)) {
		let e = n.indexOf(":");
		if (e < 0) continue;
		let r = n.slice(0, e), i = n.slice(e + 1).split(f).filter(Boolean);
		r && i.length && t.set(r, i);
	}
	return t;
}
function g({ odata: e, segment: t, model: o, value: s, dependencies: c }) {
	let [u, d] = i(""), [f, p] = i(""), { codeKey: g, searchDebounceDelay: _, minSearchCodeLength: v, minSearchTextLength: y } = o, { data: b, textKey: x, isLoading: S, isError: C, getItems: w, separatedItems: T, findSourceItemsByKeys: E, setFilteredItems: D, debounce: O } = a({
		odata: e,
		model: o
	}), k = T.length, A = t.selectText && x ? x : g, j = r(() => b ? m(g, b.keyPairsMap, c) : "", [
		b,
		g,
		c
	]);
	n(() => {
		if (!b) return;
		let e = b.chain.findIndex((e) => e.codeKey === g), t = h(j), n = (e) => {
			let n = b.chain[e].codeKey, r = t.get(n);
			if (r?.length) return E(n, r);
		};
		for (let t = b.chain.length - 1; t > e; t--) {
			let e = n(t);
			if (e) {
				D(e);
				return;
			}
		}
		for (let t = 0; t < e; t++) {
			let e = n(t);
			if (e) {
				D(e);
				return;
			}
		}
		D(void 0);
	}, [
		b,
		j,
		g,
		E,
		D
	]), n(() => {
		O("search", () => {
			p(u.trim().toLowerCase());
		}, _);
	}, [
		O,
		u,
		_
	]);
	let M = r(() => {
		if (!s?.length || !A) return [];
		let e = new Set(s);
		return T.filter((t) => e.has(t[A]));
	}, [
		T,
		A,
		s
	]), N = r(() => {
		if (!b) return [];
		if (!(f.length >= y)) return w(void 0, M);
		let e = f.length >= y, n = !t.hideCode && f.length >= v && !Number.isNaN(Number(f));
		return w((t, r) => !!(t && r && (n && l(t, f) || e && r.toLowerCase().includes(f))), M);
	}, [
		f,
		w,
		v,
		y,
		b,
		t.hideCode,
		M
	]);
	return {
		query: u,
		setQuery: d,
		debouncedQuery: f,
		selectedItems: M,
		filteredItems: N,
		itemsCount: k,
		isLoading: S,
		isError: C,
		codeKey: g,
		textKey: x,
		selectionKey: A,
		placeholder: r(() => {
			let e = N.length;
			return k ? e < k ? `${t.placeholder} <${e}+>` : `${t.placeholder} <${k > o.maxVisibleItems ? `${o.maxVisibleItems}+` : k}>` : `${t.placeholder} <пусто>`;
		}, [
			N.length,
			k,
			o.maxVisibleItems,
			t.placeholder
		])
	};
}
function _(e, t) {
	return e.map((e) => e[t]);
}
//#endregion
export { g as n, u as r, _ as t };

//# sourceMappingURL=useODataSelectBase-CWBR3WCn.js.map