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/editCartLine.ts
var editCartLine_exports = {};
__export(editCartLine_exports, {
  resolveEditCartLineTarget: () => resolveEditCartLineTarget,
  shouldEditCartLineAsBooking: () => shouldEditCartLineAsBooking
});
module.exports = __toCommonJS(editCartLine_exports);
function pickCartLineUid(item) {
  var _a, _b;
  const uid = ((_a = item._identity) == null ? void 0 : _a.unique_identification_number) ?? ((_b = item.metadata) == null ? void 0 : _b.unique_identification_number);
  if (uid === void 0 || uid === null || uid === "") return null;
  return String(uid);
}
function pickBookingProductUid(booking) {
  var _a;
  const uid = booking.product_uid ?? ((_a = booking.metadata) == null ? void 0 : _a.product_uid);
  if (uid === void 0 || uid === null || uid === "") return null;
  return String(uid);
}
function findItemByUid(uid, items) {
  return items.find((line) => pickCartLineUid(line) === uid);
}
function findItemByBookingId(booking, items) {
  const bookingId = booking.booking_id;
  if (bookingId === void 0 || bookingId === null || bookingId === "") {
    return void 0;
  }
  return items.find(
    (line) => {
      var _a, _b;
      return String(((_b = (_a = line._extend) == null ? void 0 : _a.booking) == null ? void 0 : _b.booking_id) ?? "") === String(bookingId);
    }
  );
}
function normalizeProductCartTarget(target) {
  if (target == null ? void 0 : target._identity) {
    return target;
  }
  const origin = target == null ? void 0 : target._origin;
  if (origin == null ? void 0 : origin._identity) {
    return origin;
  }
  return null;
}
function resolveEditCartLineTarget(target, items) {
  var _a, _b;
  const normalized = normalizeProductCartTarget(target);
  if (normalized) {
    return normalized;
  }
  const bookingProduct = (_a = target == null ? void 0 : target._extend) == null ? void 0 : _a.product;
  if (bookingProduct) {
    const productLine = normalizeProductCartTarget(bookingProduct) ?? bookingProduct;
    if (pickCartLineUid(productLine)) {
      return productLine;
    }
  }
  const uidFromCard = ((_b = target == null ? void 0 : target.metadata) == null ? void 0 : _b.unique_identification_number) ?? (typeof (target == null ? void 0 : target._id) === "string" ? target._id : null);
  if (uidFromCard) {
    const matched = findItemByUid(String(uidFromCard), items);
    if (matched) return matched;
  }
  const booking = target;
  const productUid = pickBookingProductUid(booking);
  if (productUid) {
    const matched = findItemByUid(productUid, items);
    if (matched) return matched;
  }
  const matchedByBookingId = findItemByBookingId(booking, items);
  if (matchedByBookingId) {
    return matchedByBookingId;
  }
  throw new Error("[SalesSdk] 未找到对应购物车商品行，无法编辑");
}
function shouldEditCartLineAsBooking(item) {
  var _a;
  return ((_a = item._extend) == null ? void 0 : _a.booking) != null;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  resolveEditCartLineTarget,
  shouldEditCartLineAsBooking
});
