import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { Row } from "./components/Row.js";
import { useExpandedKeys } from "./hooks/useExpandedKeys.js";
import React, { useCallback, useMemo } from "react";
import classNames from "classnames";
import "./PisellHierarchicalSummaryList.less";
//#region src/components/pisellHierarchicalSummaryList/PisellHierarchicalSummaryList.tsx
/** 根节点类名，供上层用同一 DOM 包裹列表与其它区块时使用 */
const PREFIX_CLS = "pisell-hierarchical-summary-list";
function normalizeItems(items) {
	return items.filter((i) => !i.hidden).slice().sort((a, b) => {
		var _a$order, _b$order;
		return ((_a$order = a.order) !== null && _a$order !== void 0 ? _a$order : 0) - ((_b$order = b.order) !== null && _b$order !== void 0 ? _b$order : 0);
	});
}
function walkForDefaultExpandedKeys(options) {
	const { items, defaultExpandedLevel, getKey } = options;
	if (!defaultExpandedLevel || defaultExpandedLevel <= 0) return [];
	const keys = [];
	const dfs = (list, level, parentPathKeys) => {
		if (level > defaultExpandedLevel) return;
		normalizeItems(list).forEach((item, index) => {
			var _item$key, _item$children;
			const key = (_item$key = item.key) !== null && _item$key !== void 0 ? _item$key : getKey(item, {
				level,
				index,
				parentPathKeys
			});
			const children = normalizeItems((_item$children = item.children) !== null && _item$children !== void 0 ? _item$children : []);
			if (children.length > 0 && level < defaultExpandedLevel) {
				keys.push(key);
				dfs(children, level + 1, [...parentPathKeys, key]);
			}
		});
	};
	dfs(items, 1, []);
	return keys;
}
const PisellHierarchicalSummaryList = React.memo(({ items, expandedKeys, defaultExpandedKeys, defaultExpandedLevel = 0, onExpandedKeysChange, accordion = false, maxRenderLevel, maxExpandLevel, indent = 16, rowGap = 8, padding, gaps, levelConfig, renderLabel, renderValue, getKey: getKeyProp, aggregate, className, style, renderRoot = true }) => {
	const rootStyle = useMemo(() => _objectSpread2(_objectSpread2({}, style), padding !== void 0 ? { padding: typeof padding === "number" ? `${padding}px` : padding } : {}), [padding, style]);
	const getKey = useCallback((item, ctx) => {
		if (item.key !== void 0 && item.key !== null) return item.key;
		if (getKeyProp) return getKeyProp(item, ctx);
		const parent = ctx.parentPathKeys.map(String).join("/");
		return `lvl${ctx.level}:${parent ? `${parent}/` : ""}${ctx.index}`;
	}, [getKeyProp]);
	const computedDefaultExpandedKeys = useMemo(() => walkForDefaultExpandedKeys({
		items,
		defaultExpandedLevel,
		getKey
	}), [
		defaultExpandedLevel,
		getKey,
		items
	]);
	const expandedController = useExpandedKeys({
		expandedKeys,
		defaultExpandedKeys: useMemo(() => {
			const merged = /* @__PURE__ */ new Set();
			(defaultExpandedKeys !== null && defaultExpandedKeys !== void 0 ? defaultExpandedKeys : []).forEach((k) => merged.add(k));
			computedDefaultExpandedKeys.forEach((k) => merged.add(k));
			return Array.from(merged);
		}, [computedDefaultExpandedKeys, defaultExpandedKeys]),
		onExpandedKeysChange
	});
	const normalizedRootItems = useMemo(() => normalizeItems(items), [items]);
	const toggleExpanded = useCallback((key, options) => {
		var _options$siblingKeys;
		const prev = expandedController.expandedKeys;
		const set = new Set(prev);
		if (set.has(key)) {
			set.delete(key);
			expandedController.setExpandedKeys(Array.from(set));
			return;
		}
		if (accordion && (options === null || options === void 0 || (_options$siblingKeys = options.siblingKeys) === null || _options$siblingKeys === void 0 ? void 0 : _options$siblingKeys.length)) options.siblingKeys.forEach((k) => set.delete(k));
		set.add(key);
		expandedController.setExpandedKeys(Array.from(set));
	}, [accordion, expandedController]);
	const rowsContent = normalizedRootItems.map((item, index) => {
		var _item$key2;
		const rootLevel = index === 0 ? 1 : 2;
		return /* @__PURE__ */ React.createElement(Row, {
			key: (_item$key2 = item.key) !== null && _item$key2 !== void 0 ? _item$key2 : getKey(item, {
				level: rootLevel,
				index,
				parentPathKeys: []
			}),
			prefixCls: PREFIX_CLS,
			item,
			level: rootLevel,
			index,
			parentPathKeys: [],
			expandedKeySet: expandedController.expandedKeySet,
			toggleExpanded,
			getKey,
			levelConfig,
			indent,
			rowGap,
			gaps,
			maxRenderLevel,
			maxExpandLevel,
			aggregate,
			renderLabel,
			renderValue
		});
	});
	if (renderRoot === false) return /* @__PURE__ */ React.createElement("div", { style: rootStyle }, rowsContent);
	return /* @__PURE__ */ React.createElement("div", {
		className: classNames(PREFIX_CLS, className),
		style: rootStyle
	}, rowsContent);
});
PisellHierarchicalSummaryList.displayName = "PisellHierarchicalSummaryList";
//#endregion
export { PREFIX_CLS, PisellHierarchicalSummaryList };
