function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
/**
 * 商品行按 unique_identification_number 建 1:1 索引（同 uid 多行时保留首行）。
 * 与 OS BookingTicket `buildCartView` 对齐。
 */
function indexProductsByUid(lines) {
  return lines.reduce(function (acc, line) {
    var _line$metadata;
    var uid = (_line$metadata = line.metadata) === null || _line$metadata === void 0 ? void 0 : _line$metadata.unique_identification_number;
    if (uid === undefined || uid === null || uid === '') return acc;
    var key = String(uid);
    if (!acc[key]) acc[key] = line;
    return acc;
  }, {});
}

/**
 * 将 tempOrder.products 与 bookings 按 OS `buildCartView` 规则拼接为购物车展示行：
 *
 * 1. 先取 `parent_id !== 0` 的 booking，按列表顺序 1:1 关联
 *    `booking.product_uid ↔ line.metadata.unique_identification_number` 的商品行；
 * 2. 再追加未被任何 booking 占用的商品行。
 *
 * 关联规则与 OS 一致，不发明 booking_id ↔ product 的其它等价关系。
 */
export function buildCartDisplayLines(products, bookings) {
  var productLines = Array.isArray(products) ? products : [];
  var bookingList = Array.isArray(bookings) ? bookings : [];
  if (productLines.length === 0) return [];
  if (bookingList.length === 0) return _toConsumableArray(productLines);
  var linesByUid = indexProductsByUid(productLines);
  var claimedUids = new Set();
  var fromBookings = [];
  bookingList.forEach(function (booking) {
    if (booking.parent_id === 0) return;
    var uid = booking === null || booking === void 0 ? void 0 : booking.product_uid;
    if (uid === undefined || uid === null || uid === '') return;
    var key = String(uid);
    if (claimedUids.has(key)) return;
    var line = linesByUid[key];
    if (!line) return;
    claimedUids.add(key);
    fromBookings.push(line);
  });
  var unclaimed = productLines.filter(function (line) {
    var _line$metadata2;
    var uid = (_line$metadata2 = line.metadata) === null || _line$metadata2 === void 0 ? void 0 : _line$metadata2.unique_identification_number;
    if (uid === undefined || uid === null || uid === '') return true;
    return !claimedUids.has(String(uid));
  });
  return [].concat(fromBookings, _toConsumableArray(unclaimed));
}