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); }
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); }
/**
 * 将 booking 上的分钟/天数 duration 转为 catalog 结构，供 `isNormalProductByDurationSchedule` 识别预约商品。
 *
 * @example
 * resolveTopLevelDurationFromBooking({ duration: 20, sub_type: 'minutes' });
 * // => { type: 'minutes', value: 20 }
 */
function resolveTopLevelDurationFromBooking(booking) {
  var raw = booking === null || booking === void 0 ? void 0 : booking.duration;
  if (raw == null || raw === '') return null;
  if (_typeof(raw) === 'object' && raw.type) return raw;
  var subType = (booking === null || booking === void 0 ? void 0 : booking.sub_type) === 'days' ? 'days' : 'minutes';
  var value = Number(raw);
  if (!Number.isFinite(value) || value <= 0) return null;
  return {
    type: subType,
    value: value
  };
}

/**
 * 规格弹窗 detail 可能缺少 `duration`，合并列表商品上的 duration 供跨日判断与 OS 转换使用。
 * 购物车反查 cacheItem 时 catalog 可能未命中，此时回退 `_extend.duration` 或 booking.duration。
 *
 * @example
 * mergeCatalogDurationIntoCacheItem(cacheItem, catalogItem);
 */
export function mergeCatalogDurationIntoCacheItem(cacheItem, catalogItem, booking) {
  var _cacheItem$duration, _cacheItem$_extend;
  if (cacheItem !== null && cacheItem !== void 0 && (_cacheItem$duration = cacheItem.duration) !== null && _cacheItem$duration !== void 0 && _cacheItem$duration.type) {
    return cacheItem;
  }
  if (catalogItem !== null && catalogItem !== void 0 && catalogItem.duration) {
    return _objectSpread(_objectSpread({}, cacheItem), {}, {
      duration: catalogItem.duration
    });
  }
  var extDuration = cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend = cacheItem._extend) === null || _cacheItem$_extend === void 0 ? void 0 : _cacheItem$_extend.duration;
  if (_typeof(extDuration) === 'object' && extDuration !== null && extDuration !== void 0 && extDuration.type) {
    return _objectSpread(_objectSpread({}, cacheItem), {}, {
      duration: extDuration
    });
  }
  if (typeof extDuration === 'number' && extDuration > 0) {
    return _objectSpread(_objectSpread({}, cacheItem), {}, {
      duration: {
        type: 'minutes',
        value: extDuration
      }
    });
  }
  var bookingDuration = resolveTopLevelDurationFromBooking(booking);
  if (bookingDuration) {
    return _objectSpread(_objectSpread({}, cacheItem), {}, {
      duration: bookingDuration
    });
  }
  return cacheItem;
}