//#region src/plus/pisellReservation/floorMap/reservationHudResourceRootBuckets.ts
/** 推断出的树根超过该数量时，视为树链不可靠，改用 form 维度聚合 */
const HUD_RAIL_TREE_ROOT_CAP = 20;
function normalizeRecordId(raw) {
	if (typeof raw === "number" && Number.isFinite(raw)) {
		const n = Math.trunc(raw);
		if (n > 0) return String(n);
		return null;
	}
	if (typeof raw === "string") {
		const t = raw.trim();
		if (!t || t === "0") return null;
		const n = Number(t);
		if (Number.isFinite(n) && n > 0) return String(Math.trunc(n));
		if (/^\d+$/.test(t)) return t;
	}
	return null;
}
function normalizeMainFieldFirstLine(raw) {
	var _first$trim;
	if (raw == null || typeof raw !== "string") return "";
	const first = raw.split(" ## ")[0];
	return (_first$trim = first === null || first === void 0 ? void 0 : first.trim()) !== null && _first$trim !== void 0 ? _first$trim : "";
}
function getParentRecordId(item) {
	var _item$parent_id;
	const p = (_item$parent_id = item.parent_id) !== null && _item$parent_id !== void 0 ? _item$parent_id : item.parentId;
	if (p == null || p === "") return null;
	if (typeof p === "number" && Number.isFinite(p)) {
		const n = Math.trunc(p);
		return n > 0 ? String(n) : null;
	}
	return normalizeRecordId(p);
}
function normalizeFormId(row) {
	var _row$form_id;
	const f = (_row$form_id = row.form_id) !== null && _row$form_id !== void 0 ? _row$form_id : row.formId;
	if (typeof f === "number" && Number.isFinite(f)) {
		const n = Math.trunc(f);
		return n > 0 ? String(n) : null;
	}
	if (typeof f === "string") {
		const t = f.trim();
		if (!t || t === "0") return null;
		const n = Number(t);
		if (Number.isFinite(n) && n > 0) return String(Math.trunc(n));
		if (/^\d+$/.test(t)) return t;
	}
	return null;
}
function pickFormAggregateLabel(rows) {
	const codes = /* @__PURE__ */ new Set();
	for (const r of rows) {
		const c = typeof r.form_code === "string" ? r.form_code.trim() : "";
		if (c) codes.add(c);
	}
	if (codes.size >= 1) {
		var _sort$;
		return (_sort$ = [...codes].sort((a, b) => a.localeCompare(b))[0]) !== null && _sort$ !== void 0 ? _sort$ : "Resource";
	}
	let shortest = "";
	for (const r of rows) {
		const m = normalizeMainFieldFirstLine(r.main_field);
		if (!m) continue;
		if (!shortest || m.length < shortest.length) shortest = m;
	}
	return shortest || "Other";
}
/** 按 form_id 分桶：每桶含该表单下全部资源 id（多选任一主类等同筛该表单下全部具体资源） */
function buildFormAggregateBuckets(rows) {
	const groups = /* @__PURE__ */ new Map();
	for (const row of rows) {
		if (!normalizeRecordId(row.id)) continue;
		const fk = normalizeFormId(row);
		const gk = fk !== null && fk !== void 0 ? fk : "__nof__";
		if (!groups.has(gk)) groups.set(gk, []);
		groups.get(gk).push(row);
	}
	const entries = [...groups.entries()].map(([formKey, groupRows]) => {
		let minSort = Infinity;
		for (const r of groupRows) {
			const s = typeof r.sort === "number" && Number.isFinite(r.sort) ? r.sort : Infinity;
			if (s < minSort) minSort = s;
		}
		if (!Number.isFinite(minSort)) minSort = 0;
		const label = pickFormAggregateLabel(groupRows);
		const keySet = /* @__PURE__ */ new Set();
		for (const r of groupRows) {
			const rid = normalizeRecordId(r.id);
			if (rid) keySet.add(rid);
		}
		const bucket = {
			id: `resourceForm:${formKey}`,
			label,
			keys: [...keySet]
		};
		return {
			sortKey: minSort,
			bucket
		};
	});
	entries.sort((a, b) => a.sortKey - b.sortKey);
	return entries.map((e) => e.bucket);
}
function buildTreeResourceRootBuckets(rows) {
	const idSet = /* @__PURE__ */ new Set();
	for (const row of rows) {
		const id = normalizeRecordId(row.id);
		if (id) idSet.add(id);
	}
	const childrenByParent = /* @__PURE__ */ new Map();
	for (const row of rows) {
		const id = normalizeRecordId(row.id);
		if (!id) continue;
		const pid = getParentRecordId(row);
		if (!pid || !idSet.has(pid)) continue;
		if (!childrenByParent.has(pid)) childrenByParent.set(pid, []);
		childrenByParent.get(pid).push(id);
	}
	const roots = [];
	const seen = /* @__PURE__ */ new Set();
	for (const row of rows) {
		const id = normalizeRecordId(row.id);
		if (!id || seen.has(id)) continue;
		const pid = getParentRecordId(row);
		if (!pid || !idSet.has(pid)) {
			roots.push(row);
			seen.add(id);
		}
	}
	roots.sort((a, b) => {
		return (typeof a.sort === "number" && Number.isFinite(a.sort) ? a.sort : 0) - (typeof b.sort === "number" && Number.isFinite(b.sort) ? b.sort : 0);
	});
	function collectSubtreeIds(rootId) {
		const out = [];
		const stack = [rootId];
		while (stack.length) {
			const cur = stack.pop();
			out.push(cur);
			const kids = childrenByParent.get(cur);
			if (!(kids === null || kids === void 0 ? void 0 : kids.length)) continue;
			for (const k of kids) stack.push(k);
		}
		return out;
	}
	const buckets = [];
	for (const row of roots) {
		const id = normalizeRecordId(row.id);
		if (!id) continue;
		const main = normalizeMainFieldFirstLine(row.main_field);
		const code = typeof row.form_code === "string" && row.form_code.trim() !== "" ? String(row.form_code).trim() : "";
		const label = (main || code || id).trim();
		const keys = collectSubtreeIds(id);
		buckets.push({
			id: `resourceRoot:${id}`,
			label,
			keys
		});
	}
	return buckets;
}
/**
* 从 form/resource/page 扁平 list 构建第一层桶。
* - 列表内父子链完整时：与 TreeSelect 第一层语义一致（根及其子树 id）；
* - 父链缺失导致伪根过多时：按 `form_id` 聚合为「主类」桶，避免竖栏铺满具体资源名。
*/
function buildResourceRootBuckets(rawList) {
	if (!Array.isArray(rawList) || rawList.length === 0) return [];
	const rows = rawList.filter((x) => x != null && typeof x === "object" && !Array.isArray(x));
	const treeBuckets = buildTreeResourceRootBuckets(rows);
	if (treeBuckets.length <= HUD_RAIL_TREE_ROOT_CAP) return treeBuckets;
	return buildFormAggregateBuckets(rows);
}
//#endregion
export { buildResourceRootBuckets };
