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/productStockCheckModal/index.tsx
var productStockCheckModal_exports = {};
__export(productStockCheckModal_exports, {
  default: () => productStockCheckModal_default
});
module.exports = __toCommonJS(productStockCheckModal_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_locales = __toESM(require("./locales"));
var import_utils2 = require("../../utils");
var import_index = require("./index.less");
var MAX_COLLAPSED_ITEMS = 5;
var ProductStockCheckModal = (props, ref) => {
  var _a, _b, _c, _d, _e, _f, _g;
  const context = (0, import_useEngineContext.default)();
  import_utils.locales.init(import_locales.default, ((_b = (_a = context == null ? void 0 : context.engine) == null ? void 0 : _a.props) == null ? void 0 : _b.locale) || "en");
  import_utils2.request.setRequest((_d = (_c = context == null ? void 0 : context.appHelper) == null ? void 0 : _c.utils) == null ? void 0 : _d.request);
  const isTerminal = (_g = (_f = (_e = context == null ? void 0 : context.appHelper) == null ? void 0 : _e.utils) == null ? void 0 : _f.isTerminal) == null ? void 0 : _g.call(_f);
  const [visible, setVisible] = (0, import_react.useState)(false);
  const modalParamsRef = (0, import_react.useRef)();
  const [items, setItems] = (0, import_react.useState)([]);
  const [isAllItemsSoldOut, setIsAllItemsSoldOut] = (0, import_react.useState)(false);
  const [listExpanded, setListExpanded] = (0, import_react.useState)(false);
  const onOpen = (0, import_react.useCallback)(
    (params) => {
      var _a2;
      modalParamsRef.current = params;
      setItems((params == null ? void 0 : params.unavailableProducts) ?? []);
      setIsAllItemsSoldOut((params == null ? void 0 : params.isAllItemsSoldOut) ?? false);
      setListExpanded(false);
      setVisible(true);
      (_a2 = props.onOpen) == null ? void 0 : _a2.call(props);
    },
    [props]
  );
  const onClose = (0, import_react.useCallback)(() => {
    var _a2;
    modalParamsRef.current = void 0;
    setItems([]);
    setIsAllItemsSoldOut(false);
    setVisible(false);
    (_a2 = props.onClose) == null ? void 0 : _a2.call(props);
  }, []);
  (0, import_react.useImperativeHandle)(ref, () => ({
    open: onOpen,
    close: onClose
  }));
  const handleReturn = (0, import_react.useCallback)(() => {
    var _a2;
    onClose();
    (_a2 = props.onReturn) == null ? void 0 : _a2.call(props, items);
  }, [props.onReturn, onClose, items]);
  const handleContinue = (0, import_react.useCallback)(() => {
    var _a2;
    onClose();
    if (isAllItemsSoldOut) {
      handleReturn();
    } else {
      (_a2 = props.onContinue) == null ? void 0 : _a2.call(props, items);
    }
  }, [props.onContinue, onClose, isAllItemsSoldOut, items]);
  const showListExpandToggle = (0, import_react.useMemo)(
    () => items.length > MAX_COLLAPSED_ITEMS,
    [items.length]
  );
  const displayItems = (0, import_react.useMemo)(() => {
    if (!showListExpandToggle || listExpanded) {
      return items;
    }
    return items.slice(0, MAX_COLLAPSED_ITEMS);
  }, [items, listExpanded, showListExpandToggle]);
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      platform: isTerminal ? "h5" : void 0,
      visible,
      header: false,
      footer: null,
      maskClosable: false,
      destroyOnClose: true,
      closable: false,
      onClose,
      onCancel: onClose,
      wrapClassName: "product-stock-check-modal",
      style: { zIndex: 1001 }
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "product-stock-check-modal-inner" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "product-stock-check-modal-icon", "aria-hidden": true }, /* @__PURE__ */ import_react.default.createElement("span", { className: "product-stock-check-modal-icon-mark" }, "!")), /* @__PURE__ */ import_react.default.createElement("h2", { className: "product-stock-check-modal-title" }, isAllItemsSoldOut ? import_utils.locales.getText(
      "pisell2.product.stock.check.modal.all_items_title"
    ) : import_utils.locales.getText("pisell2.product.stock.check.modal.title")), /* @__PURE__ */ import_react.default.createElement("p", { className: "product-stock-check-modal-subtitle" }, import_utils.locales.getText("pisell2.product.stock.check.modal.subtitle")), /* @__PURE__ */ import_react.default.createElement("div", { className: "product-stock-check-modal-list" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "product-stock-check-modal-list-items" }, displayItems.map((item, index) => /* @__PURE__ */ import_react.default.createElement("div", { key: index, className: "product-stock-check-modal-item" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "product-stock-check-modal-item-text" }, /* @__PURE__ */ import_react.default.createElement("span", { className: "product-stock-check-modal-item-title" }, item.title), /* @__PURE__ */ import_react.default.createElement("span", { className: "product-stock-check-modal-item-qty" }, import_utils.locales.getText(
      "pisell2.product.stock.check.modal.item.quantity.prefix"
    ), " ", "x", item.quantity)), /* @__PURE__ */ import_react.default.createElement("span", { className: "product-stock-check-modal-tag" }, /* @__PURE__ */ import_react.default.createElement(import_materials.Tag, { color: "error" }, import_utils.locales.getText(
      "pisell2.product.stock.check.modal.status.sold_out"
    )))))), showListExpandToggle && /* @__PURE__ */ import_react.default.createElement("div", { className: "product-stock-check-modal-list-toggle", onClick: () => setListExpanded((v) => !v) }, listExpanded ? import_utils.locales.getText(
      "pisell2.product.stock.check.modal.btn.collapse"
    ) : import_utils.locales.getText(
      "pisell2.product.stock.check.modal.btn.expand"
    ), /* @__PURE__ */ import_react.default.createElement(import_materials.Icon, { type: listExpanded ? "pisell2-chevron-up-double" : "pisell2-chevron-down-double" }))), /* @__PURE__ */ import_react.default.createElement("div", { className: "product-stock-check-modal-buttons" }, /* @__PURE__ */ import_react.default.createElement(import_materials.Button, { type: "primary", onClick: handleContinue }, isAllItemsSoldOut ? import_utils.locales.getText(
      "pisell2.product.stock.check.modal.btn.remove_all_continue"
    ) : import_utils.locales.getText(
      "pisell2.product.stock.check.modal.btn.remove_continue"
    )), /* @__PURE__ */ import_react.default.createElement(import_materials.Button, { type: "text", onClick: handleReturn }, import_utils.locales.getText("pisell2.product.stock.check.modal.btn.go_back"))))
  );
};
var productStockCheckModal_default = (0, import_react.forwardRef)(ProductStockCheckModal);
