const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_locales = require("./locales.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./index.less");
//#region src/components/subTotal/index.tsx
const _excluded = [
	"label",
	"key",
	"count",
	"value",
	"tax",
	"hidden"
], _excluded2 = [
	"list",
	"mode",
	"describe",
	"children"
];
/**
* 获取标签映射
*/
const getLabelMaps = () => {
	return {
		deposit: _pisell_utils.locales.getText("pisell2.subtotal.deposit"),
		total: _pisell_utils.locales.getText("pisell2.subtotal.total"),
		sub_total: _pisell_utils.locales.getText("pisell2.subtotal.subtotal"),
		tax: _pisell_utils.locales.getText("pisell2.subtotal.tax"),
		delivery_fee: _pisell_utils.locales.getText("pisell2.subtotal.delivery-fee"),
		discount: _pisell_utils.locales.getText("pisell2.subtotal.discount"),
		point_deduction: _pisell_utils.locales.getText("pisell2.subtotal.point-deduction"),
		member_deduction: _pisell_utils.locales.getText("pisell2.subtotal.member-deduction")
	};
};
const groupByMod = (arr, groupCount) => {
	if (groupCount < 1) throw new Error("Group count must be greater than 0");
	if (arr.length === 0) return Array(groupCount).fill([]);
	const result = Array(groupCount).fill(null).map(() => []);
	return arr.reduce((acc, item, index) => {
		acc[index % groupCount].push(item);
		return acc;
	}, result);
};
const Item = (props) => {
	const { item, bold, labelMaps } = props;
	const { label, key, count, value, tax, hidden } = item, rest = require_objectWithoutProperties._objectWithoutProperties(item, _excluded);
	const taxText = (0, react.useMemo)(() => {
		if (!tax) return "";
		let str = "";
		str = str + (tax === null || tax === void 0 ? void 0 : tax.title);
		str = str + " " + Number(tax.rate) * 100 + "%";
		if (tax === null || tax === void 0 ? void 0 : tax.is_price_include_tax) str = str + " " + _pisell_utils.locales.getText("pisell2.subtotal.included");
		else str = str + " " + _pisell_utils.locales.getText("pisell2.subtotal.not-contain");
		return str;
	}, [tax]);
	const renderLabel = (0, react.useMemo)(() => {
		if (label) return label;
		if (key === "sub_total" && count !== void 0) return /* @__PURE__ */ react.default.createElement("span", null, /* @__PURE__ */ react.default.createElement("span", null, `${labelMaps[key]}`), /* @__PURE__ */ react.default.createElement("span", { style: {
			color: "#667085",
			paddingLeft: "8px"
		} }, `${count || 0} ${_pisell_utils.locales.getText("pisell2.subtotal.items")}`));
		if (key === "tax") return /* @__PURE__ */ react.default.createElement("span", null, /* @__PURE__ */ react.default.createElement("span", null, `${labelMaps[key]}`), /* @__PURE__ */ react.default.createElement(_pisell_materials.Tooltip, { title: taxText }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
			style: {
				color: "#667085",
				paddingLeft: "8px",
				fontSize: "16px"
			},
			type: "pisell2-help-circle"
		})));
		return labelMaps[key];
	}, [
		label,
		key,
		count,
		taxText,
		labelMaps
	]);
	if (hidden) return null;
	return /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2({ className: (0, classnames.default)("pisell-lowcode__subtotal-item", { "pisell-lowcode__subtotal-item-bold": bold || (item === null || item === void 0 ? void 0 : item.bold) }) }, rest), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__subtotal-item-label" }, renderLabel), /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellText.Amount, {
		value,
		className: "pisell-lowcode__subtotal-item-value"
	}));
};
const Describe = (props) => {
	const { describe } = props;
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(antd.Divider, { className: "pisell-lowcode__subtotal-divider" }), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__subtotal-describe" }, describe));
};
const SubTotal = (props) => {
	var _context$engine;
	const { list = [], mode = "column1", describe = "", children } = props, rest = require_objectWithoutProperties._objectWithoutProperties(props, _excluded2);
	const context = require_useEngineContext.default();
	const locale = (context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.locale) || "en";
	_pisell_utils.locales.init(require_locales.default, locale);
	const labelMaps = (0, react.useMemo)(() => {
		return getLabelMaps();
	}, [locale]);
	const depositItem = (0, react.useMemo)(() => {
		return list.find((item) => item.key === "deposit" && !(item === null || item === void 0 ? void 0 : item.hidden));
	}, [list]);
	const totalItem = (0, react.useMemo)(() => {
		return list.find((item) => item.key === "total");
	}, [list]);
	const headerItem = (0, react.useMemo)(() => {
		if (depositItem) return depositItem;
		if (totalItem) return totalItem;
		return null;
	}, [depositItem, totalItem]);
	const mapData = (0, react.useMemo)(() => {
		if (list.length === 0) return [];
		let filterList = [];
		if (depositItem) filterList.push("deposit");
		if (totalItem) filterList.push("total");
		const groupCount = mode === "column2" ? 2 : 1;
		return groupByMod(list.filter((d) => !filterList.includes(d.key)), groupCount);
	}, [
		list,
		depositItem,
		totalItem
	]);
	(0, react.useEffect)(() => {
		document.body.id = "body";
	}, []);
	return /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2({ className: "pisell-lowcode__subtotal-wrap" }, rest), headerItem && /* @__PURE__ */ react.default.createElement(Item, {
		key: headerItem.key,
		item: headerItem,
		bold: true,
		labelMaps
	}), depositItem ? /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(antd.Divider, { className: "pisell-lowcode__subtotal-divider" }), totalItem ? /* @__PURE__ */ react.default.createElement(Item, {
		key: totalItem.key,
		item: totalItem,
		labelMaps
	}) : null) : null, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__subtotal-list" }, mapData.map((items, index) => {
		const isLast = index === mapData.length - 1;
		return /* @__PURE__ */ react.default.createElement(react.default.Fragment, { key: index }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__subtotal-item-block" }, /* @__PURE__ */ react.default.createElement("div", {
			key: index,
			className: "pisell-lowcode__subtotal-item-wrap"
		}, items.map((d) => /* @__PURE__ */ react.default.createElement(Item, {
			key: d.key,
			item: d,
			labelMaps
		})))), mode === "column2" && !isLast ? /* @__PURE__ */ react.default.createElement(antd.Divider, {
			type: "vertical",
			style: { height: "auto" }
		}) : null);
	})), describe ? /* @__PURE__ */ react.default.createElement(Describe, { describe }) : null, children ? /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(antd.Divider, { className: "pisell-lowcode__subtotal-divider" }), children) : null);
};
//#endregion
exports.default = SubTotal;
