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); }
function toPriceString(value) {
  var fallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '0.00';
  var parsed = Number(value);
  if (!Number.isFinite(parsed)) {
    return fallback;
  }
  return parsed.toFixed(2);
}
function resolveQuantity(edited, orderLine) {
  var _ref, _ref2, _edited$_extend$quant, _edited$_extend;
  var raw = Number((_ref = (_ref2 = (_edited$_extend$quant = edited === null || edited === void 0 || (_edited$_extend = edited._extend) === null || _edited$_extend === void 0 ? void 0 : _edited$_extend.quantity) !== null && _edited$_extend$quant !== void 0 ? _edited$_extend$quant : edited === null || edited === void 0 ? void 0 : edited.num) !== null && _ref2 !== void 0 ? _ref2 : orderLine.num) !== null && _ref !== void 0 ? _ref : 1);
  if (!Number.isFinite(raw)) {
    return 1;
  }
  return Math.max(1, Math.floor(raw));
}
function resolveUnitPrice(edited, orderLine) {
  var _ref3, _ref4, _ref5, _extend$price;
  var extend = (edited === null || edited === void 0 ? void 0 : edited._extend) || {};
  var raw = (_ref3 = (_ref4 = (_ref5 = (_extend$price = extend.price) !== null && _extend$price !== void 0 ? _extend$price : extend.total) !== null && _ref5 !== void 0 ? _ref5 : edited === null || edited === void 0 ? void 0 : edited.selling_price) !== null && _ref4 !== void 0 ? _ref4 : orderLine.selling_price) !== null && _ref3 !== void 0 ? _ref3 : '0';
  return toPriceString(raw, toPriceString(orderLine.selling_price, '0.00'));
}
function resolveProductName(edited, orderLine) {
  var _edited$_extend2, _orderLine$metadata$o, _orderLine$metadata, _orderLine$metadata2;
  var fromExtend = edited === null || edited === void 0 || (_edited$_extend2 = edited._extend) === null || _edited$_extend2 === void 0 ? void 0 : _edited$_extend2.product_name;
  if (typeof fromExtend === 'string' && fromExtend.trim()) {
    return fromExtend.trim();
  }
  if (typeof (edited === null || edited === void 0 ? void 0 : edited.title) === 'string' && edited.title.trim()) {
    return edited.title.trim();
  }
  var originTitle = (_orderLine$metadata$o = (_orderLine$metadata = orderLine.metadata) === null || _orderLine$metadata === void 0 || (_orderLine$metadata = _orderLine$metadata.origin) === null || _orderLine$metadata === void 0 ? void 0 : _orderLine$metadata.title) !== null && _orderLine$metadata$o !== void 0 ? _orderLine$metadata$o : (_orderLine$metadata2 = orderLine.metadata) === null || _orderLine$metadata2 === void 0 || (_orderLine$metadata2 = _orderLine$metadata2.origin) === null || _orderLine$metadata2 === void 0 ? void 0 : _orderLine$metadata2.name;
  if (typeof originTitle === 'string' && originTitle.trim()) {
    return originTitle.trim();
  }
  return undefined;
}

/**
 * SkuDetailModal edit 确认后，将自定义商品 cacheItem 转为 `updateOrderProduct.updates`。
 * 不经过 transformDetailToProductAndBooking；行定位由调用方传 unique_identification_number。
 *
 * @example
 * const updates = buildCustomProductUpdatesFromEditedCacheItem(editedCacheItem, orderLine);
 * await updateItem({ ...pickIdentity(orderLine), product_id: null, updates });
 */
export function buildCustomProductUpdatesFromEditedCacheItem(edited, orderLine) {
  var _ref6, _ref7, _edited$note;
  var extend = (edited === null || edited === void 0 ? void 0 : edited._extend) || {};
  var unitPrice = resolveUnitPrice(edited, orderLine);
  var productName = resolveProductName(edited, orderLine);
  var existingMeta = orderLine.metadata || {};
  var existingOrigin = existingMeta.origin || {};
  return {
    num: resolveQuantity(edited, orderLine),
    note: String((_ref6 = (_ref7 = (_edited$note = edited.note) !== null && _edited$note !== void 0 ? _edited$note : extend.note) !== null && _ref7 !== void 0 ? _ref7 : orderLine.note) !== null && _ref6 !== void 0 ? _ref6 : ''),
    selling_price: unitPrice,
    original_price: unitPrice,
    product_option_item: [],
    product_bundle: [],
    metadata: _objectSpread(_objectSpread({}, existingMeta), {}, {
      origin: _objectSpread(_objectSpread({}, existingOrigin), {}, {
        isCustomItem: true
      }, productName ? {
        title: productName,
        name: productName
      } : {})
    })
  };
}