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

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