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 { getIsEject, getIsOnlySession, isSessionProduct } from "../../../components/booking/utils";
import { getIsShowNumber } from "../../../components/ticketBooking/utils";
/**
 * 构造 session 商品详情弹窗的时间回显值，优先使用已选场次本身的起止时间。
 */
function buildSessionStartEndTime(cacheItem) {
  var _extend$other, _session$start_at, _session$end_at;
  var extend = (cacheItem === null || cacheItem === void 0 ? void 0 : cacheItem._extend) || {};
  var session = (extend === null || extend === void 0 || (_extend$other = extend.other) === null || _extend$other === void 0 ? void 0 : _extend$other.session) || {};
  var startDate = dayjs(extend.start_date);
  var endDate = dayjs(extend.end_date);
  var startAt = (_session$start_at = session === null || session === void 0 ? void 0 : session.start_at) !== null && _session$start_at !== void 0 ? _session$start_at : startDate.isValid() && extend.start_time ? dayjs("".concat(startDate.format('YYYY-MM-DD'), " ").concat(extend.start_time)) : extend.start_date;
  var endAt = (_session$end_at = session === null || session === void 0 ? void 0 : session.end_at) !== null && _session$end_at !== void 0 ? _session$end_at : endDate.isValid() && extend.end_time ? dayjs("".concat(endDate.format('YYYY-MM-DD'), " ").concat(extend.end_time)) : extend.end_date;
  return {
    startDate: startAt,
    endDate: endAt,
    duration: extend.duration
  };
}

/**
 * 从资源编辑抽屉内的 cacheItem 构造 `openProductDetail` 入参（对齐 Info2 editService handleEdit）。
 *
 * @example
 * const payload = buildEditProductDetailPayload({
 *   cacheItem,
 *   bookingState,
 *   formValues: form.getFieldsValue(),
 * });
 * const detailResult = await uiHosts.openProductDetail?.(payload);
 */
export function buildEditProductDetailPayload(options) {
  var _bookingState$date$va, _bookingState$date, _bookingState$date$fo, _formValues$quantity, _cacheItem$_extend, _cacheItem$_extend2, _options$customerId, _bookingState$client;
  var cacheItem = options.cacheItem,
    bookingState = options.bookingState,
    formValues = options.formValues;
  var isSession = isSessionProduct(cacheItem);
  var date = (_bookingState$date$va = (_bookingState$date = bookingState.date) === null || _bookingState$date === void 0 || (_bookingState$date = _bookingState$date.value) === null || _bookingState$date === void 0 || (_bookingState$date$fo = _bookingState$date.format) === null || _bookingState$date$fo === void 0 ? void 0 : _bookingState$date$fo.call(_bookingState$date, 'YYYY-MM-DD')) !== null && _bookingState$date$va !== void 0 ? _bookingState$date$va : dayjs().format('YYYY-MM-DD');
  var sessionValue = {};
  if (isSession) {
    sessionValue.startEndTime = buildSessionStartEndTime(cacheItem);
  }
  var initQuantity = (_formValues$quantity = formValues.quantity) !== null && _formValues$quantity !== void 0 ? _formValues$quantity : cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend = cacheItem._extend) === null || _cacheItem$_extend === void 0 ? void 0 : _cacheItem$_extend.quantity;
  return {
    item: cacheItem,
    productData: _objectSpread(_objectSpread({}, cacheItem), {}, {
      initValue: _objectSpread(_objectSpread(_objectSpread({}, cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend2 = cacheItem._extend) === null || _cacheItem$_extend2 === void 0 ? void 0 : _cacheItem$_extend2.other), sessionValue), {}, {
        quantity: initQuantity
      })
    }),
    showConfig: {
      option: true,
      session: isSession,
      variant: true,
      package: true,
      number: getIsShowNumber(cacheItem, bookingState)
    },
    isOnlySession: getIsOnlySession(cacheItem),
    isEject: getIsEject(cacheItem, 'detail') ? 1 : 0,
    customerId: (_options$customerId = options.customerId) !== null && _options$customerId !== void 0 ? _options$customerId : (_bookingState$client = bookingState.client) === null || _bookingState$client === void 0 || (_bookingState$client = _bookingState$client.value) === null || _bookingState$client === void 0 ? void 0 : _bookingState$client.id,
    date: date
  };
}