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/ecocup/components/CupStatusModal/index.tsx
var CupStatusModal_exports = {};
__export(CupStatusModal_exports, {
  default: () => CupStatusModal_default
});
module.exports = __toCommonJS(CupStatusModal_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_types = require("../../types");
var import_index = require("./index.less");
var import_CupInfoCard = __toESM(require("../CupInfoCard"));
var import_CustomerCard = __toESM(require("../CustomerCard"));
var import_hooks = require("./hooks");
var import_SearchBar = __toESM(require("../SearchBar"));
var SCAN_LISTENER_KEY = "eco-cup-status-modal-scan-listener";
var CupStatusModal = (props) => {
  var _a, _b, _c;
  const { visible, onCancel, onOk, data } = props;
  const { scanStatus, cupData } = data || {};
  const cupAction = (0, import_hooks.useCupAction)();
  const [cupList, setCupList] = (0, import_react.useState)([]);
  const batchCupAction = (0, import_hooks.useBatchCupAction)();
  (0, import_react.useEffect)(() => {
    if (!cupData) {
      setCupList([]);
      return;
    }
    const initialCups = Array.isArray(cupData) ? cupData : [cupData];
    setCupList(initialCups);
  }, [cupData]);
  const otherRentStatus = (0, import_react.useMemo)(() => {
    return scanStatus === import_types.ECupScanStatus.OTHER_RENT;
  }, [scanStatus]);
  const isRed = !otherRentStatus;
  const cupStatistics = (0, import_hooks.useCupListStatistics)(cupList);
  const lineStyle = (0, import_react.useMemo)(() => {
    if (isRed) {
      return {
        backgroundColor: "#FEE4E2",
        color: "#F04438"
      };
    }
    return {
      backgroundColor: "#F6F2FF",
      color: "#7F56DA"
    };
  }, [isRed]);
  const cupInfoCardType = (0, import_react.useMemo)(() => {
    if (scanStatus === import_types.ECupScanStatus.OTHER_IN_STOCK) {
      return "otherBelongInfo";
    }
    if (scanStatus === import_types.ECupScanStatus.IN_STOCK) {
      return "belongInfo";
    }
    if (otherRentStatus || scanStatus === import_types.ECupScanStatus.RENT) {
      return "rentInfo";
    }
    if (scanStatus === import_types.ECupScanStatus.MISSING || scanStatus === import_types.ECupScanStatus.BROKEN) {
      return "markInfo";
    }
    return "";
  }, [scanStatus]);
  const _onCancel = () => {
    if (cupAction == null ? void 0 : cupAction.loading) return;
    onCancel == null ? void 0 : onCancel();
  };
  const _onOk = async () => {
    if (cupAction == null ? void 0 : cupAction.loading) return;
    const type = otherRentStatus ? import_types.ECupActionType.RETURN : "";
    if (!type) {
      onOk == null ? void 0 : onOk();
      return;
    }
    let result;
    if (otherRentStatus) {
      const ids = cupList.map((cup) => (cup == null ? void 0 : cup.eco_cup_id) || (cup == null ? void 0 : cup.id));
      result = await batchCupAction.run(ids);
    } else {
      result = await cupAction.run((cupData == null ? void 0 : cupData.eco_cup_id) || (cupData == null ? void 0 : cupData.id), type);
    }
    if (result) {
      (0, import_materials.PisellToast)({
        content: import_utils.locales.getText(`pisell2.text.ecocup.toast.success-${type}`)
      });
      onOk == null ? void 0 : onOk();
    }
  };
  const onSearchResult = (result) => {
    var _a2;
    if (cupList.find((c) => {
      var _a3;
      return (c == null ? void 0 : c.id) === ((_a3 = result == null ? void 0 : result.data) == null ? void 0 : _a3.id);
    })) {
      return;
    }
    if (((_a2 = result == null ? void 0 : result.data) == null ? void 0 : _a2.status) !== import_types.ECupStatus.RENT) {
      return;
    }
    setCupList([...cupList, result == null ? void 0 : result.data]);
  };
  const removeCupFromList = (cupId) => {
    setCupList(cupList.filter((cup) => (cup == null ? void 0 : cup.id) !== cupId));
  };
  const renderSearchBar = () => {
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell2-ecocup-customer-first-modal-search-bar" }, /* @__PURE__ */ import_react.default.createElement(
      import_SearchBar.default,
      {
        scanListenerKey: SCAN_LISTENER_KEY,
        searchType: "cup",
        placeholder: import_utils.locales.getText(
          "pisell2.text.ecocup.customer-first-modal.search-bar.placeholder"
        ),
        onSearchResult,
        onCupStatusValidate: onSearchResult
      }
    ));
  };
  const showRemove = (0, import_react.useMemo)(() => {
    return (cupList == null ? void 0 : cupList.length) > 1 && otherRentStatus;
  }, [cupList.length, otherRentStatus]);
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      headerDivider: false,
      open: visible,
      onCancel: _onCancel,
      onOk: _onOk,
      title: otherRentStatus ? import_utils.locales.getText(
        `pisell2.text.ecocup.cup-status-modal.title-${scanStatus}`
      )(cupList.length) : import_utils.locales.getText(
        `pisell2.text.ecocup.cup-status-modal.title-${scanStatus}`
      ),
      cancelText: otherRentStatus ? import_utils.locales.getText("pisell2.text.ecocup.button.cancel") : import_utils.locales.getText("pisell2.text.ecocup.button.dismiss"),
      okText: isRed ? import_utils.locales.getText("pisell2.text.ecocup.button.confirm") : import_utils.locales.getText("pisell2.text.ecocup.button.return"),
      okButtonProps: { loading: cupAction == null ? void 0 : cupAction.loading },
      cancelVisible: !isRed,
      width: 580
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell2-ecocup-cup-status-modal-content", style: { gap: otherRentStatus ? "20px" : "32px" } }, otherRentStatus ? /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell2-ecocup-cup-status-modal-content-search" }, /* @__PURE__ */ import_react.default.createElement("span", { className: "tip-text" }, import_utils.locales.getText(
      "pisell2.text.ecocup.cup-status-modal.search-bar.title"
    )), renderSearchBar()) : null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell2-ecocup-cup-status-modal-content-cup-lists" }, cupList.map((cup, index) => /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        key: (cup == null ? void 0 : cup.id) || index,
        className: "pisell2-ecocup-cup-status-modal-content-cup",
        style: {
          ...lineStyle,
          justifyContent: showRemove ? "space-between" : "center"
        }
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell2-ecocup-cup-status-modal-content-cup-info" }, /* @__PURE__ */ import_react.default.createElement(import_materials.Icon, { type: "RestOutlined", size: 24 }), /* @__PURE__ */ import_react.default.createElement(
        "span",
        {
          style: {
            color: !isRed ? "#667085" : "#F04438"
          }
        },
        import_utils.locales.getText("pisell2.text.ecocup.cup-id")
      ), /* @__PURE__ */ import_react.default.createElement("span", null, (cup == null ? void 0 : cup.eco_cup_code) || (cup == null ? void 0 : cup.code) || "")),
      showRemove && /* @__PURE__ */ import_react.default.createElement(
        import_materials.Button,
        {
          type: "primary",
          icon: /* @__PURE__ */ import_react.default.createElement(import_materials.Icon, { type: "pisell2-trash-01", size: 24, color: "#7F56DA" }),
          onClick: () => removeCupFromList(cup == null ? void 0 : cup.id),
          className: "info-remove"
        },
        import_utils.locales.getText("pisell2.text.ecocup.button.remove")
      )
    ))), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell2-ecocup-cup-status-modal-content-info" }, /* @__PURE__ */ import_react.default.createElement("div", { style: { flex: 1 } }, ((cupStatistics == null ? void 0 : cupStatistics.uniqueShop) || (cupStatistics == null ? void 0 : cupStatistics.hasMultipleShops)) && cupInfoCardType ? /* @__PURE__ */ import_react.default.createElement(
      import_CupInfoCard.default,
      {
        type: cupInfoCardType,
        data: {
          shop: (_a = cupStatistics == null ? void 0 : cupStatistics.uniqueShop) == null ? void 0 : _a.name,
          time: cupStatistics == null ? void 0 : cupStatistics.uniqueTime,
          isMultipleTime: cupStatistics == null ? void 0 : cupStatistics.hasMultipleTimes,
          isMultipleShop: cupStatistics == null ? void 0 : cupStatistics.hasMultipleShops
        }
      }
    ) : null), /* @__PURE__ */ import_react.default.createElement("div", { style: { flex: 1 } }, ((_b = cupList == null ? void 0 : cupList[0]) == null ? void 0 : _b.status) === import_types.ECupStatus.RENT ? (cupStatistics == null ? void 0 : cupStatistics.uniqueCustomer) || (cupStatistics == null ? void 0 : cupStatistics.hasMultipleCustomers) ? /* @__PURE__ */ import_react.default.createElement(
      import_CustomerCard.default,
      {
        ...(cupStatistics == null ? void 0 : cupStatistics.uniqueCustomer) || {},
        isMultiple: cupStatistics == null ? void 0 : cupStatistics.hasMultipleCustomers
      }
    ) : null : /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell2-ecocup-cup-status-modal-content-info-tip" }, ((_c = cupList == null ? void 0 : cupList[0]) == null ? void 0 : _c.status) === import_types.ECupStatus.IN_STOCK ? import_utils.locales.getText(
      "pisell2.text.ecocup.cup-status-modal.tip-in-stock"
    ) : import_utils.locales.getText("pisell2.text.ecocup.cup-status-modal.tip")))))
  );
};
var CupStatusModal_default = CupStatusModal;
