import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import locales_default from "./locales.js";
import React, { useEffect, useMemo } from "react";
import { Divider } from "antd";
import { locales } from "@pisell/utils";
import { Iconfont, PisellText, Tooltip as Tooltip$1 } from "@pisell/materials";
import classNames from "classnames";
import "./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: locales.getText("pisell2.subtotal.deposit"),
		total: locales.getText("pisell2.subtotal.total"),
		sub_total: locales.getText("pisell2.subtotal.subtotal"),
		tax: locales.getText("pisell2.subtotal.tax"),
		delivery_fee: locales.getText("pisell2.subtotal.delivery-fee"),
		discount: locales.getText("pisell2.subtotal.discount"),
		point_deduction: locales.getText("pisell2.subtotal.point-deduction"),
		member_deduction: 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 = _objectWithoutProperties(item, _excluded);
	const taxText = 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 + " " + locales.getText("pisell2.subtotal.included");
		else str = str + " " + locales.getText("pisell2.subtotal.not-contain");
		return str;
	}, [tax]);
	const renderLabel = useMemo(() => {
		if (label) return label;
		if (key === "sub_total" && count !== void 0) return /* @__PURE__ */ React.createElement("span", null, /* @__PURE__ */ React.createElement("span", null, `${labelMaps[key]}`), /* @__PURE__ */ React.createElement("span", { style: {
			color: "#667085",
			paddingLeft: "8px"
		} }, `${count || 0} ${locales.getText("pisell2.subtotal.items")}`));
		if (key === "tax") return /* @__PURE__ */ React.createElement("span", null, /* @__PURE__ */ React.createElement("span", null, `${labelMaps[key]}`), /* @__PURE__ */ React.createElement(Tooltip$1, { title: taxText }, /* @__PURE__ */ React.createElement(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.createElement("div", _objectSpread2({ className: classNames("pisell-lowcode__subtotal-item", { "pisell-lowcode__subtotal-item-bold": bold || (item === null || item === void 0 ? void 0 : item.bold) }) }, rest), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__subtotal-item-label" }, renderLabel), /* @__PURE__ */ React.createElement(PisellText.Amount, {
		value,
		className: "pisell-lowcode__subtotal-item-value"
	}));
};
const Describe = (props) => {
	const { describe } = props;
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Divider, { className: "pisell-lowcode__subtotal-divider" }), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__subtotal-describe" }, describe));
};
const SubTotal = (props) => {
	var _context$engine;
	const { list = [], mode = "column1", describe = "", children } = props, rest = _objectWithoutProperties(props, _excluded2);
	const context = useEngineContext();
	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";
	locales.init(locales_default, locale);
	const labelMaps = useMemo(() => {
		return getLabelMaps();
	}, [locale]);
	const depositItem = useMemo(() => {
		return list.find((item) => item.key === "deposit" && !(item === null || item === void 0 ? void 0 : item.hidden));
	}, [list]);
	const totalItem = useMemo(() => {
		return list.find((item) => item.key === "total");
	}, [list]);
	const headerItem = useMemo(() => {
		if (depositItem) return depositItem;
		if (totalItem) return totalItem;
		return null;
	}, [depositItem, totalItem]);
	const mapData = 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
	]);
	useEffect(() => {
		document.body.id = "body";
	}, []);
	return /* @__PURE__ */ React.createElement("div", _objectSpread2({ className: "pisell-lowcode__subtotal-wrap" }, rest), headerItem && /* @__PURE__ */ React.createElement(Item, {
		key: headerItem.key,
		item: headerItem,
		bold: true,
		labelMaps
	}), depositItem ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Divider, { className: "pisell-lowcode__subtotal-divider" }), totalItem ? /* @__PURE__ */ React.createElement(Item, {
		key: totalItem.key,
		item: totalItem,
		labelMaps
	}) : null) : null, /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__subtotal-list" }, mapData.map((items, index) => {
		const isLast = index === mapData.length - 1;
		return /* @__PURE__ */ React.createElement(React.Fragment, { key: index }, /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__subtotal-item-block" }, /* @__PURE__ */ React.createElement("div", {
			key: index,
			className: "pisell-lowcode__subtotal-item-wrap"
		}, items.map((d) => /* @__PURE__ */ React.createElement(Item, {
			key: d.key,
			item: d,
			labelMaps
		})))), mode === "column2" && !isLast ? /* @__PURE__ */ React.createElement(Divider, {
			type: "vertical",
			style: { height: "auto" }
		}) : null);
	})), describe ? /* @__PURE__ */ React.createElement(Describe, { describe }) : null, children ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Divider, { className: "pisell-lowcode__subtotal-divider" }), children) : null);
};
//#endregion
export { SubTotal as default };
