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/plus/saleDetail/components/ProductItemsSummary/index.tsx
var ProductItemsSummary_exports = {};
__export(ProductItemsSummary_exports, {
  ProductItemsSummary: () => ProductItemsSummary
});
module.exports = __toCommonJS(ProductItemsSummary_exports);
var import_react = __toESM(require("react"));
var import_decimal = __toESM(require("decimal.js"));
var import_icon = require("@pisell/icon");
var import_utils = require("@pisell/utils");
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var import_theme = require("./theme");
var import_display = require("../LineItem/utils/display");
var import_render = require("../LineItem/utils/render");
var import_index = require("./index.less");
var PREFIX = "pisell-sale-detail-product-items-summary";
var hasMoneyValue = (value) => {
  return value !== void 0 && value !== null && value !== "";
};
var getPriceQuantityText = (price, quantity, symbol) => {
  if (!hasMoneyValue(price) || quantity === void 0 || quantity === null) {
    return void 0;
  }
  return `${(0, import_render.formatMoney)(price, symbol)} x ${quantity}`;
};
var getProductQuantity = (item) => {
  var _a;
  const product = item;
  return ((_a = product._extend) == null ? void 0 : _a.displayQuantity) ?? product.num ?? product.product_quantity ?? 1;
};
var getProductLineTotal = (item) => {
  const product = item;
  return new import_decimal.default(product.selling_price ?? product.total ?? product.payment_price ?? 0).mul(getProductQuantity(item)).toString();
};
var isVisibleProductItem = (item) => {
  return !(item == null ? void 0 : item._isGiftPromotion) && !(item == null ? void 0 : item._giftInfo);
};
var getProductTitle = (product) => {
  const item = product;
  return (0, import_display.toDisplayText)(item.product_title || item.title || item.name);
};
var getProductOptions = (product) => {
  const item = product;
  if (Array.isArray(item.product_option_item)) {
    return item.product_option_item;
  }
  if (Array.isArray(item.options)) {
    return item.options;
  }
  return [];
};
var getProductBundles = (product) => {
  const item = product;
  if (Array.isArray(item.product_bundle)) {
    return item.product_bundle;
  }
  if (Array.isArray(item.bundle)) {
    return item.bundle;
  }
  return [];
};
var getBundleOptions = (bundle) => {
  if (Array.isArray(bundle.option)) return bundle.option;
  if (Array.isArray(bundle.options)) return bundle.options;
  return [];
};
var getProductKey = (item, index) => {
  var _a;
  const product = item;
  return ((_a = product.metadata) == null ? void 0 : _a.unique_identification_number) || product.order_detail_id || product.product_id || index;
};
var buildSpecificationItems = (product, symbol) => {
  const items = [];
  const variantText = (0, import_display.toDisplayText)(product.product_option_string);
  const optionList = getProductOptions(product);
  if (variantText) {
    items.push({
      key: `variant-${variantText}`,
      type: "variant",
      label: variantText
    });
  }
  optionList.forEach((option, index) => {
    const label = (0, import_display.toDisplayText)((option == null ? void 0 : option.name) ?? (option == null ? void 0 : option.title));
    if (!label) return;
    items.push({
      key: `option-${(option == null ? void 0 : option.id) ?? (option == null ? void 0 : option.product_option_item_id) ?? index}`,
      type: "option",
      label,
      meta: getPriceQuantityText(
        (option == null ? void 0 : option.add_price) ?? (option == null ? void 0 : option.price),
        (option == null ? void 0 : option.num) ?? (option == null ? void 0 : option.quantity) ?? 1,
        symbol
      )
    });
  });
  const productBundles = getProductBundles(product);
  if (productBundles.length) {
    productBundles.forEach((bundle, index) => {
      const label = (0, import_display.toDisplayText)((bundle == null ? void 0 : bundle.title) ?? (bundle == null ? void 0 : bundle.bundle_title));
      if (!label) return;
      const bundleItem = {
        key: `bundle-${(bundle == null ? void 0 : bundle.id) ?? index}`,
        type: "bundle",
        label,
        meta: getPriceQuantityText(
          (bundle == null ? void 0 : bundle.bundle_selling_price) ?? (bundle == null ? void 0 : bundle.total),
          (bundle == null ? void 0 : bundle.num) ?? (bundle == null ? void 0 : bundle.quantity) ?? 1,
          symbol
        ),
        children: []
      };
      const bundleOptions = getBundleOptions(bundle);
      bundleOptions.forEach((option, optionIndex) => {
        var _a;
        const optionLabel = (0, import_display.toDisplayText)(option == null ? void 0 : option.name);
        if (!optionLabel) return;
        (_a = bundleItem.children) == null ? void 0 : _a.push({
          key: `bundle-option-${(bundle == null ? void 0 : bundle.id) ?? index}-${(option == null ? void 0 : option.id) ?? (option == null ? void 0 : option.product_option_item_id) ?? optionIndex}`,
          type: "bundle-option",
          label: optionLabel,
          meta: getPriceQuantityText(
            (option == null ? void 0 : option.add_price) ?? (option == null ? void 0 : option.price),
            (option == null ? void 0 : option.num) ?? (option == null ? void 0 : option.quantity) ?? 1,
            symbol
          )
        });
      });
      items.push(bundleItem);
    });
  }
  return items;
};
var SpecificationRow = ({ item }) => {
  return /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__spec ${PREFIX}__spec--${item.type}` }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__spec-main` }, /* @__PURE__ */ import_react.default.createElement("span", { className: `${PREFIX}__spec-label` }, item.label), item.meta ? /* @__PURE__ */ import_react.default.createElement("span", { className: `${PREFIX}__spec-meta` }, item.meta) : null));
};
var SelectedSpecificationList = ({
  items
}) => {
  if (!items.length) return null;
  return /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__spec-section` }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__spec-list` }, items.map((item) => {
    if (item.type === "bundle") {
      return /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          key: item.key,
          className: `${PREFIX}__spec-group ${PREFIX}__spec-group--bundle`
        },
        /* @__PURE__ */ import_react.default.createElement(SpecificationRow, { item }),
        item.children && item.children.length > 0 ? /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__spec-group-children` }, item.children.map((child) => /* @__PURE__ */ import_react.default.createElement(SpecificationRow, { key: child.key, item: child }))) : null
      );
    }
    return /* @__PURE__ */ import_react.default.createElement(SpecificationRow, { key: item.key, item });
  })));
};
function ProductItemsSummary({
  items = [],
  disabledEdit,
  onEdit,
  theme = "dark",
  className,
  style
}) {
  var _a, _b, _c, _d;
  const engineContext = (0, import_useEngineContext.default)();
  const displayItems = (0, import_react.useMemo)(
    () => [...items].reverse().filter(isVisibleProductItem),
    [items]
  );
  const symbol = ((_c = (_b = (_a = engineContext == null ? void 0 : engineContext.appHelper) == null ? void 0 : _a.utils) == null ? void 0 : _b.getSymbolic) == null ? void 0 : _c.call(_b)) || ((_d = displayItems[0]) == null ? void 0 : _d.symbol) || "$";
  const displayRows = (0, import_react.useMemo)(
    () => displayItems.map((item, index) => ({
      key: getProductKey(item, index),
      title: getProductTitle(item),
      quantity: getProductQuantity(item),
      lineTotal: getProductLineTotal(item),
      specificationItems: buildSpecificationItems(item, symbol)
    })),
    [displayItems, symbol]
  );
  const themeStyle = (0, import_react.useMemo)(
    () => (0, import_theme.getProductItemsSummaryThemeStyle)(theme),
    [theme]
  );
  const rootStyle = (0, import_react.useMemo)(
    () => ({
      ...themeStyle,
      ...style
    }),
    [style, themeStyle]
  );
  const themeModifier = theme === "light" ? "light" : "dark";
  const subtotal = (0, import_react.useMemo)(() => {
    return displayRows.reduce(
      (total, item) => total.add(item.lineTotal),
      new import_decimal.default(0)
    ).toString();
  }, [displayRows]);
  if (!displayRows.length) return null;
  return /* @__PURE__ */ import_react.default.createElement(
    "section",
    {
      className: [
        PREFIX,
        `${PREFIX}--${themeModifier}`,
        className || ""
      ].filter(Boolean).join(" "),
      style: rootStyle
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__header` }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__title` }, /* @__PURE__ */ import_react.default.createElement(import_icon.Clipboard, { size: 18 }), /* @__PURE__ */ import_react.default.createElement("span", null, import_utils.locales.getText("pisell2.sale-detail.items-ordered"))), !disabledEdit && onEdit ? /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        className: `${PREFIX}__edit`,
        type: "button",
        onClick: onEdit
      },
      import_utils.locales.getText("pisell2.sale-detail.action.edit")
    ) : null),
    /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__list` }, displayRows.map((item) => /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: `${PREFIX}__row`,
        key: item.key
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__row-main` }, /* @__PURE__ */ import_react.default.createElement("span", { className: `${PREFIX}__name` }, item.title), /* @__PURE__ */ import_react.default.createElement("span", { className: `${PREFIX}__amount` }, (0, import_render.formatMoney)(item.lineTotal, symbol), " x ", item.quantity)),
      /* @__PURE__ */ import_react.default.createElement(SelectedSpecificationList, { items: item.specificationItems })
    ))),
    /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__subtotal` }, /* @__PURE__ */ import_react.default.createElement("span", null, import_utils.locales.getText("pisell2.sale-detail.subtotal")), /* @__PURE__ */ import_react.default.createElement("span", null, (0, import_render.formatMoney)(subtotal, symbol)))
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ProductItemsSummary
});
