import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import React, { useCallback, useMemo } from "react";
import { Tooltip } from "antd";
import { QuestionCircleOutlined, UpOutlined } from "@ant-design/icons";
import classNames from "classnames";
//#region src/components/pisellHierarchicalSummaryList/components/Row.tsx
/** 取当前层级的配置，若无则回退到上一层级（便于 L2+ 共用同一套样式） */
function getLevelCfg(levelConfig, level) {
	for (let lv = level; lv >= 1; lv--) {
		const cfg = levelConfig === null || levelConfig === void 0 ? void 0 : levelConfig[lv];
		if (cfg) return cfg;
	}
}
function normalizeChildren(children) {
	if (!(children === null || children === void 0 ? void 0 : children.length)) return [];
	return children.filter((c) => !c.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 defaultGetNumberValue(item) {
	return typeof item.value === "number" ? item.value : null;
}
function aggregateSum(item, getNumberValue) {
	const children = normalizeChildren(item.children);
	if (!children.length) return 0;
	let sum = 0;
	for (const child of children) {
		var _child$children;
		const self = getNumberValue(child);
		if (typeof self === "number" && !Number.isNaN(self)) sum += self;
		if ((_child$children = child.children) === null || _child$children === void 0 ? void 0 : _child$children.length) sum += aggregateSum(child, getNumberValue);
	}
	return sum;
}
const Row$1 = React.memo((props) => {
	var _ref, _cfg$infoIconStyle$wi, _ref2, _cfg$infoIconStyle$he, _cfg$divider, _cfg$divider$style, _cfg$divider$color, _ref3, _cfg$expandIconStyle$, _ref4, _cfg$expandIconStyle$2, _cfg$expandIconStyle, _cfg$expandIconStyle2;
	const { prefixCls, item, level, index, parentPathKeys, expandedKeySet, toggleExpanded, getKey, levelConfig, indent, rowGap, gaps, maxRenderLevel, maxExpandLevel, aggregate, renderLabel, renderValue, parentHasItemsCount = false } = props;
	const key = useMemo(() => {
		var _item$key;
		return (_item$key = item.key) !== null && _item$key !== void 0 ? _item$key : getKey(item, {
			level,
			index,
			parentPathKeys
		});
	}, [
		getKey,
		index,
		item,
		level,
		parentPathKeys
	]);
	const pathKeys = useMemo(() => [...parentPathKeys, key], [parentPathKeys, key]);
	const children = useMemo(() => normalizeChildren(item.children), [item.children]);
	const hasExpandedContent = item.expandedContent != null;
	const hasChildren = (maxRenderLevel ? level < maxRenderLevel : true) && children.length > 0 || hasExpandedContent;
	const canExpand = hasChildren && (maxExpandLevel ? level < maxExpandLevel : true);
	const expanded = canExpand && expandedKeySet.has(key);
	const cfg = getLevelCfg(levelConfig, level);
	cfg === null || cfg === void 0 || cfg.indent;
	const rowStyleMerged = _objectSpread2(_objectSpread2({}, cfg === null || cfg === void 0 ? void 0 : cfg.rowStyle), item.rowStyle);
	if (level === 1) {
		rowStyleMerged.paddingTop = 20;
		rowStyleMerged.paddingBottom = 20;
	}
	if (level === 2) {
		rowStyleMerged.paddingTop = 20;
		rowStyleMerged.paddingBottom = expanded ? 16 : 20;
	}
	const aggregatedValue = useMemo(() => {
		var _aggregate$mode, _aggregate$mode2, _aggregate$getNumberV;
		if (item.value !== void 0) return void 0;
		if (!aggregate || ((_aggregate$mode = aggregate.mode) !== null && _aggregate$mode !== void 0 ? _aggregate$mode : "none") === "none") return void 0;
		if (((_aggregate$mode2 = aggregate.mode) !== null && _aggregate$mode2 !== void 0 ? _aggregate$mode2 : "none") !== "sum") return void 0;
		const sum = aggregateSum(item, (_aggregate$getNumberV = aggregate.getNumberValue) !== null && _aggregate$getNumberV !== void 0 ? _aggregate$getNumberV : defaultGetNumberValue);
		return aggregate.format ? aggregate.format(sum, {
			item,
			level
		}) : sum;
	}, [
		aggregate,
		item,
		level
	]);
	const ctx = useMemo(() => ({
		level,
		pathKeys,
		expanded,
		hasChildren,
		aggregatedValue
	}), [
		aggregatedValue,
		expanded,
		hasChildren,
		level,
		pathKeys
	]);
	const siblingKeys = useMemo(() => hasExpandedContent ? [] : children.map((c, i) => {
		var _c$key;
		return (_c$key = c.key) !== null && _c$key !== void 0 ? _c$key : getKey(c, {
			level: level + 1,
			index: i,
			parentPathKeys: pathKeys
		});
	}), [
		children,
		getKey,
		hasExpandedContent,
		level,
		pathKeys
	]);
	const onToggle = useCallback(() => {
		if (!canExpand) return;
		toggleExpanded(key, { siblingKeys });
	}, [
		canExpand,
		key,
		siblingKeys,
		toggleExpanded
	]);
	const left = renderLabel ? renderLabel(item, ctx) : item.label;
	const right = renderValue ? renderValue(item, ctx) : aggregatedValue !== null && aggregatedValue !== void 0 ? aggregatedValue : item.value;
	cfg === null || cfg === void 0 || cfg.showDotSeparator;
	const showItemsCount = item.itemsCount !== void 0 && item.itemsCount !== null;
	const showInfo = item.infoTooltip !== void 0 && item.infoTooltip !== null;
	const metaStyle = cfg === null || cfg === void 0 ? void 0 : cfg.metaTextStyle;
	const infoIconStyleObj = (cfg === null || cfg === void 0 ? void 0 : cfg.infoIconStyle) != null ? {
		fontSize: cfg.infoIconStyle.size,
		color: cfg.infoIconStyle.color,
		width: (_ref = (_cfg$infoIconStyle$wi = cfg.infoIconStyle.width) !== null && _cfg$infoIconStyle$wi !== void 0 ? _cfg$infoIconStyle$wi : cfg.infoIconStyle.size) !== null && _ref !== void 0 ? _ref : 16,
		height: (_ref2 = (_cfg$infoIconStyle$he = cfg.infoIconStyle.height) !== null && _cfg$infoIconStyle$he !== void 0 ? _cfg$infoIconStyle$he : cfg.infoIconStyle.size) !== null && _ref2 !== void 0 ? _ref2 : 16,
		display: "inline-flex",
		alignItems: "center",
		justifyContent: "center"
	} : showInfo ? {
		fontSize: 16,
		width: 16,
		height: 16,
		display: "inline-flex",
		alignItems: "center",
		justifyContent: "center"
	} : void 0;
	const leftMeta = (showItemsCount || showInfo) && /* @__PURE__ */ React.createElement("span", {
		className: `${prefixCls}-row-meta`,
		style: _objectSpread2(_objectSpread2({}, metaStyle), (gaps === null || gaps === void 0 ? void 0 : gaps.meta) !== void 0 ? { gap: gaps.meta } : void 0)
	}, showItemsCount ? /* @__PURE__ */ React.createElement("span", {
		className: `${prefixCls}-row-items-count`,
		style: metaStyle
	}, item.itemsCount) : null, showInfo ? /* @__PURE__ */ React.createElement(Tooltip, { title: item.infoTooltip }, /* @__PURE__ */ React.createElement("span", {
		className: `${prefixCls}-row-info`,
		"aria-label": "info",
		style: infoIconStyleObj
	}, /* @__PURE__ */ React.createElement(QuestionCircleOutlined, null))) : null);
	const nodeStyle = _objectSpread2(_objectSpread2(_objectSpread2({}, level <= 2 ? expanded ? { paddingBottom: 20 } : {} : { marginTop: rowGap }), level === 2 ? {
		paddingLeft: 20,
		paddingRight: 20
	} : {}), level === 2 && (cfg === null || cfg === void 0 || (_cfg$divider = cfg.divider) === null || _cfg$divider === void 0 ? void 0 : _cfg$divider.show) ? { borderTop: `1px ${(_cfg$divider$style = cfg.divider.style) !== null && _cfg$divider$style !== void 0 ? _cfg$divider$style : "solid"} ${(_cfg$divider$color = cfg.divider.color) !== null && _cfg$divider$color !== void 0 ? _cfg$divider$color : "#D0D5DD"}` } : {});
	if (hasChildren && (item.label === "" || item.label === null || item.label === void 0) && item.value === void 0) return /* @__PURE__ */ React.createElement(React.Fragment, null, children.map((child, childIndex) => {
		var _child$key;
		return /* @__PURE__ */ React.createElement(Row$1, _objectSpread2(_objectSpread2({ key: (_child$key = child.key) !== null && _child$key !== void 0 ? _child$key : getKey(child, {
			level,
			index: childIndex,
			parentPathKeys: pathKeys
		}) }, props), {}, {
			item: child,
			level,
			index: childIndex,
			parentPathKeys: pathKeys
		}));
	}));
	return /* @__PURE__ */ React.createElement("div", {
		className: `${prefixCls}-node`,
		style: nodeStyle
	}, /* @__PURE__ */ React.createElement("div", {
		className: classNames(`${prefixCls}-row`, cfg === null || cfg === void 0 ? void 0 : cfg.rowClassName, item.rowClassName, {
			[`${prefixCls}-row-expandable`]: canExpand,
			[`${prefixCls}-row-expanded`]: expanded
		}),
		style: _objectSpread2(_objectSpread2({}, rowStyleMerged), (gaps === null || gaps === void 0 ? void 0 : gaps.row) !== void 0 ? { gap: gaps.row } : void 0),
		onClick: onToggle,
		role: canExpand ? "button" : void 0,
		tabIndex: canExpand ? 0 : void 0,
		onKeyDown: (e) => {
			if (!canExpand) return;
			if (e.key === "Enter" || e.key === " ") {
				e.preventDefault();
				onToggle();
			}
		}
	}, /* @__PURE__ */ React.createElement("div", {
		className: `${prefixCls}-row-left`,
		style: (gaps === null || gaps === void 0 ? void 0 : gaps.rowLeft) !== void 0 ? { gap: gaps.rowLeft } : void 0
	}, parentHasItemsCount && !showItemsCount ? /* @__PURE__ */ React.createElement("span", {
		className: `${prefixCls}-row-bullet`,
		style: {
			color: "#667085",
			flexShrink: 0
		},
		"aria-hidden": true
	}, "•") : null, /* @__PURE__ */ React.createElement("span", {
		className: `${prefixCls}-row-label`,
		style: parentHasItemsCount ? _objectSpread2(_objectSpread2({}, cfg === null || cfg === void 0 ? void 0 : cfg.labelTextStyle), {}, {
			color: "#667085",
			fontWeight: 400
		}) : cfg === null || cfg === void 0 ? void 0 : cfg.labelTextStyle,
		title: typeof item.label === "string" ? item.label : void 0
	}, left), leftMeta, /* @__PURE__ */ React.createElement("span", {
		style: {
			flex: 1,
			minWidth: 0
		},
		"aria-hidden": true
	})), /* @__PURE__ */ React.createElement("div", {
		className: `${prefixCls}-row-right`,
		style: (gaps === null || gaps === void 0 ? void 0 : gaps.rowRight) !== void 0 ? { gap: gaps.rowRight } : void 0
	}, /* @__PURE__ */ React.createElement("span", {
		className: `${prefixCls}-row-value`,
		style: parentHasItemsCount ? _objectSpread2(_objectSpread2({}, cfg === null || cfg === void 0 ? void 0 : cfg.valueTextStyle), {}, {
			color: "#667085",
			fontWeight: 400
		}) : cfg === null || cfg === void 0 ? void 0 : cfg.valueTextStyle
	}, right), item.extra ? /* @__PURE__ */ React.createElement("span", { className: `${prefixCls}-row-extra` }, item.extra) : null, canExpand ? /* @__PURE__ */ React.createElement("span", {
		className: `${prefixCls}-row-expand-icon`,
		"aria-label": expanded ? "collapse" : "expand",
		style: (cfg === null || cfg === void 0 ? void 0 : cfg.expandIconStyle) ? {
			width: (_ref3 = (_cfg$expandIconStyle$ = cfg.expandIconStyle.width) !== null && _cfg$expandIconStyle$ !== void 0 ? _cfg$expandIconStyle$ : cfg.expandIconStyle.size) !== null && _ref3 !== void 0 ? _ref3 : 24,
			height: (_ref4 = (_cfg$expandIconStyle$2 = cfg.expandIconStyle.height) !== null && _cfg$expandIconStyle$2 !== void 0 ? _cfg$expandIconStyle$2 : cfg.expandIconStyle.size) !== null && _ref4 !== void 0 ? _ref4 : 24
		} : void 0
	}, /* @__PURE__ */ React.createElement(UpOutlined, { style: _objectSpread2(_objectSpread2({ transform: expanded ? "rotate(0deg)" : "rotate(180deg)" }, (cfg === null || cfg === void 0 || (_cfg$expandIconStyle = cfg.expandIconStyle) === null || _cfg$expandIconStyle === void 0 ? void 0 : _cfg$expandIconStyle.color) ? { color: cfg.expandIconStyle.color } : null), (cfg === null || cfg === void 0 || (_cfg$expandIconStyle2 = cfg.expandIconStyle) === null || _cfg$expandIconStyle2 === void 0 ? void 0 : _cfg$expandIconStyle2.size) ? { fontSize: cfg.expandIconStyle.size } : null) })) : null)), hasChildren && expanded ? /* @__PURE__ */ React.createElement("div", {
		className: `${prefixCls}-children`,
		style: {
			paddingLeft: 0,
			marginTop: 8
		}
	}, hasExpandedContent ? item.expandedContent : children.map((child, childIndex) => {
		var _child$key2;
		return /* @__PURE__ */ React.createElement(Row$1, _objectSpread2(_objectSpread2({ key: (_child$key2 = child.key) !== null && _child$key2 !== void 0 ? _child$key2 : getKey(child, {
			level: level + 1,
			index: childIndex,
			parentPathKeys: pathKeys
		}) }, props), {}, {
			item: child,
			level: level + 1,
			index: childIndex,
			parentPathKeys: pathKeys,
			parentHasItemsCount: showItemsCount || parentHasItemsCount
		}));
	})) : null);
});
Row$1.displayName = "PisellHierarchicalSummaryListRow";
//#endregion
export { Row$1 as Row };
