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/editCartLineProduct.ts
var editCartLineProduct_exports = {};
__export(editCartLineProduct_exports, {
  editCartLineProduct: () => editCartLineProduct
});
module.exports = __toCommonJS(editCartLineProduct_exports);
var import_utils = require("../../../components/booking/info/service2/utils");
var import_buildProductLineUpdates = require("./buildProductLineUpdates");
function findOrderLine(item, tempOrder) {
  var _a, _b, _c;
  const uid = ((_a = item._identity) == null ? void 0 : _a.unique_identification_number) ?? ((_b = item.metadata) == null ? void 0 : _b.unique_identification_number);
  if (!uid || !((_c = tempOrder == null ? void 0 : tempOrder.products) == null ? void 0 : _c.length)) return null;
  return tempOrder.products.find(
    (line) => {
      var _a2;
      return String((_a2 = line.metadata) == null ? void 0 : _a2.unique_identification_number) === String(uid);
    }
  ) ?? null;
}
function findCatalogProduct(productId, catalog) {
  if (productId == null) return void 0;
  return catalog.find((p) => String(p.id) === String(productId));
}
async function editCartLineProduct(input) {
  var _a, _b, _c;
  const { item, bookingTicket, updateItem, normalProductDetailBridge } = input;
  const tempOrder = ((_a = bookingTicket.getTempOrder) == null ? void 0 : _a.call(bookingTicket)) ?? input.tempOrder ?? null;
  const orderLine = findOrderLine(item, tempOrder);
  if (!orderLine) {
    throw new Error("[SalesSdk] 未在 tempOrder.products 找到对应商品行");
  }
  if (typeof bookingTicket.buildNormalProductCacheItemFromOrderLine !== "function") {
    throw new Error(
      "[SalesSdk] bookingTicket.buildNormalProductCacheItemFromOrderLine 不可用，请升级 pisellos"
    );
  }
  const catalog = ((_c = (_b = bookingTicket.getProductCatalog) == null ? void 0 : _b.call(bookingTicket)) == null ? void 0 : _c.products) ?? [];
  const sourceProduct = findCatalogProduct(orderLine.product_id, catalog);
  if (sourceProduct && !(0, import_utils.isNormalProductByDurationSchedule)(sourceProduct)) {
    throw new Error("[SalesSdk] 该商品不是普通商品，请使用 editCartLineBooking");
  }
  const cacheItem = bookingTicket.buildNormalProductCacheItemFromOrderLine({
    product: orderLine,
    sourceProduct: sourceProduct ?? null
  });
  const editedResult = await normalProductDetailBridge.request({
    mode: "edit",
    cacheItem,
    skuDetailConfig: sourceProduct == null ? void 0 : sourceProduct.skuDetailConfig
  });
  if (!editedResult || editedResult.mode !== "edit") {
    return { status: "cancelled" };
  }
  const edited = editedResult.cacheItem;
  if (typeof bookingTicket.transformDetailToProductAndBooking !== "function") {
    throw new Error("[SalesSdk] bookingTicket.transformDetailToProductAndBooking 不可用");
  }
  const catalogItem = sourceProduct ?? { id: orderLine.product_id };
  const { product: productPatch } = bookingTicket.transformDetailToProductAndBooking({
    item: catalogItem,
    cacheItem: edited
  });
  const identity = item._identity;
  const products = await updateItem({
    product_id: identity.product_id,
    product_variant_id: identity.product_variant_id,
    unique_identification_number: identity.unique_identification_number,
    product_option_item: identity.product_option_item,
    product_bundle: identity.product_bundle,
    updates: (0, import_buildProductLineUpdates.buildProductLineUpdates)(orderLine, productPatch)
  });
  return { status: "updated", products };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  editCartLineProduct
});
