require("../../_virtual/_rolldown/runtime.js");
let _pisell_materials = require("@pisell/materials");
//#region src/plus/pisellProResourcePickerModal/utils.ts
function normalizeSegment(value) {
	if (value == null) return null;
	const t = String(value).trim();
	return t ? t : null;
}
/** 解析单条记录在最多 3 级上的分类 key */
function resolveCategoryPath(item, config) {
	var _config$levels;
	if (!config) return [
		null,
		null,
		null
	];
	if (config.getCategoryPath) {
		const raw = config.getCategoryPath(item);
		return [
			normalizeSegment(raw === null || raw === void 0 ? void 0 : raw[0]),
			normalizeSegment(raw === null || raw === void 0 ? void 0 : raw[1]),
			normalizeSegment(raw === null || raw === void 0 ? void 0 : raw[2])
		];
	}
	if (config.nestedPathField) {
		const nested = (0, _pisell_materials.parseNestedPathSegments)(item[config.nestedPathField], 3);
		return [
			normalizeSegment(nested[0]),
			normalizeSegment(nested[1]),
			normalizeSegment(nested[2])
		];
	}
	const out = [
		null,
		null,
		null
	];
	const levels = (_config$levels = config.levels) !== null && _config$levels !== void 0 ? _config$levels : [];
	for (let i = 0; i < 3; i++) {
		var _levels$i;
		const field = (_levels$i = levels[i]) === null || _levels$i === void 0 ? void 0 : _levels$i.field;
		if (field) out[i] = normalizeSegment(item[field]);
	}
	return out;
}
/** 是否匹配当前已选层级路径（前缀匹配） */
function matchesSelection(item, selection, config) {
	if (!config) return true;
	const path = resolveCategoryPath(item, config);
	for (let i = 0; i < 3; i++) {
		const picked = selection[i];
		if (picked != null && path[i] !== picked) return false;
	}
	return true;
}
/**
* 根据 records 派生 PisellTabbarTemplate1 的多层级 dataSource。
* - id 使用「分类 key」（已确保非空字符串）
* - 顶部不再额外注入「全部」节点；不选层级即视为全部
*/
function buildTabbarDataSourceFromRecords(items, config) {
	if (!config || items.length === 0) return [];
	const root = [];
	const ensureChild = (list, key, label) => {
		let found = list.find((n) => n.id === key);
		if (!found) {
			found = {
				id: key,
				name: label,
				children: []
			};
			list.push(found);
		}
		return found;
	};
	for (const item of items) {
		const path = resolveCategoryPath(item, config);
		let cursor = root;
		for (let i = 0; i < 3; i++) {
			const seg = path[i];
			if (!seg) break;
			const node = ensureChild(cursor, seg, seg);
			if (!node.children) node.children = [];
			cursor = node.children;
		}
	}
	const cleanup = (nodes) => {
		nodes.forEach((n) => {
			if (n.children && n.children.length === 0) delete n.children;
			else if (n.children) cleanup(n.children);
		});
		nodes.sort((a, b) => String(a.name).localeCompare(String(b.name), "zh-CN"));
		return nodes;
	};
	return cleanup(root);
}
/** 默认搜索匹配文本：拼接 record 标题 + id */
function defaultGetSearchText(record) {
	var _record$id;
	return `${typeof record.title === "string" ? record.title : typeof record.name === "string" ? record.name : ""} ${String((_record$id = record.id) !== null && _record$id !== void 0 ? _record$id : "").trim()}`.toLowerCase();
}
//#endregion
exports.buildTabbarDataSourceFromRecords = buildTabbarDataSourceFromRecords;
exports.defaultGetSearchText = defaultGetSearchText;
exports.matchesSelection = matchesSelection;
