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/appointmentBooking/components/Cart/Item/index.tsx
var Item_exports = {};
__export(Item_exports, {
  default: () => Item_default
});
module.exports = __toCommonJS(Item_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_shoppingCart = __toESM(require("../../../../shoppingCart"));
var import_ContinueButton = __toESM(require("../ContinueButton"));
var import_hooks = require("../../../hooks");
var import_context = require("../../../context");
var import_hooks2 = require("../../../hooks");
var import_utils2 = require("../../../utils");
var import_Voucher = __toESM(require("../../Voucher"));
var import_Total = __toESM(require("./Total"));
var import_Deposit = __toESM(require("../../Deposit"));
var import_utils3 = require("../utils");
var import_index = require("./index.less");
var CartItem = ({ onNext, renderContactInfo }) => {
  var _a, _b, _c, _d, _e;
  const appointmentBooking = (0, import_hooks.useAppointmentBooking)();
  const {
    carts,
    currentStep,
    setResourceOtherParams,
    resourceOtherParams,
    summary,
    isLastStep,
    currentSelectedResources,
    currentStepParams,
    isErrorHolder
  } = (0, import_context.useAppointmentBookingContext)();
  const { accountLists, getHolders, addAccount } = (0, import_hooks2.useAppointmentBookingAccount)();
  const { getResourceList } = (0, import_hooks2.useAppointmentBookingResource)();
  const { isFormSubject, bookingSubject, isRetailTemplate } = (0, import_hooks2.useAppointmentBookingSetting)();
  const { isLogin } = (0, import_hooks2.useAppointmentBookingLogin)();
  const utils = (0, import_hooks2.contextUtils)();
  const { businessUtils } = utils;
  const { deposit } = summary;
  const currentUser = (0, import_react.useMemo)(() => {
    return (0, import_utils2.formatCurrentUser)(utils) || {};
  }, []);
  const holderIds = (0, import_react.useMemo)(() => {
    if (isFormSubject) return [];
    return accountLists.map((item) => item.id);
  }, [accountLists, isFormSubject]);
  const holders = (0, import_react.useMemo)(() => {
    if (isFormSubject) return [];
    return (0, import_utils2.formatHolder)(accountLists);
  }, [accountLists, isFormSubject]);
  const hasGoodPassApp = !!((_a = businessUtils == null ? void 0 : businessUtils.getAppByModal) == null ? void 0 : _a.call(businessUtils, "goodpass"));
  const hasDiscountCardApp = !!((_b = businessUtils == null ? void 0 : businessUtils.getAppByModal) == null ? void 0 : _b.call(businessUtils, "discountcard"));
  const handleClickCard = (e, type, val) => {
    console.log(e, type, val, "handleClickCard");
    if (type === "note") {
      return appointmentBooking.updateCart({
        _id: e == null ? void 0 : e._id,
        ...val
      });
    }
    if (type === "delete") {
      return appointmentBooking.deleteCart(e == null ? void 0 : e._id);
    }
    if (type === "delete_holders") {
      return appointmentBooking.removeAccount(val);
    }
    if (type === "change_resource") {
      setResourceOtherParams({
        ...resourceOtherParams,
        errorList: null
      });
      val.like_status = "like";
      appointmentBooking.updateCart({
        _id: e == null ? void 0 : e._id,
        resources: (0, import_utils2.updateCartResources)(e, val, currentSelectedResources)
      });
      getResourceList();
      return;
    }
    if (type === "change_holder") {
      if (!val) {
        appointmentBooking.deleteCartItemInfo("holder", e == null ? void 0 : e._id);
      } else {
        appointmentBooking.updateCart({
          _id: e == null ? void 0 : e._id,
          account: val
        });
      }
      return;
    }
    if (type === "add_holder") {
      const relationShipFormId = bookingSubject == null ? void 0 : bookingSubject.relationship_form_id;
      return (0, import_utils2.openForm)({
        form_id: relationShipFormId || (bookingSubject == null ? void 0 : bookingSubject.form_id),
        title: import_utils.locales.getText("pisell2.event.create.form"),
        utils,
        callback: async (res) => {
          if ((res == null ? void 0 : res.status) === "success") {
            let result = void 0;
            if (!relationShipFormId) {
              result = await addAccount(res == null ? void 0 : res.values, "holder");
            } else {
              result = await (0, import_utils3.handleAddHolder)({
                res,
                bookingSubject,
                accountLists,
                cartItem: e,
                getHolders,
                appointmentBooking
              });
            }
            if (result) {
              appointmentBooking.updateCart({
                _id: e == null ? void 0 : e._id,
                account: result
              });
            }
          }
        }
      });
    }
  };
  const products = (0, import_react.useMemo)(() => {
    if (!(carts == null ? void 0 : carts.length)) return [];
    carts.forEach((item) => {
      item.isShowChangeHolder = !appointmentBooking.isTargetCartIdNormalProduct(
        item == null ? void 0 : item._id
      );
    });
    if ((currentSelectedResources == null ? void 0 : currentSelectedResources.length) > 0) {
      const newCarts = carts.map((item) => {
        const currentResourceData = currentSelectedResources.find(
          (resource) => (resource == null ? void 0 : resource._id) === (item == null ? void 0 : item._id)
        );
        const multiResources = (0, import_utils2.getMultiResources)(
          item,
          currentResourceData == null ? void 0 : currentResourceData.currentSelectedResource,
          currentStepParams == null ? void 0 : currentStepParams.key
        );
        return {
          ...item,
          isMultipleResource: true,
          multiResources,
          isErrorHolder: isErrorHolder || false
        };
      });
      return newCarts;
    } else {
      return carts;
    }
  }, [carts, currentSelectedResources, currentStepParams == null ? void 0 : currentStepParams.key, isErrorHolder]);
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: `appointment-cart-item-wrap ${(0, import_utils.isMobile)() ? "appointment-cart-mobile-item-wrap" : ""}`
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "content" }, renderContactInfo ? renderContactInfo() : null, /* @__PURE__ */ import_react.default.createElement(
      import_shoppingCart.default,
      {
        productParams: {
          products,
          hideAddBtn: false,
          holders,
          disabledEdit: isLastStep,
          isShowChangeResource: (_d = (_c = currentStep == null ? void 0 : currentStep.step) == null ? void 0 : _c.key) == null ? void 0 : _d.startsWith("select-resource"),
          isShowDelete: ((_e = currentStep == null ? void 0 : currentStep.step) == null ? void 0 : _e.key) === "select-service",
          isShowAmountFooter: true,
          isShowPackageNote: false,
          isShowChangeHolder: isLastStep ? false : isFormSubject && isLogin,
          isShowHolder: isFormSubject,
          isShowAddHolderButton: true
        },
        collapseParams: {
          activeKey: holderIds,
          ghost: true
        },
        platform: "h5",
        emptyText: import_utils.locales.getText("pisell2.event.empty.service"),
        isOpenGroup: !!(holderIds == null ? void 0 : holderIds.length),
        isHideClearCart: true,
        onChange: (e, type, key) => handleClickCard(e, type, key)
      }
    )),
    /* @__PURE__ */ import_react.default.createElement("div", { className: "appointment-footer" }, hasGoodPassApp || hasDiscountCardApp ? /* @__PURE__ */ import_react.default.createElement(import_Voucher.default, { currentUser, products: carts }) : null, /* @__PURE__ */ import_react.default.createElement(import_Total.default, null), (carts == null ? void 0 : carts.length) > 0 && (deposit == null ? void 0 : deposit.hasDeposit) ? /* @__PURE__ */ import_react.default.createElement(import_Deposit.default, null) : null, /* @__PURE__ */ import_react.default.createElement(import_ContinueButton.default, { onNext }))
  ));
};
var Item_default = CartItem;
