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 { syncBundleFromExtendOther } from "./syncBundleFromExtendOther";

/**
 * 编辑商品行时构造 updateOrderProduct.updates：
 * 保留 v2 metadata 主价，由 OrderModule.normalizeOrderProduct 按新 bundle/option 重算 composite。
 */
export function buildProductLineUpdates(orderLine, productPatch) {
  var _patchMeta$source_pro, _patchMeta$main_produ, _patchMeta$main_produ2, _patchMeta$price_over, _patchMeta$origin, _patchMeta$origin$_ex, _patchMeta$origin2, _patchMeta$origin3, _productPatch$product, _productPatch$product2, _productPatch$discoun;
  var patchMeta = productPatch.metadata || {};
  var lineMeta = orderLine.metadata || {};
  var sourceProductPrice = (_patchMeta$source_pro = patchMeta.source_product_price) !== null && _patchMeta$source_pro !== void 0 ? _patchMeta$source_pro : lineMeta.source_product_price;
  var mainProductSellingPrice = (_patchMeta$main_produ = patchMeta.main_product_selling_price) !== null && _patchMeta$main_produ !== void 0 ? _patchMeta$main_produ : lineMeta.main_product_selling_price;
  var mainProductOriginalPrice = (_patchMeta$main_produ2 = patchMeta.main_product_original_price) !== null && _patchMeta$main_produ2 !== void 0 ? _patchMeta$main_produ2 : lineMeta.main_product_original_price;
  var priceOverride = (_patchMeta$price_over = patchMeta.price_override) !== null && _patchMeta$price_over !== void 0 ? _patchMeta$price_over : (_patchMeta$origin = patchMeta.origin) === null || _patchMeta$origin === void 0 || (_patchMeta$origin = _patchMeta$origin._extend) === null || _patchMeta$origin === void 0 ? void 0 : _patchMeta$origin.priceOverride;
  var otherBundle = (_patchMeta$origin$_ex = (_patchMeta$origin2 = patchMeta.origin) === null || _patchMeta$origin2 === void 0 || (_patchMeta$origin2 = _patchMeta$origin2._extend) === null || _patchMeta$origin2 === void 0 || (_patchMeta$origin2 = _patchMeta$origin2.other) === null || _patchMeta$origin2 === void 0 ? void 0 : _patchMeta$origin2.bundle) !== null && _patchMeta$origin$_ex !== void 0 ? _patchMeta$origin$_ex : (_patchMeta$origin3 = patchMeta.origin) === null || _patchMeta$origin3 === void 0 || (_patchMeta$origin3 = _patchMeta$origin3._extend) === null || _patchMeta$origin3 === void 0 || (_patchMeta$origin3 = _patchMeta$origin3.skuValue) === null || _patchMeta$origin3 === void 0 ? void 0 : _patchMeta$origin3.bundle;
  var hasPriceOverride = priceOverride !== undefined && priceOverride !== null && priceOverride !== '';
  var baseBundle = (_productPatch$product = productPatch.product_bundle) !== null && _productPatch$product !== void 0 ? _productPatch$product : orderLine.product_bundle;
  var productBundle = hasPriceOverride ? syncBundleFromExtendOther(baseBundle, otherBundle) : (_productPatch$product2 = productPatch.product_bundle) !== null && _productPatch$product2 !== void 0 ? _productPatch$product2 : orderLine.product_bundle;
  var updates = {
    num: productPatch.num,
    note: productPatch.note,
    tax_fee: productPatch.tax_fee,
    is_charge_tax: productPatch.is_charge_tax,
    discount_list: (_productPatch$discoun = productPatch.discount_list) !== null && _productPatch$discoun !== void 0 ? _productPatch$discoun : orderLine.discount_list,
    product_option_item: productPatch.product_option_item,
    metadata: _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, lineMeta), patchMeta), sourceProductPrice !== undefined ? {
      source_product_price: String(sourceProductPrice)
    } : {}), mainProductSellingPrice !== undefined ? {
      main_product_selling_price: String(mainProductSellingPrice)
    } : {}), mainProductOriginalPrice !== undefined ? {
      main_product_original_price: String(mainProductOriginalPrice)
    } : {}), hasPriceOverride ? {
      price_override: String(priceOverride)
    } : {}), hasPriceOverride ? {
      is_manual_discount: 1
    } : {}), {}, {
      price_schema_version: 2
    })
  };
  if (productBundle !== undefined) {
    updates.product_bundle = productBundle;
  }
  return updates;
}