require("../../../_virtual/_rolldown/runtime.js");
let _pisell_utils = require("@pisell/utils");
//#region src/plus/pisellFinancialSummary/utils/buildSummaryItems.ts
function formatAmount(amount, options) {
	var _options$currencySymb, _options$amountPrecis, _options$useThousands, _options$hideDecimalF;
	if (amount == null || typeof amount === "number" && Number.isNaN(amount)) {
		var _options$missingValue;
		return (_options$missingValue = options.missingValuePlaceholder) !== null && _options$missingValue !== void 0 ? _options$missingValue : "—";
	}
	const symbol = (_options$currencySymb = options.currencySymbol) !== null && _options$currencySymb !== void 0 ? _options$currencySymb : "";
	return (0, _pisell_utils.formatAmountWithOptions)(amount, symbol, {
		precision: (_options$amountPrecis = options.amountPrecision) !== null && _options$amountPrecis !== void 0 ? _options$amountPrecis : 2,
		showCurrencySymbol: !!symbol,
		useThousandsSeparator: (_options$useThousands = options.useThousandsSeparator) !== null && _options$useThousands !== void 0 ? _options$useThousands : false,
		hideDecimalForWholeNumbers: (_options$hideDecimalF = options.hideDecimalForWholeNumbers) !== null && _options$hideDecimalF !== void 0 ? _options$hideDecimalF : true
	});
}
function lineItemToSummary(item, options, level) {
	var _options$missingValue2, _item$children, _item$key;
	if (options.hideZeroAmountRows && item.value === 0) return null;
	const valueDisplay = item.value != null && !Number.isNaN(Number(item.value)) ? formatAmount(Number(item.value), options) : (_options$missingValue2 = options.missingValuePlaceholder) !== null && _options$missingValue2 !== void 0 ? _options$missingValue2 : "—";
	const children = ((_item$children = item.children) !== null && _item$children !== void 0 ? _item$children : []).map((c) => lineItemToSummary(c, options, level + 1)).filter((x) => x != null);
	return {
		key: (_item$key = item.key) !== null && _item$key !== void 0 ? _item$key : `line-${level}-${item.label}`,
		label: item.label,
		value: valueDisplay,
		children: children.length > 0 ? children : void 0,
		itemsCount: options.showItemsCount ? item.itemsCount : void 0,
		infoTooltip: options.showInfoIcon ? item.infoTooltip : void 0
	};
}
function totalBlockToChildren(block, options) {
	var _block$children;
	if (!(block === null || block === void 0 || (_block$children = block.children) === null || _block$children === void 0 ? void 0 : _block$children.length)) return [];
	return block.children.map((c) => lineItemToSummary(c, options, 2)).filter((x) => x != null);
}
function sumLineItems(items) {
	let sum = 0;
	for (const item of items) {
		var _item$children2;
		if (typeof item.value === "number" && !Number.isNaN(item.value)) sum += item.value;
		if ((_item$children2 = item.children) === null || _item$children2 === void 0 ? void 0 : _item$children2.length) sum += sumLineItems(item.children);
	}
	return sum;
}
function paymentToItem(p, options) {
	var _p$methodName, _p$time, _p$key;
	const labelParts = [(_p$methodName = p.methodName) !== null && _p$methodName !== void 0 ? _p$methodName : "", (_p$time = p.time) !== null && _p$time !== void 0 ? _p$time : ""].filter(Boolean);
	const label = labelParts.length > 0 ? labelParts.join(" · ") : void 0;
	return {
		key: (_p$key = p.key) !== null && _p$key !== void 0 ? _p$key : `paid-${p.amount}-${p.methodName}`,
		label: label !== null && label !== void 0 ? label : "—",
		value: formatAmount(p.amount, options),
		extra: p.detail
	};
}
function refundToItem(r, options) {
	var _r$key, _r$label;
	return {
		key: (_r$key = r.key) !== null && _r$key !== void 0 ? _r$key : `refund-${r.amount}-${r.label}`,
		label: (_r$label = r.label) !== null && _r$label !== void 0 ? _r$label : "—",
		value: formatAmount(r.amount, options),
		extra: r.detail
	};
}
/**
* 将 FinancialSummaryData 转为 PisellHierarchicalSummaryList 所需的 items
*/
function buildSummaryItems(data, options) {
	var _data$paid, _data$refund;
	const items = [];
	const getTitle = (key, override) => override !== void 0 ? override : options.getText(key);
	const secondary = [];
	if (options.showTotal !== false && data.total) {
		var _data$total$children;
		const totalChildren = totalBlockToChildren(data.total, options);
		const totalValueRaw = data.total.value != null && !Number.isNaN(Number(data.total.value)) ? Number(data.total.value) : ((_data$total$children = data.total.children) === null || _data$total$children === void 0 ? void 0 : _data$total$children.length) ? sumLineItems(data.total.children) : void 0;
		const totalValue = totalValueRaw !== void 0 && !Number.isNaN(totalValueRaw) ? formatAmount(totalValueRaw, options) : void 0;
		secondary.push({
			key: "total",
			label: getTitle("pisell2.financialSummary.total", options.titleTotal),
			value: totalValue,
			children: totalChildren.length > 0 ? totalChildren : void 0
		});
	}
	if (!options.skipPaidAndRefund && options.showPaid !== false && ((_data$paid = data.paid) === null || _data$paid === void 0 ? void 0 : _data$paid.length)) {
		const paidChildren = data.paid.map((p) => paymentToItem(p, options));
		secondary.push({
			key: "paid",
			label: getTitle("pisell2.financialSummary.paid", options.titlePaid),
			children: paidChildren
		});
	}
	if (!options.skipPaidAndRefund && options.showRefund !== false && ((_data$refund = data.refund) === null || _data$refund === void 0 ? void 0 : _data$refund.length)) {
		const refundChildren = data.refund.map((r) => refundToItem(r, options));
		secondary.push({
			key: "refund",
			label: getTitle("pisell2.financialSummary.refund", options.titleRefund),
			children: refundChildren
		});
	}
	const groupNode = secondary.length > 0 ? {
		key: "summary-sections",
		label: "",
		value: void 0,
		children: secondary
	} : {
		key: "summary-sections",
		label: "",
		value: void 0,
		children: []
	};
	items.push({
		key: "balance-due",
		label: getTitle("pisell2.financialSummary.balanceDue", options.titleBalanceDue),
		value: formatAmount(data.balanceDue, options),
		children: secondary.length > 0 ? [groupNode] : void 0
	});
	return items;
}
//#endregion
exports.buildSummaryItems = buildSummaryItems;
