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 { getProductTotalPrice } from "../../../components/booking/utils";
import { editSessionProduct } from "../../../components/booking/info/utils";
import { setDefaultCapacitys } from "../../../components/booking/info2/service/addService/utils";
/**
 * 预约商品规格弹窗确认后，合并到抽屉内 cacheItem（对齐 Info2 editService handleEdit callback）。
 *
 * @example
 * const { cacheItem: next, formPatch } = applyProductDetailEditInDrawer({
 *   cacheItem,
 *   formValues: form.getFieldsValue(),
 *   detailResult,
 * });
 * onCacheItemChange(next);
 * form.setFieldsValue(formPatch);
 */
export function applyProductDetailEditInDrawer(input) {
  var _item$_extend, _ref, _ref2, _formValues$quantity, _item$_extend2, _item$id, _e$key, _detail$_extend$price, _detail$_extend, _item$_extend3, _item$new, _nextItem$_extend, _nextItem$_extend2, _nextItem$_extend3, _item$_extend4, _nextItem$_extend4;
  var item = input.cacheItem,
    formValues = input.formValues,
    detailResult = input.detailResult,
    _input$showNumber = input.showNumber,
    showNumber = _input$showNumber === void 0 ? true : _input$showNumber;
  var e = detailResult.e,
    detail = detailResult.detail;
  var _detail = detail || item;
  var other = JSON.parse(JSON.stringify(e || {}));
  other.session = e === null || e === void 0 ? void 0 : e.session;
  var resolvedQuantity = showNumber ? (e === null || e === void 0 ? void 0 : e.quantity) || (formValues === null || formValues === void 0 ? void 0 : formValues.quantity) || ((_item$_extend = item._extend) === null || _item$_extend === void 0 ? void 0 : _item$_extend.quantity) || 1 : (_ref = (_ref2 = (_formValues$quantity = formValues === null || formValues === void 0 ? void 0 : formValues.quantity) !== null && _formValues$quantity !== void 0 ? _formValues$quantity : (_item$_extend2 = item._extend) === null || _item$_extend2 === void 0 ? void 0 : _item$_extend2.quantity) !== null && _ref2 !== void 0 ? _ref2 : e === null || e === void 0 ? void 0 : e.quantity) !== null && _ref !== void 0 ? _ref : 1;
  var nextItem = _objectSpread(_objectSpread(_objectSpread({}, item), _detail), {}, {
    product_id: (_item$id = item.id) !== null && _item$id !== void 0 ? _item$id : item.product_id,
    _id: item === null || item === void 0 ? void 0 : item._id,
    _key: (_e$key = e === null || e === void 0 ? void 0 : e.key) !== null && _e$key !== void 0 ? _e$key : item._key,
    _extend: _objectSpread(_objectSpread(_objectSpread({}, item._extend), formValues), {}, {
      quantity: resolvedQuantity,
      other: other,
      priceOverride: (_detail$_extend$price = (_detail$_extend = _detail._extend) === null || _detail$_extend === void 0 ? void 0 : _detail$_extend.priceOverride) !== null && _detail$_extend$price !== void 0 ? _detail$_extend$price : (_item$_extend3 = item._extend) === null || _item$_extend3 === void 0 ? void 0 : _item$_extend3.priceOverride
    }),
    new: (_item$new = item === null || item === void 0 ? void 0 : item.new) !== null && _item$new !== void 0 ? _item$new : 0
  });
  var total = getProductTotalPrice(nextItem);
  var hadManualOverride = ((_nextItem$_extend = nextItem._extend) === null || _nextItem$_extend === void 0 ? void 0 : _nextItem$_extend.priceOverride) !== undefined && ((_nextItem$_extend2 = nextItem._extend) === null || _nextItem$_extend2 === void 0 ? void 0 : _nextItem$_extend2.priceOverride) !== null && ((_nextItem$_extend3 = nextItem._extend) === null || _nextItem$_extend3 === void 0 ? void 0 : _nextItem$_extend3.priceOverride) !== '';
  var preservedOriginTotal = (_item$_extend4 = item._extend) === null || _item$_extend4 === void 0 ? void 0 : _item$_extend4.origin_total;
  nextItem._extend.total = total;
  // 已有手动改价时保留 origin_total，避免规格编辑后丢失划线原价
  nextItem._extend.origin_total = hadManualOverride ? preservedOriginTotal !== null && preservedOriginTotal !== void 0 ? preservedOriginTotal : total : total;
  try {
    var _nextItem$_data;
    nextItem._extend.capacity = setDefaultCapacitys({
      capacitys: (_nextItem$_data = nextItem._data) === null || _nextItem$_data === void 0 ? void 0 : _nextItem$_data.capacitys,
      cacheItem: nextItem
    });
  } catch (_unused) {
    /* ignore */
  }
  var formPatch = {
    total: total,
    origin_total: nextItem._extend.origin_total,
    capacity: (_nextItem$_extend4 = nextItem._extend) === null || _nextItem$_extend4 === void 0 ? void 0 : _nextItem$_extend4.capacity,
    quantity: resolvedQuantity
  };
  var sessionDetail = editSessionProduct(nextItem, e === null || e === void 0 ? void 0 : e.session);
  if (sessionDetail) {
    formPatch.start_time = sessionDetail.start_time;
    formPatch.duration = sessionDetail.duration;
  }
  return {
    cacheItem: nextItem,
    formPatch: formPatch
  };
}

/**
 * 普通商品 SkuDetailModal 确认后，保留抽屉内资源/时间表单值并写回 cacheItem。
 *
 * @example
 * const { cacheItem: next, formPatch } = mergeNormalProductEditInDrawer({
 *   cacheItem,
 *   editedCacheItem: result.cacheItem,
 *   formValues: form.getFieldsValue(),
 * });
 */
export function mergeNormalProductEditInDrawer(input) {
  var _editedCacheItem$_key, _item$new2, _item$_data, _formValues$resource, _item$_extend5, _ref3, _formValues$start_tim, _item$_extend6, _ref4, _formValues$start_dat, _item$_extend7, _ref5, _formValues$end_date, _item$_extend8, _ref6, _formValues$duration, _item$_extend9, _formValues$holder_id, _item$_extend10, _formValues$note, _item$_extend11, _editedExtend$other, _item$_extend12, _ref7, _editedExtend$quantit, _item$_extend13;
  var item = input.cacheItem,
    editedCacheItem = input.editedCacheItem,
    formValues = input.formValues;
  var editedExtend = (editedCacheItem === null || editedCacheItem === void 0 ? void 0 : editedCacheItem._extend) || {};
  var nextItem = _objectSpread(_objectSpread(_objectSpread({}, item), editedCacheItem), {}, {
    _id: item === null || item === void 0 ? void 0 : item._id,
    _key: (_editedCacheItem$_key = editedCacheItem._key) !== null && _editedCacheItem$_key !== void 0 ? _editedCacheItem$_key : item._key,
    new: (_item$new2 = item === null || item === void 0 ? void 0 : item.new) !== null && _item$new2 !== void 0 ? _item$new2 : 0,
    _data: (_item$_data = item._data) !== null && _item$_data !== void 0 ? _item$_data : editedCacheItem._data,
    _extend: _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, item._extend), editedExtend), formValues), {}, {
      resource: (_formValues$resource = formValues.resource) !== null && _formValues$resource !== void 0 ? _formValues$resource : (_item$_extend5 = item._extend) === null || _item$_extend5 === void 0 ? void 0 : _item$_extend5.resource,
      start_time: (_ref3 = (_formValues$start_tim = formValues.start_time) !== null && _formValues$start_tim !== void 0 ? _formValues$start_tim : editedExtend.start_time) !== null && _ref3 !== void 0 ? _ref3 : (_item$_extend6 = item._extend) === null || _item$_extend6 === void 0 ? void 0 : _item$_extend6.start_time,
      start_date: (_ref4 = (_formValues$start_dat = formValues.start_date) !== null && _formValues$start_dat !== void 0 ? _formValues$start_dat : editedExtend.start_date) !== null && _ref4 !== void 0 ? _ref4 : (_item$_extend7 = item._extend) === null || _item$_extend7 === void 0 ? void 0 : _item$_extend7.start_date,
      end_date: (_ref5 = (_formValues$end_date = formValues.end_date) !== null && _formValues$end_date !== void 0 ? _formValues$end_date : editedExtend.end_date) !== null && _ref5 !== void 0 ? _ref5 : (_item$_extend8 = item._extend) === null || _item$_extend8 === void 0 ? void 0 : _item$_extend8.end_date,
      duration: (_ref6 = (_formValues$duration = formValues.duration) !== null && _formValues$duration !== void 0 ? _formValues$duration : editedExtend.duration) !== null && _ref6 !== void 0 ? _ref6 : (_item$_extend9 = item._extend) === null || _item$_extend9 === void 0 ? void 0 : _item$_extend9.duration,
      holder_id: (_formValues$holder_id = formValues.holder_id) !== null && _formValues$holder_id !== void 0 ? _formValues$holder_id : (_item$_extend10 = item._extend) === null || _item$_extend10 === void 0 ? void 0 : _item$_extend10.holder_id,
      note: (_formValues$note = formValues.note) !== null && _formValues$note !== void 0 ? _formValues$note : (_item$_extend11 = item._extend) === null || _item$_extend11 === void 0 ? void 0 : _item$_extend11.note,
      other: (_editedExtend$other = editedExtend.other) !== null && _editedExtend$other !== void 0 ? _editedExtend$other : (_item$_extend12 = item._extend) === null || _item$_extend12 === void 0 ? void 0 : _item$_extend12.other,
      total: editedExtend.total,
      origin_total: editedExtend.origin_total,
      quantity: (_ref7 = (_editedExtend$quantit = editedExtend.quantity) !== null && _editedExtend$quantit !== void 0 ? _editedExtend$quantit : formValues.quantity) !== null && _ref7 !== void 0 ? _ref7 : (_item$_extend13 = item._extend) === null || _item$_extend13 === void 0 ? void 0 : _item$_extend13.quantity
    })
  });
  return {
    cacheItem: nextItem,
    formPatch: {
      total: nextItem._extend.total,
      origin_total: nextItem._extend.origin_total,
      quantity: nextItem._extend.quantity,
      capacity: nextItem._extend.capacity
    }
  };
}