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); }
/**
 * 仅按 booking.product_uid 建索引，避免发明 booking 与商品行之间的其它等价关系。
 */
export function indexBookingsByProductUid(bookings) {
  if (!Array.isArray(bookings) || bookings.length === 0) return {};
  return bookings.reduce(function (acc, booking) {
    var uid = booking === null || booking === void 0 ? void 0 : booking.product_uid;
    if (uid === undefined || uid === null || uid === '') return acc;
    var key = String(uid);
    if (!acc[key]) acc[key] = [];
    acc[key].push(booking);
    return acc;
  }, {});
}

/**
 * 商品行按 unique_identification_number 建 1:1 索引（同 uid 多行时保留首行）。
 */
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;
  }, {});
}

/**
 * 构建购物车 UI 视图：bookings 平铺附带关联商品；items 仅含未关联 booking 的商品行。
 *
 * 商品行直接透传 tempOrder.products，不做 enrichment。
 * 关联规则（1:1）：booking.product_uid ↔ line.metadata.unique_identification_number；
 * 同一 uid 已被更早的 booking 占用时，后续 booking 的 product 为 null。
 */
export function buildCartView(lines, bookings) {
  var productLines = Array.isArray(lines) ? lines : [];
  var bookingList = Array.isArray(bookings) ? bookings : [];
  var linesByUid = indexProductsByUid(productLines);
  var claimedUids = new Set();
  var cartBookings = bookingList.filter(function (booking) {
    return booking.parent_id !== 0;
  }).map(function (booking) {
    var uid = booking === null || booking === void 0 ? void 0 : booking.product_uid;
    var product = null;
    if (uid !== undefined && uid !== null && uid !== '') {
      var key = String(uid);
      if (!claimedUids.has(key)) {
        var line = linesByUid[key];
        if (line) {
          claimedUids.add(key);
          product = line;
        }
      }
    }
    return _objectSpread(_objectSpread({}, booking), {}, {
      _extend: {
        product: product
      }
    });
  });
  var items = productLines.filter(function (line) {
    var _line$metadata2, _line$metadata3;
    // 促销赠品行仅通过主商品 Gift 行展示，不在 items 单独占一行（对齐 legacy `isGift: !!_giftInfo`）
    if ((_line$metadata2 = line.metadata) !== null && _line$metadata2 !== void 0 && _line$metadata2._giftInfo) return false;
    var uid = (_line$metadata3 = line.metadata) === null || _line$metadata3 === void 0 ? void 0 : _line$metadata3.unique_identification_number;
    if (uid === undefined || uid === null || uid === '') return true;
    return !claimedUids.has(String(uid));
  });
  return {
    bookings: cartBookings,
    items: items
  };
}
export function isWalkInCustomer(customer) {
  if (!customer) return true;
  var id = customer.id;
  if (id === undefined || id === null) return true;
  if (typeof id === 'string') {
    return ['', '0', '1'].includes(id);
  }
  return [0, 1].includes(id);
}

/**
 * selected 优先，缺失时回退 tempOrder.customer_*，统一输出 UI 客户视图。
 */
export function buildCustomerView(selected, tempOrder) {
  if (selected && selected.id !== undefined && selected.id !== null && selected.id !== '' && selected.id !== 0) {
    var _selected$name;
    return {
      id: selected.id,
      name: (_selected$name = selected.name) !== null && _selected$name !== void 0 ? _selected$name : '',
      phone: selected.phone,
      email: selected.email,
      country_calling_code: selected.country_calling_code,
      cover: selected.cover,
      isWalkIn: isWalkInCustomer(selected)
    };
  }
  if (tempOrder && (tempOrder.customer_id != null || tempOrder.customer_name)) {
    var _tempOrder$customer_i, _tempOrder$customer_n;
    var id = (_tempOrder$customer_i = tempOrder.customer_id) !== null && _tempOrder$customer_i !== void 0 ? _tempOrder$customer_i : '';
    return {
      id: id,
      name: (_tempOrder$customer_n = tempOrder.customer_name) !== null && _tempOrder$customer_n !== void 0 ? _tempOrder$customer_n : '',
      phone: tempOrder.phone,
      email: tempOrder.email,
      country_calling_code: tempOrder.country_calling_code,
      cover: undefined,
      isWalkIn: isWalkInCustomer({
        id: id
      })
    };
  }
  return null;
}