var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/subTotal/index.tsx
var subTotal_exports = {};
__export(subTotal_exports, {
  default: () => subTotal_default
});
module.exports = __toCommonJS(subTotal_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_index = require("./index.less");
var import_locales = __toESM(require("./locales"));
var import_classnames = __toESM(require("classnames"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var getLabelMaps = () => {
  return {
    deposit: import_utils.locales.getText("pisell2.subtotal.deposit"),
    total: import_utils.locales.getText("pisell2.subtotal.total"),
    sub_total: import_utils.locales.getText("pisell2.subtotal.subtotal"),
    tax: import_utils.locales.getText("pisell2.subtotal.tax"),
    delivery_fee: import_utils.locales.getText("pisell2.subtotal.delivery-fee"),
    discount: import_utils.locales.getText("pisell2.subtotal.discount"),
    point_deduction: import_utils.locales.getText("pisell2.subtotal.point-deduction"),
    member_deduction: import_utils.locales.getText("pisell2.subtotal.member-deduction")
  };
};
var 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) => {
    const groupIndex = index % groupCount;
    acc[groupIndex].push(item);
    return acc;
  }, result);
};
var Item = (props) => {
  const { item, bold, labelMaps } = props;
  const { label, key, count, value, tax, hidden, ...rest } = item;
  const taxText = (0, import_react.useMemo)(() => {
    if (!tax) {
      return "";
    }
    let str = "";
    str = str + (tax == null ? void 0 : tax.title);
    str = str + " " + Number(tax.rate) * 100 + "%";
    if (tax == null ? void 0 : tax.is_price_include_tax) {
      str = str + " " + import_utils.locales.getText("pisell2.subtotal.included");
    } else {
      str = str + " " + import_utils.locales.getText("pisell2.subtotal.not-contain");
    }
    return str;
  }, [tax]);
  const renderLabel = (0, import_react.useMemo)(() => {
    if (label) {
      return label;
    }
    if (key === "sub_total" && count !== void 0) {
      return /* @__PURE__ */ import_react.default.createElement("span", null, /* @__PURE__ */ import_react.default.createElement("span", null, `${labelMaps[key]}`), /* @__PURE__ */ import_react.default.createElement("span", { style: { color: "#667085", paddingLeft: "8px" } }, `${count || 0} ${import_utils.locales.getText("pisell2.subtotal.items")}`));
    }
    if (key === "tax") {
      return /* @__PURE__ */ import_react.default.createElement("span", null, /* @__PURE__ */ import_react.default.createElement("span", null, `${labelMaps[key]}`), /* @__PURE__ */ import_react.default.createElement(import_materials.Tooltip, { title: taxText }, /* @__PURE__ */ import_react.default.createElement(
        import_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__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)("pisell-lowcode__subtotal-item", {
        "pisell-lowcode__subtotal-item-bold": bold || (item == null ? void 0 : item.bold)
      }),
      ...rest
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__subtotal-item-label" }, renderLabel),
    /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellText.Amount,
      {
        value,
        className: "pisell-lowcode__subtotal-item-value"
      }
    )
  );
};
var Describe = (props) => {
  const { describe } = props;
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(import_antd.Divider, { className: "pisell-lowcode__subtotal-divider" }), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__subtotal-describe" }, describe));
};
var SubTotal = (props) => {
  var _a, _b;
  const {
    list = [],
    mode = "column1",
    describe = "",
    children,
    ...rest
  } = props;
  const context = (0, import_useEngineContext.default)();
  const locale = ((_b = (_a = context == null ? void 0 : context.engine) == null ? void 0 : _a.props) == null ? void 0 : _b.locale) || "en";
  import_utils.locales.init(import_locales.default, locale);
  const labelMaps = (0, import_react.useMemo)(() => {
    return getLabelMaps();
  }, [locale]);
  const depositItem = (0, import_react.useMemo)(() => {
    return list.find((item) => item.key === "deposit" && !(item == null ? void 0 : item.hidden));
  }, [list]);
  const totalItem = (0, import_react.useMemo)(() => {
    return list.find((item) => item.key === "total");
  }, [list]);
  const headerItem = (0, import_react.useMemo)(() => {
    if (depositItem) {
      return depositItem;
    }
    if (totalItem) {
      return totalItem;
    }
    return null;
  }, [depositItem, totalItem]);
  const mapData = (0, import_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, import_react.useEffect)(() => {
    document.body.id = "body";
  }, []);
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__subtotal-wrap", ...rest }, headerItem && /* @__PURE__ */ import_react.default.createElement(Item, { key: headerItem.key, item: headerItem, bold: true, labelMaps }), depositItem ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(import_antd.Divider, { className: "pisell-lowcode__subtotal-divider" }), totalItem ? /* @__PURE__ */ import_react.default.createElement(Item, { key: totalItem.key, item: totalItem, labelMaps }) : null) : null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__subtotal-list" }, mapData.map((items, index) => {
    const isLast = index === mapData.length - 1;
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__subtotal-item-block" }, /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        key: index,
        className: "pisell-lowcode__subtotal-item-wrap"
      },
      items.map((d) => /* @__PURE__ */ import_react.default.createElement(Item, { key: d.key, item: d, labelMaps }))
    )), mode === "column2" && !isLast ? /* @__PURE__ */ import_react.default.createElement(import_antd.Divider, { type: "vertical", style: { height: "auto" } }) : null);
  })), describe ? /* @__PURE__ */ import_react.default.createElement(Describe, { describe }) : null, children ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(import_antd.Divider, { className: "pisell-lowcode__subtotal-divider" }), children) : null);
};
var subTotal_default = SubTotal;
