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/venueBooking/components/Voucher/index.tsx
var Voucher_exports = {};
__export(Voucher_exports, {
  default: () => VoucherCard
});
module.exports = __toCommonJS(Voucher_exports);
var import_react = __toESM(require("react"));
var import_ChevronDown = __toESM(require("@pisell/icon/es/ChevronDown"));
var import_UserCircle = __toESM(require("@pisell/icon/es/UserCircle"));
var import_classnames = __toESM(require("classnames"));
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_materials = require("@pisell/materials");
var import_context = require("../../context");
var import_voucher = require("../../../booking/components/voucher/index.less");
var import_Voucher = require("../../../appointmentBooking/components/Voucher/index.less");
function VoucherCard() {
  const {
    isLogin,
    openLoginModal,
    discountList,
    runModuleAction,
    refreshViewModel,
    venueBookingModule
  } = (0, import_context.useVenueBookingContext)();
  const [showDetail, setShowDetail] = (0, import_react.useState)(true);
  const [code, setCode] = (0, import_react.useState)("");
  const availableData = (0, import_react.useMemo)(() => {
    return (discountList || []).filter((item) => !!item.isAvailable).map((discount) => ({
      ...discount,
      isDisabled: discount.isDisabled || discount.isDisabledForProductUsed
    }));
  }, [discountList]);
  const syncAfterDiscountChange = (0, import_react.useCallback)(async () => {
    await refreshViewModel();
  }, [refreshViewModel]);
  const onScanCode = (0, import_react.useCallback)(async () => {
    var _a;
    if (!code.trim()) return;
    try {
      const tempOrder = (_a = venueBookingModule == null ? void 0 : venueBookingModule.getTempOrder) == null ? void 0 : _a.call(venueBookingModule);
      const customerId = (tempOrder == null ? void 0 : tempOrder.customer_id) || void 0;
      const result = await runModuleAction("scanCode", code, customerId);
      if (result == null ? void 0 : result.isAvailable) {
        setCode("");
        await syncAfterDiscountChange();
      } else {
        const content = {
          time_limit: import_utils.locales.getText("pisell2.text.goodpass.code-invalid-expired")
        };
        import_antd.message.error({
          content: String(
            (result == null ? void 0 : result.type) === "server" ? import_utils.locales.getText("pisell2.text.goodpass.code-invalid") : content[result == null ? void 0 : result.unavailableReason] || import_utils.locales.getText("pisell2.text.goodpass.code-invalid-cart")
          )
        });
      }
    } catch (error) {
      console.warn("[venueBooking] scanCode failed", error);
      import_antd.message.error({ content: String(import_utils.locales.getText("pisell2.text.goodpass.code-invalid") || "兑换码无效") });
    }
  }, [code, venueBookingModule, runModuleAction, syncAfterDiscountChange]);
  const onDiscountChange = (0, import_react.useCallback)(async (val) => {
    try {
      await runModuleAction("setDiscountSelected", {
        discountId: val.id,
        isSelected: val.isSelected
      });
      await syncAfterDiscountChange();
    } catch (error) {
      console.warn("[venueBooking] setDiscountSelected failed", error);
    }
  }, [runModuleAction, syncAfterDiscountChange]);
  const mixedSort = (0, import_react.useCallback)((val) => {
    return [...val].sort((a, b) => Number(b == null ? void 0 : b.isSelected) - Number(a == null ? void 0 : a.isSelected));
  }, []);
  const renderLoginCard = () => {
    if (isLogin) return null;
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "login-card" }, /* @__PURE__ */ import_react.default.createElement(import_UserCircle.default, { style: { fontSize: 28 } }), /* @__PURE__ */ import_react.default.createElement("div", { className: "text-wrapper", style: { maxWidth: "none" } }, /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellText,
      {
        className: "login-btn",
        onClick: () => {
          openLoginModal({
            callback: () => {
            }
          });
        }
      },
      import_utils.locales.getText("pisell2.text.goodpass.login")
    ), " ", /* @__PURE__ */ import_react.default.createElement("span", null, import_utils.locales.getText("pisell2.text.goodpass.login-tip"))));
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__appointment-booking-voucher-container" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "voucher-title" }, /* @__PURE__ */ import_react.default.createElement("span", { className: "voucher-title-left" }, import_utils.locales.getText("pisell2.text.goodpass")), /* @__PURE__ */ import_react.default.createElement(
    "span",
    {
      className: "voucher-title-right",
      onClick: () => setShowDetail(!showDetail)
    },
    import_utils.locales.getText("pisell2.text.details"),
    " ",
    /* @__PURE__ */ import_react.default.createElement(
      import_ChevronDown.default,
      {
        className: (0, import_classnames.default)("voucher-right-icon", { show: showDetail })
      }
    )
  )), /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("voucher-detail", { show: showDetail }) }, availableData.length > 0 ? /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellGoodPassCard,
    {
      style: { width: "100%" },
      dataSource: availableData,
      mixedSort,
      onChange: onDiscountChange
    }
  ) : null, renderLoginCard(), /* @__PURE__ */ import_react.default.createElement("div", { className: "voucher-code-input-wrapper" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "voucher-code-input" }, /* @__PURE__ */ import_react.default.createElement("div", { style: { whiteSpace: "nowrap" } }, import_utils.locales.getText("pisell2.text.goodpass.code"), ":"), /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellInput,
    {
      value: code,
      allowClear: true,
      onChange: (e) => {
        var _a;
        return setCode(((_a = e.target) == null ? void 0 : _a.value) || "");
      },
      size: "small",
      placeholder: import_utils.locales.getText("pisell2.text.goodpass.enter-code")
    }
  )), /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { size: "small", onClick: onScanCode, disabled: !code.trim() }, import_utils.locales.getText("pisell2.text.apply")))));
}
