var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/salesSdk/utils/assignHolderToCartLine.ts
var assignHolderToCartLine_exports = {};
__export(assignHolderToCartLine_exports, {
  assignHolderToCartLine: () => assignHolderToCartLine,
  assignHolderToCartLineWithFeedback: () => assignHolderToCartLineWithFeedback
});
module.exports = __toCommonJS(assignHolderToCartLine_exports);
var import_antd = require("antd");
var import_buildEditedCacheItem = require("../bookingEditService/buildEditedCacheItem");
var import_applyHolderToCacheItem = require("./applyHolderToCacheItem");
var import_buildProductLineUpdates = require("./buildProductLineUpdates");
var import_identity = require("./identity");
function needsHolderSplit(cacheItem, holderIds) {
  var _a;
  const quantity = ((_a = cacheItem == null ? void 0 : cacheItem._extend) == null ? void 0 : _a.quantity) ?? 1;
  return holderIds.length > 1 || quantity > 1 && holderIds.length >= 1;
}
function mergeHolderIntoCacheItem(cacheItem, holderIds, options = {}) {
  const ext = (cacheItem == null ? void 0 : cacheItem._extend) || {};
  if (!needsHolderSplit(cacheItem, holderIds)) {
    return (0, import_applyHolderToCacheItem.applyHolderToCacheItem)(cacheItem, holderIds, options);
  }
  return (0, import_buildEditedCacheItem.buildEditedCacheItem)({
    values: {
      holder_id: holderIds,
      total: ext.total ?? ext.price,
      origin_total: ext.origin_total ?? ext.total ?? ext.price,
      quantity: ext.quantity ?? 1
    },
    cacheItem,
    isEditGroup: Boolean(ext.groupId),
    isChangeTotal: false,
    isPriceIncludeTax: cacheItem == null ? void 0 : cacheItem.is_price_include_tax,
    bookingConfig: options.bookingConfig,
    customerId: options.customerId,
    isFranchisee: options.isFranchisee,
    holderOptions: options.holderOptions
  });
}
async function assignHolderToCartLine(params) {
  var _a, _b, _c, _d, _e, _f, _g;
  const {
    line,
    bookingTicket,
    tempOrder,
    updateItem,
    onMutate,
    openHolderSelect,
    holderOptions: holderOptionsFromParams
  } = params;
  if (!openHolderSelect) {
    throw new Error("[SalesSdk] openHolderSelect 未配置");
  }
  if (typeof bookingTicket.transformDetailToProductAndBooking !== "function") {
    throw new Error("[SalesSdk] transformDetailToProductAndBooking 不可用");
  }
  const updateOrderProduct = updateItem ?? (typeof bookingTicket.updateOrderProduct === "function" ? bookingTicket.updateOrderProduct.bind(bookingTicket) : null);
  if (!updateOrderProduct) {
    throw new Error("[SalesSdk] updateOrderProduct 不可用");
  }
  const bookingConfig = ((_a = bookingTicket.getBookingConfig) == null ? void 0 : _a.call(bookingTicket)) ?? null;
  const holderConfig = (bookingConfig == null ? void 0 : bookingConfig.holder) || {};
  const maxSelectedCount = (holderConfig == null ? void 0 : holderConfig.maxSelectedCount) || ((_c = (_b = line.cacheItem) == null ? void 0 : _b._extend) == null ? void 0 : _c.quantity) || 1;
  const holderIds = await openHolderSelect({
    cacheItem: line.cacheItem,
    customerId: (tempOrder == null ? void 0 : tempOrder.customer_id) ?? void 0,
    maxSelectedCount
  });
  if (!(holderIds == null ? void 0 : holderIds.length)) {
    return false;
  }
  const catalog = ((_e = (_d = bookingTicket.getProductCatalog) == null ? void 0 : _d.call(bookingTicket)) == null ? void 0 : _e.products) ?? [];
  const sourceProduct = catalog.find((p) => String(p.id) === String(line.orderLine.product_id)) ?? { id: line.orderLine.product_id };
  const holderOptions = {
    bookingConfig,
    customerId: (tempOrder == null ? void 0 : tempOrder.customer_id) ?? void 0,
    isFranchisee: Boolean((_g = (_f = bookingTicket.getOtherParams) == null ? void 0 : _f.call(bookingTicket)) == null ? void 0 : _g.isFranchisee),
    holderOptions: holderOptionsFromParams
  };
  let edited = mergeHolderIntoCacheItem(line.cacheItem, holderIds, holderOptions);
  const remainder = edited._splitHolderRemainder;
  delete edited._splitHolderRemainder;
  const { product: productPatch, booking: bookingPatch } = bookingTicket.transformDetailToProductAndBooking({
    item: sourceProduct,
    cacheItem: edited
  });
  const identity = (0, import_identity.pickIdentity)(line.orderLine);
  const updateParams = {
    ...identity,
    updates: (0, import_buildProductLineUpdates.buildProductLineUpdates)(line.orderLine, productPatch),
    booking: bookingPatch
  };
  await updateOrderProduct(updateParams);
  if (Array.isArray(remainder) && remainder.length > 0) {
    for (const part of remainder) {
      const splitCacheItem = {
        ...edited,
        _id: part._id,
        _extend: {
          ...edited._extend,
          holder_id: part.holder_id,
          quantity: part.quantity ?? 1
        }
      };
      const splitCacheItemWithHolder = (0, import_applyHolderToCacheItem.applyHolderToCacheItem)(
        splitCacheItem,
        part.holder_id,
        holderOptions
      );
      const transformed = bookingTicket.transformDetailToProductAndBooking({
        item: sourceProduct,
        cacheItem: splitCacheItemWithHolder
      });
      if (typeof bookingTicket.addProductToOrder !== "function") {
        throw new Error("[SalesSdk] addProductToOrder 不可用");
      }
      await bookingTicket.addProductToOrder(
        transformed.product,
        transformed.booking
      );
    }
  }
  onMutate == null ? void 0 : onMutate();
  return true;
}
async function assignHolderToCartLineWithFeedback(params) {
  try {
    return await assignHolderToCartLine(params);
  } catch (error) {
    import_antd.message.error(String((error == null ? void 0 : error.message) ?? error));
    throw error;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  assignHolderToCartLine,
  assignHolderToCartLineWithFeedback
});
