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 dayjs from 'dayjs';
import { resolveIsMultiDayProduct } from "./resolveIsMultiDayProduct";
/**
 * 将跨日弹窗结果写入 cacheItem._extend（对齐 `createCacheItemFromParams` 的 start/end）。
 */
export function patchCacheItemDateRange(cacheItem, range) {
  var _range$duration, _cacheItem$_extend, _cacheItem$_extend2, _cacheItem$_extend3;
  var start = dayjs(range.startDate);
  var end = dayjs(range.endDate);
  var duration = (_range$duration = range.duration) !== null && _range$duration !== void 0 ? _range$duration : end.isValid() && start.isValid() ? Math.max(1, end.diff(start, 'day')) : undefined;
  var startTime = (_cacheItem$_extend = cacheItem._extend) !== null && _cacheItem$_extend !== void 0 && _cacheItem$_extend.start_time && String(cacheItem._extend.start_time).trim() !== '' ? cacheItem._extend.start_time : start.isValid() ? start.format('HH:mm') : undefined;
  return _objectSpread(_objectSpread({}, cacheItem), {}, {
    _extend: _objectSpread(_objectSpread(_objectSpread({}, cacheItem._extend), {}, {
      start_date: start.isValid() ? start : (_cacheItem$_extend2 = cacheItem._extend) === null || _cacheItem$_extend2 === void 0 ? void 0 : _cacheItem$_extend2.start_date,
      end_date: end.isValid() ? end : (_cacheItem$_extend3 = cacheItem._extend) === null || _cacheItem$_extend3 === void 0 ? void 0 : _cacheItem$_extend3.end_date
    }, startTime ? {
      start_time: startTime,
      end_time: startTime
    } : {}), duration !== undefined ? {
      duration: duration
    } : {})
  });
}

/**
 * 跨日商品未选区间时，对齐 info2 addService：默认 end = start + 1 天。
 */
export function applyDefaultMultiDayRangeIfNeeded(cacheItem, catalogItem, anchorDate) {
  var _cacheItem$_extend4, _cacheItem$_extend5;
  if (!resolveIsMultiDayProduct(cacheItem, catalogItem)) {
    return cacheItem;
  }
  if (cacheItem !== null && cacheItem !== void 0 && (_cacheItem$_extend4 = cacheItem._extend) !== null && _cacheItem$_extend4 !== void 0 && _cacheItem$_extend4.end_date) {
    return cacheItem;
  }
  var start = dayjs((cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend5 = cacheItem._extend) === null || _cacheItem$_extend5 === void 0 ? void 0 : _cacheItem$_extend5.start_date) || anchorDate || undefined);
  if (!start.isValid()) {
    return cacheItem;
  }
  return patchCacheItemDateRange(cacheItem, {
    startDate: start.format('YYYY-MM-DD'),
    endDate: start.add(1, 'day').format('YYYY-MM-DD'),
    duration: 1
  });
}