function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { getMergedBookingSources } from "./mergeBookingsForDisplay";
function pickCartLineUid(item) {
  var _item$_identity$uniqu, _item$_identity, _item$metadata;
  var uid = (_item$_identity$uniqu = (_item$_identity = item._identity) === null || _item$_identity === void 0 ? void 0 : _item$_identity.unique_identification_number) !== null && _item$_identity$uniqu !== void 0 ? _item$_identity$uniqu : (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.unique_identification_number;
  if (uid === undefined || uid === null || uid === '') return null;
  return String(uid);
}
function pickBookingProductUid(booking) {
  var _booking$product_uid, _booking$metadata;
  var uid = (_booking$product_uid = booking.product_uid) !== null && _booking$product_uid !== void 0 ? _booking$product_uid : (_booking$metadata = booking.metadata) === null || _booking$metadata === void 0 ? void 0 : _booking$metadata.product_uid;
  if (uid === undefined || uid === null || uid === '') return null;
  return String(uid);
}
function findItemByUid(uid, items) {
  return items.find(function (line) {
    return pickCartLineUid(line) === uid;
  });
}
function findItemByBookingId(booking, items) {
  var bookingId = booking.booking_id;
  if (bookingId === undefined || bookingId === null || bookingId === '') {
    return undefined;
  }
  return items.find(function (line) {
    var _line$_extend$booking, _line$_extend;
    return String((_line$_extend$booking = (_line$_extend = line._extend) === null || _line$_extend === void 0 || (_line$_extend = _line$_extend.booking) === null || _line$_extend === void 0 ? void 0 : _line$_extend.booking_id) !== null && _line$_extend$booking !== void 0 ? _line$_extend$booking : '') === String(bookingId);
  });
}

/**
 * ProductCard LineItem 会把商品行 convert 后再触发 onCard，原始 enriched 行挂在 `_origin`。
 */
function normalizeProductCartTarget(target) {
  if (target !== null && target !== void 0 && target._identity) {
    return target;
  }
  var origin = target === null || target === void 0 ? void 0 : target._origin;
  if (origin !== null && origin !== void 0 && origin._identity) {
    return origin;
  }
  return null;
}

/**
 * 将 booking 行或商品行统一解析为可编辑的 `SalesSdkCartItemView`。
 *
 * 解析顺序：
 * 1. 已是 enriched 商品行（含 ProductCard `_origin` 还原）
 * 2. booking 卡片：`getCart` 挂在 `_extend.product` 上的 enriched 行（items 可能为空）
 * 3. 通过 uid / booking_id 在 items 中反查（历史兼容）
 */
export function resolveEditCartLineTarget(target, items) {
  var _extend, _target$metadata$uniq, _target$metadata;
  var normalized = normalizeProductCartTarget(target);
  if (normalized) {
    return normalized;
  }
  var bookingProduct = target === null || target === void 0 || (_extend = target._extend) === null || _extend === void 0 ? void 0 : _extend.product;
  if (bookingProduct) {
    var _normalizeProductCart;
    var productLine = (_normalizeProductCart = normalizeProductCartTarget(bookingProduct)) !== null && _normalizeProductCart !== void 0 ? _normalizeProductCart : bookingProduct;
    if (pickCartLineUid(productLine)) {
      var mergedSources = getMergedBookingSources(target);
      if (mergedSources.length > 1) {
        var _extend$displayQuanti, _extend2;
        return _objectSpread(_objectSpread({}, productLine), {}, {
          _extend: _objectSpread(_objectSpread({}, productLine._extend), {}, {
            isMergedBookingLine: true,
            displayQuantity: (_extend$displayQuanti = (_extend2 = target._extend) === null || _extend2 === void 0 ? void 0 : _extend2.displayQuantity) !== null && _extend$displayQuanti !== void 0 ? _extend$displayQuanti : productLine.num,
            mergedBookingSources: mergedSources
          })
        });
      }
      return productLine;
    }
  }
  var uidFromCard = (_target$metadata$uniq = target === null || target === void 0 || (_target$metadata = target.metadata) === null || _target$metadata === void 0 ? void 0 : _target$metadata.unique_identification_number) !== null && _target$metadata$uniq !== void 0 ? _target$metadata$uniq : typeof (target === null || target === void 0 ? void 0 : target._id) === 'string' ? target._id : null;
  if (uidFromCard) {
    var matched = findItemByUid(String(uidFromCard), items);
    if (matched) return matched;
  }
  var booking = target;
  var productUid = pickBookingProductUid(booking);
  if (productUid) {
    var _matched = findItemByUid(productUid, items);
    if (_matched) return _matched;
  }
  var matchedByBookingId = findItemByBookingId(booking, items);
  if (matchedByBookingId) {
    return matchedByBookingId;
  }
  throw new Error('[SalesSdk] 未找到对应购物车商品行，无法编辑');
}

/** 是否应走资源/时间编辑（openBookingEdit）而非普通商品弹窗。 */
export function shouldEditCartLineAsBooking(item) {
  var _item$_extend;
  return ((_item$_extend = item._extend) === null || _item$_extend === void 0 ? void 0 : _item$_extend.booking) != null;
}