const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_buildSummaryItems = require("./utils/buildSummaryItems.js");
const require_Collapse = require("./components/Collapse/Collapse.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./PisellFinancialSummary.less");
//#region src/plus/pisellFinancialSummary/PisellFinancialSummary.tsx
const PREFIX_CLS = "pisell-financial-summary";
/**
* 层级约定：首行「应付余额」为主标题（level 1），「费用合计 / 付款历史 / 退款」为二级标题（level 2），其下为明细（level 3+）。
* 付款历史、退款展开后内置使用 Collapse 展示每条记录。
*/
const FIGMA_GAPS = {
	row: 8,
	rowLeft: 8,
	meta: 4,
	rowRight: 6
};
const DEFAULT_LEVEL_CONFIG = {
	1: {
		labelTextStyle: {
			fontSize: 16,
			fontWeight: 700,
			color: "#1B1B1B",
			lineHeight: "24px"
		},
		valueTextStyle: {
			fontSize: 16,
			fontWeight: 700,
			color: "#DC6803",
			lineHeight: "24px"
		},
		expandIconStyle: {
			size: 16,
			width: 16,
			height: 16
		}
	},
	2: {
		labelTextStyle: {
			fontSize: 16,
			fontWeight: 700,
			color: "#1B1B1B",
			lineHeight: "24px"
		},
		valueTextStyle: {
			fontSize: 16,
			fontWeight: 700,
			color: "#DC6803",
			lineHeight: "24px"
		},
		expandIconStyle: {
			size: 16,
			width: 16,
			height: 16
		},
		divider: {
			show: true,
			color: "#D0D5DD",
			style: "solid"
		},
		showDotSeparator: false
	},
	3: {
		labelTextStyle: {
			fontSize: 16,
			fontWeight: 500,
			color: "#000000",
			lineHeight: "24px"
		},
		valueTextStyle: {
			fontSize: 16,
			fontWeight: 500,
			color: "#000000",
			lineHeight: "24px"
		},
		metaTextStyle: {
			fontSize: 16,
			fontWeight: 400,
			color: "#667085",
			lineHeight: "24px"
		},
		infoIconStyle: {
			size: 16,
			width: 16,
			height: 16
		}
	}
};
const PisellFinancialSummary = react.default.memo(({ data, showTotal = true, showPaid = true, showRefund = true, showItemsCount = true, showInfoIcon = true, hideZeroAmountRows = false, titleBalanceDue, titleTotal, titlePaid, titleRefund, currencySymbol = "", amountPrecision = 2, useThousandsSeparator = false, hideDecimalForWholeNumbers = true, missingValuePlaceholder = "—", defaultExpandedLevel = 0, className, style }) => {
	var _data$paid$length, _data$paid2, _data$refund$length, _data$refund2;
	const formatOpts = (0, react.useMemo)(() => ({
		precision: amountPrecision !== null && amountPrecision !== void 0 ? amountPrecision : 2,
		showCurrencySymbol: !!currencySymbol,
		useThousandsSeparator: useThousandsSeparator !== null && useThousandsSeparator !== void 0 ? useThousandsSeparator : false,
		hideDecimalForWholeNumbers: hideDecimalForWholeNumbers !== null && hideDecimalForWholeNumbers !== void 0 ? hideDecimalForWholeNumbers : true
	}), [
		amountPrecision,
		currencySymbol,
		useThousandsSeparator,
		hideDecimalForWholeNumbers
	]);
	const formatAmountDisplay = (0, react.useCallback)((amount) => (0, _pisell_utils.formatAmountWithOptions)(amount, currencySymbol !== null && currencySymbol !== void 0 ? currencySymbol : "", formatOpts), [currencySymbol, formatOpts]);
	const getTitle = (0, react.useCallback)((key, override) => override !== void 0 ? override : _pisell_utils.locales.getText(key), [
		titleBalanceDue,
		titleTotal,
		titlePaid,
		titleRefund
	]);
	const paidSum = (0, react.useMemo)(() => {
		var _data$paid;
		return ((_data$paid = data.paid) !== null && _data$paid !== void 0 ? _data$paid : []).reduce((s, p) => s + p.amount, 0);
	}, [data.paid]);
	const refundSum = (0, react.useMemo)(() => {
		var _data$refund;
		return ((_data$refund = data.refund) !== null && _data$refund !== void 0 ? _data$refund : []).reduce((s, r) => s + r.amount, 0);
	}, [data.refund]);
	const useCollapseForPaidRefund = showPaid !== false && ((_data$paid$length = (_data$paid2 = data.paid) === null || _data$paid2 === void 0 ? void 0 : _data$paid2.length) !== null && _data$paid$length !== void 0 ? _data$paid$length : 0) > 0 || showRefund !== false && ((_data$refund$length = (_data$refund2 = data.refund) === null || _data$refund2 === void 0 ? void 0 : _data$refund2.length) !== null && _data$refund$length !== void 0 ? _data$refund$length : 0) > 0;
	const items = (0, react.useMemo)(() => {
		var _root$children, _data$paid$length2, _data$paid3, _data$refund$length2, _data$refund3;
		const base = require_buildSummaryItems.buildSummaryItems(data, {
			currencySymbol,
			amountPrecision,
			useThousandsSeparator,
			hideDecimalForWholeNumbers,
			showItemsCount,
			showInfoIcon,
			hideZeroAmountRows,
			missingValuePlaceholder,
			getText: (key) => _pisell_utils.locales.getText(key),
			titleBalanceDue,
			titleTotal,
			titlePaid,
			titleRefund,
			showTotal,
			showPaid: useCollapseForPaidRefund ? false : showPaid,
			showRefund: useCollapseForPaidRefund ? false : showRefund,
			skipPaidAndRefund: useCollapseForPaidRefund
		});
		if (!useCollapseForPaidRefund) return base;
		const root = base.slice()[0];
		const group = root === null || root === void 0 || (_root$children = root.children) === null || _root$children === void 0 ? void 0 : _root$children[0];
		if (!(group === null || group === void 0 ? void 0 : group.children)) return base;
		const newChildren = group.children.slice();
		if (showPaid !== false && ((_data$paid$length2 = (_data$paid3 = data.paid) === null || _data$paid3 === void 0 ? void 0 : _data$paid3.length) !== null && _data$paid$length2 !== void 0 ? _data$paid$length2 : 0) > 0) {
			var _data$paid4;
			newChildren.push({
				key: "paid",
				label: getTitle("pisell2.financialSummary.paid", titlePaid),
				value: formatAmountDisplay(paidSum),
				expandedContent: /* @__PURE__ */ react.default.createElement("div", {
					className: `${PREFIX_CLS}-collapse-list`,
					style: { marginTop: 8 }
				}, ((_data$paid4 = data.paid) !== null && _data$paid4 !== void 0 ? _data$paid4 : []).map((p, i) => {
					var _p$key;
					return /* @__PURE__ */ react.default.createElement(PaymentCollapseItem, {
						key: (_p$key = p.key) !== null && _p$key !== void 0 ? _p$key : i,
						item: p,
						formatAmount: formatAmountDisplay
					});
				}))
			});
		}
		if (showRefund !== false && ((_data$refund$length2 = (_data$refund3 = data.refund) === null || _data$refund3 === void 0 ? void 0 : _data$refund3.length) !== null && _data$refund$length2 !== void 0 ? _data$refund$length2 : 0) > 0) {
			var _data$refund4;
			newChildren.push({
				key: "refund",
				label: getTitle("pisell2.financialSummary.refund", titleRefund),
				value: formatAmountDisplay(refundSum),
				expandedContent: /* @__PURE__ */ react.default.createElement("div", {
					className: `${PREFIX_CLS}-collapse-list`,
					style: { marginTop: 8 }
				}, ((_data$refund4 = data.refund) !== null && _data$refund4 !== void 0 ? _data$refund4 : []).map((r, i) => {
					var _r$key;
					return /* @__PURE__ */ react.default.createElement(RefundCollapseItem, {
						key: (_r$key = r.key) !== null && _r$key !== void 0 ? _r$key : i,
						item: r,
						formatAmount: formatAmountDisplay
					});
				}))
			});
		}
		return [require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, root), {}, { children: [require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, group), {}, { children: newChildren })] })];
	}, [
		data,
		currencySymbol,
		amountPrecision,
		useThousandsSeparator,
		hideDecimalForWholeNumbers,
		showItemsCount,
		showInfoIcon,
		hideZeroAmountRows,
		missingValuePlaceholder,
		titleBalanceDue,
		titleTotal,
		titlePaid,
		titleRefund,
		showTotal,
		showPaid,
		showRefund,
		getTitle,
		formatAmountDisplay,
		paidSum,
		refundSum,
		useCollapseForPaidRefund
	]);
	return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)(PREFIX_CLS, className),
		style
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellHierarchicalSummaryList, {
		items,
		indent: 16,
		rowGap: 8,
		padding: 20,
		gaps: FIGMA_GAPS,
		levelConfig: DEFAULT_LEVEL_CONFIG,
		defaultExpandedLevel
	}));
});
function paymentToCollapseProps(p, formatAmount) {
	var _p$bodyRows$length, _p$bodyRows, _p$bodyRows2, _p$methodName;
	const bodyRows = ((_p$bodyRows$length = (_p$bodyRows = p.bodyRows) === null || _p$bodyRows === void 0 ? void 0 : _p$bodyRows.length) !== null && _p$bodyRows$length !== void 0 ? _p$bodyRows$length : 0) > 0 ? ((_p$bodyRows2 = p.bodyRows) !== null && _p$bodyRows2 !== void 0 ? _p$bodyRows2 : []).map((r) => {
		var _r$amount;
		return {
			label: r.label,
			amount: (_r$amount = r.amount) !== null && _r$amount !== void 0 ? _r$amount : formatAmount(p.amount),
			action: r.action
		};
	}) : [{
		label: (_p$methodName = p.methodName) !== null && _p$methodName !== void 0 ? _p$methodName : "—",
		amount: formatAmount(p.amount),
		action: {
			label: "Void",
			onClick: void 0
		}
	}];
	return {
		headMeta: {
			time: p.time,
			date: p.date,
			customer: p.customer,
			channel: p.channel
		},
		headAmount: formatAmount(p.amount),
		bodyRows
	};
}
function PaymentCollapseItem({ item, formatAmount }) {
	const { headMeta, headAmount, bodyRows } = (0, react.useMemo)(() => paymentToCollapseProps(item, formatAmount), [item, formatAmount]);
	return /* @__PURE__ */ react.default.createElement(require_Collapse.Collapse, {
		headMeta,
		headAmount,
		defaultExpanded: true,
		bodyRows
	});
}
function RefundCollapseItem({ item, formatAmount }) {
	var _item$bodyRows$length, _item$bodyRows, _item$bodyRows2, _item$label;
	const bodyRows = ((_item$bodyRows$length = (_item$bodyRows = item.bodyRows) === null || _item$bodyRows === void 0 ? void 0 : _item$bodyRows.length) !== null && _item$bodyRows$length !== void 0 ? _item$bodyRows$length : 0) > 0 ? (_item$bodyRows2 = item.bodyRows) !== null && _item$bodyRows2 !== void 0 ? _item$bodyRows2 : [] : [{
		label: (_item$label = item.label) !== null && _item$label !== void 0 ? _item$label : "—",
		amount: formatAmount(item.amount),
		action: {
			label: "Void",
			onClick: void 0
		}
	}];
	return /* @__PURE__ */ react.default.createElement(require_Collapse.Collapse, {
		headMeta: {
			time: item.time,
			date: item.date,
			customer: item.customer,
			channel: item.channel
		},
		headAmount: formatAmount(item.amount),
		defaultExpanded: true,
		bodyRows
	});
}
PisellFinancialSummary.displayName = "PisellFinancialSummary";
//#endregion
exports.PisellFinancialSummary = PisellFinancialSummary;
