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/pro/skuDetailModal/index.tsx
var skuDetailModal_exports = {};
__export(skuDetailModal_exports, {
  default: () => skuDetailModal_default
});
module.exports = __toCommonJS(skuDetailModal_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_decimal = __toESM(require("decimal.js"));
var import_materials2 = require("@pisell/materials");
var import_react_dom = require("react-dom");
var import_cloneDeep = __toESM(require("lodash/cloneDeep"));
var import_utils = require("@pisell/utils");
var import_Delete_wght300_24px = __toESM(require("@pisell/icon/es/Delete_wght300_24px"));
var import_locales = __toESM(require("./locales"));
var import_index = require("./index.less");
var import_pisellNumberSelector = __toESM(require("../pisellNumberSelector"));
var import_noteModal = __toESM(require("../../plus/noteModal"));
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var SkuDetailModal = ({ onConfirm, onRemove, onClose }, ref) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
  const [open, setOpen] = (0, import_react.useState)(false);
  const [productData, setProductData] = (0, import_react.useState)(null);
  const [note, setNote] = (0, import_react.useState)("");
  const [quantity, setQuantity] = (0, import_react.useState)(1);
  const [priceOverride, setPriceOverride] = (0, import_react.useState)(0);
  const [discounts, setDiscounts] = (0, import_react.useState)([]);
  const [noteModalOpen, setNoteModalOpen] = (0, import_react.useState)(false);
  const [showPriceAdjustModal, setShowPriceAdjustModal] = (0, import_react.useState)(false);
  const context = (0, import_useEngineContext.default)();
  const { store } = ((_a = context.appHelper) == null ? void 0 : _a.utils) || {};
  const symbol = ((_d = (_c = (_b = store == null ? void 0 : store.getState) == null ? void 0 : _b.call(store)) == null ? void 0 : _c.global) == null ? void 0 : _d.symbol) || "$";
  import_utils.locales.init(import_locales.default, ((_f = (_e = context == null ? void 0 : context.engine) == null ? void 0 : _e.props) == null ? void 0 : _f.locale) || "en");
  (0, import_react.useImperativeHandle)(ref, () => ({
    open: (data) => {
      var _a2, _b2, _c2;
      const _data = (0, import_cloneDeep.default)(data);
      setProductData(_data);
      setNote(((_a2 = _data == null ? void 0 : _data._extend) == null ? void 0 : _a2.note) || "");
      setQuantity(((_b2 = _data == null ? void 0 : _data._extend) == null ? void 0 : _b2.quantity) || 1);
      setPriceOverride(((_c2 = _data == null ? void 0 : _data._extend) == null ? void 0 : _c2.total) || 0);
      setDiscounts((_data == null ? void 0 : _data.discount_list) || []);
      setOpen(true);
      console.log("data>>>>>", _data);
    }
  }));
  const priceText = (0, import_react.useMemo)(() => {
    var _a2;
    const price = ((_a2 = productData == null ? void 0 : productData._extend) == null ? void 0 : _a2.price) || 0;
    const priceSign = Number(price) < 0 ? "-" : "";
    return `${priceSign}${symbol}${Math.abs(Number(price))}`;
  }, [(_g = productData == null ? void 0 : productData._extend) == null ? void 0 : _g.price, symbol]);
  const subtotal = (0, import_react.useMemo)(() => {
    var _a2;
    const dePrice = new import_decimal.default(((_a2 = productData == null ? void 0 : productData._extend) == null ? void 0 : _a2.price) || "0");
    const deQuantity = new import_decimal.default(quantity || "1");
    return dePrice.mul(deQuantity);
  }, [(_h = productData == null ? void 0 : productData._extend) == null ? void 0 : _h.price, quantity]);
  const subtotalText = (0, import_react.useMemo)(() => {
    const priceSign = subtotal.toNumber() < 0 ? "-" : "";
    return `${priceSign}${symbol}${subtotal.abs().toNumber().toFixed(2)}`;
  }, [subtotal, symbol]);
  const discountsCount = (0, import_react.useMemo)(() => {
    return (discounts == null ? void 0 : discounts.length) || 0;
  }, [discounts]);
  const totalDiscounts = (0, import_react.useMemo)(() => {
    if ((discounts == null ? void 0 : discounts.length) > 0) {
      return discounts.reduce((sum, discount) => {
        const includeType = ["discount_card", "product_discount_card"];
        const _quantity = includeType.includes((discount == null ? void 0 : discount.type) || "") ? quantity : 1;
        return sum.plus(new import_decimal.default((discount == null ? void 0 : discount.amount) || 0).mul(_quantity));
      }, new import_decimal.default(0)).toNumber().toFixed(2);
    }
    return "0.00";
  }, [discounts, quantity]);
  const finalTotal = (0, import_react.useMemo)(() => {
    const deTotal = new import_decimal.default(priceOverride || "0");
    const deQuantity = new import_decimal.default(quantity || "1");
    return deTotal.mul(deQuantity);
  }, [priceOverride, quantity]);
  const totalText = (0, import_react.useMemo)(() => {
    const priceSign = finalTotal.toNumber() < 0 ? "-" : "";
    return `${priceSign}${symbol}${finalTotal.abs().toNumber().toFixed(2)}`;
  }, [finalTotal, symbol]);
  const priceChange = (0, import_react.useMemo)(() => {
    var _a2;
    const dePrice = new import_decimal.default(((_a2 = productData == null ? void 0 : productData._extend) == null ? void 0 : _a2.price) || "0");
    const dePriceOverride = new import_decimal.default(priceOverride || "0");
    const deQuantity = new import_decimal.default(quantity || "1");
    return dePriceOverride.minus(dePrice).mul(deQuantity);
  }, [priceOverride, quantity, (_i = productData == null ? void 0 : productData._extend) == null ? void 0 : _i.price]);
  const priceChangeText = (0, import_react.useMemo)(() => {
    const priceChangeNumber = priceChange.toNumber();
    const priceChangeSign = priceChangeNumber > 0 ? "+" : priceChangeNumber < 0 ? "-" : "";
    const absPriceChange = priceChange.abs().toNumber().toFixed(2);
    return `${priceChangeSign}${symbol}${absPriceChange}`;
  }, [priceChange, symbol]);
  const priceOverrideText = (0, import_react.useMemo)(() => {
    const prefix = `(${import_utils.locales.getText(
      "pisell2.text.sku.detail.modal.price.override"
    )} `;
    const priceOverrideNumber = new import_decimal.default(priceOverride || "0");
    const priceOverrideSign = priceOverrideNumber.toNumber() < 0 ? "-" : "";
    const absPriceOverride = priceOverrideNumber.abs().toNumber().toFixed(2);
    return `${prefix}${priceOverrideSign}${symbol}${absPriceOverride})`;
  }, [priceOverride, symbol]);
  const modalCloseAction = () => {
    setOpen(false);
    setProductData(null);
    setNote("");
    setQuantity(1);
    setPriceOverride(0);
    setNoteModalOpen(false);
    setShowPriceAdjustModal(false);
    setDiscounts([]);
  };
  const handleCancel = () => {
    modalCloseAction();
    onClose == null ? void 0 : onClose();
  };
  const handleConfirm = () => {
    if (productData && onConfirm) {
      const updatedData = {
        ...productData,
        discount_list: discounts || [],
        _extend: {
          ...(productData == null ? void 0 : productData._extend) || {},
          quantity,
          note,
          total: priceOverride
        }
      };
      onConfirm(updatedData);
      modalCloseAction();
    }
  };
  const handleRemove = () => {
    if (productData && onRemove) {
      onRemove(productData);
      modalCloseAction();
    }
  };
  const handleManualPriceChange = () => {
    if (productData) {
      console.log("Manual price change for:", productData.id);
      onOpenPriceAdjust();
    }
  };
  const handleQuantityChange = (value) => {
    if (value && value > 0) {
      setQuantity(value);
    }
  };
  const onNoteConfirm = (note2) => {
    setNote(note2);
    setNoteModalOpen(false);
  };
  const onNoteDelete = () => {
    setNote("");
    setNoteModalOpen(false);
  };
  const onNoteCancel = () => {
    setNoteModalOpen(false);
  };
  const onOpenPriceAdjust = () => {
    setShowPriceAdjustModal(true);
  };
  const handlePriceAdjustConfirm = (value) => {
    setPriceOverride(value);
    setDiscounts([]);
    setShowPriceAdjustModal(false);
  };
  const coverText = (0, import_react.useMemo)(() => {
    var _a2;
    const title = ((_a2 = productData == null ? void 0 : productData.title) == null ? void 0 : _a2.trim()) || "";
    if (!title) return "";
    return Array.from(title).slice(0, 2).join("");
  }, [productData == null ? void 0 : productData.title]);
  if (!productData) return null;
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      platform: "pc",
      open,
      title: "",
      headerDivider: false,
      footer: null,
      onCancel: handleCancel,
      destroyOnClose: true,
      width: 880,
      className: "pisell-sku-detail-modal"
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "sku-detail-content" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "sku-detail-content-main" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "sku-detail-left" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "product-image" }, productData.cover ? /* @__PURE__ */ import_react.default.createElement("img", { src: productData.cover || "" }) : /* @__PURE__ */ import_react.default.createElement("div", { className: "product-cover-text" }, coverText)), /* @__PURE__ */ import_react.default.createElement("div", { className: "product-actions" }, /* @__PURE__ */ import_react.default.createElement(
      import_materials.Button,
      {
        size: "large",
        className: "add-note-btn",
        onClick: () => setNoteModalOpen(true)
      },
      import_utils.locales.getText(
        "pisell2.text.sku.detail.modal.btn.add.note"
      )
    ))), /* @__PURE__ */ import_react.default.createElement("div", { className: "sku-detail-right" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "product-info" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "product-info-left" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "product-name" }, (productData == null ? void 0 : productData.title) || ""), /* @__PURE__ */ import_react.default.createElement("div", { className: "product-spec" })), /* @__PURE__ */ import_react.default.createElement("div", { className: "item-price" }, priceText)), /* @__PURE__ */ import_react.default.createElement("div", { className: "quantity-section" }, /* @__PURE__ */ import_react.default.createElement("span", null, import_utils.locales.getText("pisell2.text.sku.detail.modal.quantity")), /* @__PURE__ */ import_react.default.createElement(
      import_pisellNumberSelector.default,
      {
        value: quantity,
        onChange: handleQuantityChange,
        min: 1,
        max: 99999,
        step: 1,
        size: "middle",
        enablePriceAdjust: true
      }
    )), /* @__PURE__ */ import_react.default.createElement("div", { className: "note-section" }, note && /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", null, import_utils.locales.getText("pisell2.text.sku.detail.modal.note")), /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: "note-text",
        onClick: () => setNoteModalOpen(true)
      },
      note
    ))), /* @__PURE__ */ import_react.default.createElement("div", { className: "price-summary-section" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "price-item" }, /* @__PURE__ */ import_react.default.createElement("div", null, import_utils.locales.getText("pisell2.text.sku.detail.modal.subtotal")), /* @__PURE__ */ import_react.default.createElement("div", null, subtotalText)), /* @__PURE__ */ import_react.default.createElement("div", { className: "price-item" }, /* @__PURE__ */ import_react.default.createElement("div", null, import_utils.locales.getText("pisell2.text.sku.detail.modal.discount"), discountsCount > 0 && /* @__PURE__ */ import_react.default.createElement("span", { className: "price-item-danger" }, " (", discountsCount, ")")), /* @__PURE__ */ import_react.default.createElement("div", { className: "price-item-danger" }, discountsCount > 0 ? "-" : "", symbol, totalDiscounts)), /* @__PURE__ */ import_react.default.createElement("div", { className: "price-item" }, /* @__PURE__ */ import_react.default.createElement("div", null, import_utils.locales.getText(
      "pisell2.text.sku.detail.modal.manual.price.change"
    ), /* @__PURE__ */ import_react.default.createElement(
      "span",
      {
        className: "price-item-override",
        onClick: onOpenPriceAdjust
      },
      priceOverrideText
    )), /* @__PURE__ */ import_react.default.createElement("div", { className: "price-item-danger" }, discountsCount > 0 ? `${symbol}0.00` : priceChangeText)), /* @__PURE__ */ import_react.default.createElement("div", { className: "price-item-total" }, /* @__PURE__ */ import_react.default.createElement("div", null, import_utils.locales.getText("pisell2.text.sku.detail.modal.total"), /* @__PURE__ */ import_react.default.createElement("span", { className: "price-item-total-gst" }, "（", (productData == null ? void 0 : productData.is_charge_tax) ? import_utils.locales.getText(
      "pisell2.text.sku.detail.modal.gst.incl"
    ) : import_utils.locales.getText(
      "pisell2.text.sku.detail.modal.gst.excl"
    ), "）")), /* @__PURE__ */ import_react.default.createElement("div", null, totalText))))), /* @__PURE__ */ import_react.default.createElement("div", { className: "sku-detail-content-footer" }, /* @__PURE__ */ import_react.default.createElement(
      import_materials.Button,
      {
        type: "primary",
        danger: true,
        size: "large",
        icon: /* @__PURE__ */ import_react.default.createElement(import_Delete_wght300_24px.default, { style: { fontSize: 20 } }),
        className: "sku-detail-content-footer-remove-btn",
        onClick: handleRemove
      },
      import_utils.locales.getText("pisell2.text.sku.detail.modal.btn.remove")
    ), /* @__PURE__ */ import_react.default.createElement("div", { className: "modal-actions" }, /* @__PURE__ */ import_react.default.createElement(
      import_materials.Button,
      {
        type: "default",
        size: "large",
        onClick: handleManualPriceChange,
        className: "sku-detail-content-footer-price-btn"
      },
      /* @__PURE__ */ import_react.default.createElement("span", { className: "sku-detail-content-footer-price-btn-text" }, import_utils.locales.getText(
        "pisell2.text.sku.detail.modal.btn.manual.price.change"
      ))
    ), /* @__PURE__ */ import_react.default.createElement(
      import_materials.Button,
      {
        type: "primary",
        size: "large",
        onClick: handleConfirm,
        className: "sku-detail-content-footer-confirm-btn"
      },
      import_utils.locales.getText("pisell2.text.sku.detail.modal.btn.confirm")
    )))),
    noteModalOpen && /* @__PURE__ */ import_react.default.createElement(
      import_noteModal.default,
      {
        open: noteModalOpen,
        value: note,
        onCancel: onNoteCancel,
        onConfirm: onNoteConfirm,
        onDelete: onNoteDelete
      }
    ),
    showPriceAdjustModal && (0, import_react_dom.createPortal)(
      /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-price-adjust-overlay" }, /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: "pisell-price-adjust-backdrop",
          onMouseDown: (e) => {
            e.preventDefault();
            setShowPriceAdjustModal(false);
          }
        }
      ), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-price-adjust-container" }, /* @__PURE__ */ import_react.default.createElement(
        import_materials2.PisellAdjustPrice,
        {
          originValue: priceOverride,
          value: priceOverride,
          onChange: handlePriceAdjustConfirm,
          selectType: "dark"
        }
      ))),
      document.body
    )
  ));
};
var skuDetailModal_default = (0, import_react.forwardRef)(SkuDetailModal);
