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 { cloneDeep } from 'lodash';
import { getProductTotalPrice } from "../../../components/booking/utils";
import { setProductPrice } from "../../../components/booking/info2/service/editService/utils";
import { formatDataKey } from "../../../components/ticketBooking/components/addServiceVariant/utils";
/** 将 setProductPrice 结果同步到 skuValue / _skuValue（与 ticketBooking 卡片一致） */
function syncSkuValueFromOther(data) {
  var _data$_extend, _ref, _other$product_id, _other$product_varian, _other$product_option;
  var other = data === null || data === void 0 || (_data$_extend = data._extend) === null || _data$_extend === void 0 ? void 0 : _data$_extend.other;
  if (!other || !(data !== null && data !== void 0 && data._extend)) return data;
  var snapshot = {
    product_id: (_ref = (_other$product_id = other.product_id) !== null && _other$product_id !== void 0 ? _other$product_id : data.product_id) !== null && _ref !== void 0 ? _ref : data.id,
    product_variant_id: (_other$product_varian = other.product_variant_id) !== null && _other$product_varian !== void 0 ? _other$product_varian : 0,
    product_option_string: (_other$product_option = other.product_option_string) !== null && _other$product_option !== void 0 ? _other$product_option : '',
    bundle: cloneDeep(other.bundle || []),
    option: cloneDeep(other.option || [])
  };
  data._extend.skuValue = snapshot;
  data._extend._skuValue = cloneDeep(snapshot);
  return data;
}

/**
 * 手动改价时 origin_total 不能回退为折后 total，否则 setProductPrice 比例为 1、子商品不会重算。
 */
function resolveOriginTotalForSetPrice(extend, lineTotal) {
  var hasOverride = (extend === null || extend === void 0 ? void 0 : extend.priceOverride) !== undefined && (extend === null || extend === void 0 ? void 0 : extend.priceOverride) !== null;
  var origin = Number(extend === null || extend === void 0 ? void 0 : extend.origin_total);
  if (Number.isFinite(origin) && origin > 0) {
    return origin;
  }
  if (hasOverride) {
    return lineTotal;
  }
  return lineTotal;
}

/**
 * create 模式：SkuDetailModal 确认结果 → `cart.confirmDetail` 所需的 detailResult。
 * 对齐 info2 EditNormalService create 分支，不依赖 miniRedux。
 */
export function transformNormalProductCreateConfirm(productId, data) {
  var _data$_extend2, _ref2, _data$_extend$price, _data$_extend3, _data$_extend$price2, _data$_extend4, _ref3, _data$_extend$origin_, _data$_extend5, _data$_extend6, _data$_extend7;
  var quantity = Number((data === null || data === void 0 || (_data$_extend2 = data._extend) === null || _data$_extend2 === void 0 ? void 0 : _data$_extend2.quantity) || 1);
  var basePrice = Number((_ref2 = (_data$_extend$price = data === null || data === void 0 || (_data$_extend3 = data._extend) === null || _data$_extend3 === void 0 ? void 0 : _data$_extend3.price) !== null && _data$_extend$price !== void 0 ? _data$_extend$price : data === null || data === void 0 ? void 0 : data.price) !== null && _ref2 !== void 0 ? _ref2 : 0) || 0;
  var detail = _objectSpread(_objectSpread({}, data || {}), {}, {
    id: productId,
    product_id: productId,
    _extend: _objectSpread(_objectSpread({}, (data === null || data === void 0 ? void 0 : data._extend) || {}), {}, {
      price: (_data$_extend$price2 = data === null || data === void 0 || (_data$_extend4 = data._extend) === null || _data$_extend4 === void 0 ? void 0 : _data$_extend4.price) !== null && _data$_extend$price2 !== void 0 ? _data$_extend$price2 : basePrice,
      origin_total: (_ref3 = (_data$_extend$origin_ = data === null || data === void 0 || (_data$_extend5 = data._extend) === null || _data$_extend5 === void 0 ? void 0 : _data$_extend5.origin_total) !== null && _data$_extend$origin_ !== void 0 ? _data$_extend$origin_ : data === null || data === void 0 || (_data$_extend6 = data._extend) === null || _data$_extend6 === void 0 ? void 0 : _data$_extend6.total) !== null && _ref3 !== void 0 ? _ref3 : basePrice,
      quantity: quantity,
      skuValue: cloneDeep((data === null || data === void 0 || (_data$_extend7 = data._extend) === null || _data$_extend7 === void 0 ? void 0 : _data$_extend7.skuValue) || {})
    })
  });
  var lineTotal = getProductTotalPrice(detail);
  detail._extend.total = lineTotal;
  detail = setProductPrice(detail, {
    newTotal: lineTotal,
    origin_total: detail._extend.origin_total
  });
  var e = _objectSpread(_objectSpread({
    product_id: productId
  }, detail._extend.other), {}, {
    quantity: quantity
  });
  e = formatDataKey(e);
  return {
    e: e,
    extension_type: undefined,
    detail: detail
  };
}

/**
 * edit 模式：SkuDetailModal 确认结果 → 写回 OS 所需的 cacheItem。
 * 对齐 info2 EditNormalService edit 分支的价格处理。
 */
export function prepareNormalProductEditCacheItem(data) {
  var _data$price, _extend$other$bundle$, _extend$other, _prepared$_extend;
  var extend = _objectSpread({
    price: (_data$price = data === null || data === void 0 ? void 0 : data.price) !== null && _data$price !== void 0 ? _data$price : 0
  }, (data === null || data === void 0 ? void 0 : data._extend) || {});
  var normalized = _objectSpread(_objectSpread({}, data || {}), {}, {
    _extend: extend
  });
  var lineTotal = getProductTotalPrice(normalized);
  normalized._extend = _objectSpread(_objectSpread({}, extend), {}, {
    total: lineTotal,
    origin_total: resolveOriginTotalForSetPrice(extend, lineTotal)
  });
  if (normalized !== null && normalized !== void 0 && normalized.is_bundle || ((_extend$other$bundle$ = extend === null || extend === void 0 || (_extend$other = extend.other) === null || _extend$other === void 0 || (_extend$other = _extend$other.bundle) === null || _extend$other === void 0 ? void 0 : _extend$other.length) !== null && _extend$other$bundle$ !== void 0 ? _extend$other$bundle$ : 0) > 0) {
    normalized._extend.bundle_edit = 1;
  }
  var prepared = setProductPrice(normalized, {
    newTotal: lineTotal,
    origin_total: normalized._extend.origin_total
  });
  syncSkuValueFromOther(prepared);
  prepared.note = (_prepared$_extend = prepared._extend) === null || _prepared$_extend === void 0 ? void 0 : _prepared$_extend.note;
  return prepared;
}