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/checkout/components/WalletPassModule/index.tsx
var WalletPassModule_exports = {};
__export(WalletPassModule_exports, {
  default: () => WalletPassModule_default
});
module.exports = __toCommonJS(WalletPassModule_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_useWalletPass = require("../../hooks/useWalletPass");
var import_WalletPassHeader = __toESM(require("./components/WalletPassHeader"));
var import_WalletPassCardDisplay = __toESM(require("./components/WalletPassCardDisplay"));
var import_WalletModal = __toESM(require("./components/WalletModal"));
var import_DiscountWallet = __toESM(require("./components/DiscountWallet"));
var import_utils2 = require("./utils");
var import_constants = require("../../constants");
var import_index = require("./index.less");
var WalletPassModule = (0, import_react.forwardRef)(
  (props, ref) => {
    var _a;
    const { orderInfo, onSelectChange, customAmount, paymentItems, clientId } = props;
    const {
      handleSelectWallet,
      cardData,
      selectedWalletIds,
      expiredWalletIds,
      expiredWalletData,
      disabledWalletData,
      clearAllSelectedWallet,
      clearDisabledWalletData,
      handleBestSaving,
      loading,
      handleSearchIdentificationCode,
      availableDiscountList,
      selectedDiscountList,
      handleSelectDiscount,
      customerName,
      handleConfirmEditAmount,
      handleBeforeEditAmount
    } = (0, import_useWalletPass.useWalletPass)({
      onSelectChange,
      customAmount,
      clientId
    });
    const [expiredModalVisible, setExpiredModalVisible] = (0, import_react.useState)(false);
    const [disabledModalVisible, setDisabledModalVisible] = (0, import_react.useState)(false);
    const [isCardMode, setIsCardMode] = (0, import_react.useState)(true);
    const handleExpiredClick = () => {
      setExpiredModalVisible(true);
    };
    const handleExpiredModalClose = () => {
      setExpiredModalVisible(false);
    };
    const handleDisabledModalClose = () => {
      setDisabledModalVisible(false);
      clearDisabledWalletData();
    };
    const toggleDisplayMode = () => {
      setIsCardMode(!isCardMode);
    };
    const handleDetail = (record, index) => {
    };
    const formattedDisabledWalletData = import_react.default.useMemo(() => {
      return (0, import_utils2.formatMachineCodeServer2CardList)(disabledWalletData || [], [], {
        error: true,
        showStatus: false
      });
    }, [disabledWalletData]);
    (0, import_react.useEffect)(() => {
      if (disabledWalletData && disabledWalletData.length > 0) {
        if (!disabledModalVisible) {
          setDisabledModalVisible(true);
        }
      }
    }, [disabledWalletData, disabledModalVisible]);
    (0, import_react.useImperativeHandle)(ref, () => ({
      handleSearchIdentificationCode,
      getWalletPassData: (select) => ({
        selectedWallet: select.length ? cardData.filter(
          (card) => selectedWalletIds.includes(card.id)
        ) : [],
        availableDiscountList,
        selectedDiscountList
      })
    }));
    const disabledTitle = (0, import_react.useMemo)(() => {
      var _a2, _b;
      if (disabledWalletData && disabledWalletData.length === 1) {
        return ((_b = (_a2 = disabledWalletData[0]) == null ? void 0 : _a2.unified_messages) == null ? void 0 : _b.title) || "";
      }
      return import_utils.locales.getText("wallet-pass-module-disabled-modal-title");
    }, [disabledWalletData]);
    const selectedCount = (0, import_react.useMemo)(() => {
      return selectedWalletIds.length + selectedDiscountList.length;
    }, [selectedWalletIds.length, selectedDiscountList.length]);
    const discountDisabled = (0, import_react.useMemo)(() => {
      if (Number(orderInfo == null ? void 0 : orderInfo.totalAmount) === 0) return true;
      return !!(paymentItems == null ? void 0 : paymentItems.some((item) => {
        return !import_constants.WALLET_PASS_CODE_ARRAY.includes(item.code) && !item.isSynced;
      }));
    }, [paymentItems, orderInfo]);
    if (cardData.length === 0 && availableDiscountList.length === 0 && disabledWalletData.length === 0) {
      return null;
    }
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "wallet-pass-module-container" }, /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellLoading,
      {
        delay: 300,
        loading,
        fullscreen: false,
        mask: {
          backgroundColor: "rgba(255, 255, 255, 0.5)",
          width: "100%",
          height: "100%"
        }
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: "wallet-pass-module" }, /* @__PURE__ */ import_react.default.createElement(
        import_WalletPassHeader.default,
        {
          customerName,
          selectedCount,
          expiredCount: expiredWalletIds.length,
          isCardMode,
          onExpiredClick: handleExpiredClick,
          onBestSaving: handleBestSaving,
          onClearAll: clearAllSelectedWallet,
          onToggleDisplayMode: toggleDisplayMode
        }
      ), /* @__PURE__ */ import_react.default.createElement(
        import_WalletPassCardDisplay.default,
        {
          isCardMode,
          cardData,
          selectedWalletIds,
          onSelectWallet: handleSelectWallet,
          onDetail: handleDetail,
          onConfirmEditAmount: handleConfirmEditAmount,
          onBeforeEditAmount: handleBeforeEditAmount
        }
      ), /* @__PURE__ */ import_react.default.createElement(
        import_DiscountWallet.default,
        {
          isCardMode,
          cardData: availableDiscountList,
          selectedWalletIds: selectedDiscountList,
          onSelectWallet: handleSelectDiscount,
          onDetail: handleDetail,
          discountDisabled
        }
      ), /* @__PURE__ */ import_react.default.createElement(
        import_WalletModal.default,
        {
          visible: expiredModalVisible,
          walletData: expiredWalletData,
          onClose: handleExpiredModalClose,
          title: (_a = import_utils.locales.getText("wallet-pass-module-expired-modal-title")) == null ? void 0 : _a(
            (expiredWalletData == null ? void 0 : expiredWalletData.length) || 0
          )
        }
      ), /* @__PURE__ */ import_react.default.createElement(
        import_WalletModal.default,
        {
          visible: disabledModalVisible,
          walletData: formattedDisabledWalletData,
          onClose: handleDisabledModalClose,
          title: disabledTitle
        }
      ))
    ));
  }
);
var WalletPassModule_default = WalletPassModule;
