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); }
/**
 * 从 BookingContext resourcesOriginMap 解析资源展示名（与 info2 serviceItem 对齐）。
 *
 * @example
 * resolveResourceDisplayName(41077, resourcesOriginMap); // '小包 01 (10人) EN'
 */
export function resolveResourceDisplayName(relationId, resourcesOriginMap) {
  var _resourcesOriginMap$S, _ref, _ref2, _origin$main_field;
  if (relationId == null || relationId === '') return '';
  var origin = (_resourcesOriginMap$S = resourcesOriginMap[String(relationId)]) !== null && _resourcesOriginMap$S !== void 0 ? _resourcesOriginMap$S : resourcesOriginMap[Number(relationId)];
  if (!origin) return '';
  return String((_ref = (_ref2 = (_origin$main_field = origin.main_field) !== null && _origin$main_field !== void 0 ? _origin$main_field : origin.room_name) !== null && _ref2 !== void 0 ? _ref2 : origin.labelText) !== null && _ref !== void 0 ? _ref : '').trim();
}

/**
 * 购物车 booking 卡片：补齐 resources[].metadata.resource_name（仅 UI，不写 OS tempOrder）。
 *
 * 新建 seed 的 booking.resources.metadata 可能为空；LineItem Resources 依赖 resource_name 展示桌台名。
 *
 * @example
 * enrichCartBookingResourceDisplay(booking, bookingTicket.getResourcesOriginMap());
 */
export function enrichCartBookingResourceDisplay(booking) {
  var _resources$;
  var resourcesOriginMap = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var rows = booking.resources;
  if (!Array.isArray(rows) || rows.length === 0) {
    return booking;
  }
  var changed = false;
  var resources = rows.map(function (resource) {
    var metadata = _objectSpread({}, (resource === null || resource === void 0 ? void 0 : resource.metadata) || {});
    var existingName = metadata.resource_name;
    if (existingName) {
      return resource;
    }
    var resolvedName = resolveResourceDisplayName(resource === null || resource === void 0 ? void 0 : resource.relation_id, resourcesOriginMap);
    if (!resolvedName) {
      return resource;
    }
    changed = true;
    return _objectSpread(_objectSpread({}, resource), {}, {
      metadata: _objectSpread(_objectSpread({}, metadata), {}, {
        resource_name: resolvedName
      })
    });
  });
  if (!changed) {
    return booking;
  }
  var relationFormName = resources.length === 1 ? (_resources$ = resources[0]) === null || _resources$ === void 0 || (_resources$ = _resources$.metadata) === null || _resources$ === void 0 ? void 0 : _resources$.resource_name : booking.relation_form_name;
  return _objectSpread(_objectSpread({}, booking), {}, {
    resources: resources
  }, relationFormName ? {
    relation_form_name: relationFormName
  } : {});
}