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 _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); }
/**
 * 判断赠品弹窗内商品是否已有 SKU / 规格选择（用于二次 Edit 回显）。
 *
 * @example
 * if (hasExistingGiftSkuSelection(product)) { /* 走 edit 模式 *\/ }
 */
export function hasExistingGiftSkuSelection(product) {
  var _ref, _extend$_skuValue;
  if (!product) return false;
  var initValue = product.initValue;
  if (initValue && _typeof(initValue) === 'object') {
    if (Array.isArray(initValue.bundle) && initValue.bundle.length > 0) return true;
    if (Array.isArray(initValue.option) && initValue.option.length > 0) return true;
    if (Number(initValue.product_variant_id) > 0) return true;
  }
  var extend = product._extend;
  var sku = (_ref = (_extend$_skuValue = extend === null || extend === void 0 ? void 0 : extend._skuValue) !== null && _extend$_skuValue !== void 0 ? _extend$_skuValue : extend === null || extend === void 0 ? void 0 : extend.skuValue) !== null && _ref !== void 0 ? _ref : extend === null || extend === void 0 ? void 0 : extend.other;
  if (!sku || _typeof(sku) !== 'object') return false;
  if (Array.isArray(sku.bundle) && sku.bundle.length > 0) return true;
  if (Array.isArray(sku.option) && sku.option.length > 0) return true;
  if (Number(sku.product_variant_id) > 0) return true;
  return false;
}

/**
 * 从弹窗列表商品提取规格回显 initValue（对齐 legacy getProductInitValue / openNormalService edit）。
 *
 * @example
 * const initValue = buildGiftSkuInitValue(product);
 */
export function buildGiftSkuInitValue(product) {
  var _ref2, _extend$other, _ref3, _ref4, _extend$quantity;
  if (!product) return null;
  if (product.initValue && _typeof(product.initValue) === 'object') {
    return product.initValue;
  }
  var extend = product._extend;
  var other = (_ref2 = (_extend$other = extend === null || extend === void 0 ? void 0 : extend.other) !== null && _extend$other !== void 0 ? _extend$other : extend === null || extend === void 0 ? void 0 : extend._skuValue) !== null && _ref2 !== void 0 ? _ref2 : extend === null || extend === void 0 ? void 0 : extend.skuValue;
  if (!other || _typeof(other) !== 'object') return null;
  return _objectSpread(_objectSpread({}, other), {}, {
    quantity: (_ref3 = (_ref4 = (_extend$quantity = extend === null || extend === void 0 ? void 0 : extend.quantity) !== null && _extend$quantity !== void 0 ? _extend$quantity : product.num) !== null && _ref4 !== void 0 ? _ref4 : other.quantity) !== null && _ref3 !== void 0 ? _ref3 : 1
  });
}

/**
 * 为 requiresDetail payload 注入 initValue，供预约类商品 legacy 弹窗回显。
 *
 * @example
 * const payload = enrichDetailPayloadWithInitValue(rawPayload, product);
 */
export function enrichDetailPayloadWithInitValue(payload, product) {
  var _ref5, _payload$productData, _payload$item, _payload$item2;
  var initValue = buildGiftSkuInitValue(product);
  if (!initValue) return payload;
  var baseProduct = (_ref5 = (_payload$productData = payload.productData) !== null && _payload$productData !== void 0 ? _payload$productData : payload.item) !== null && _ref5 !== void 0 ? _ref5 : {};
  return _objectSpread(_objectSpread({}, payload), {}, {
    item: _objectSpread(_objectSpread({}, (_payload$item = payload.item) !== null && _payload$item !== void 0 ? _payload$item : baseProduct), {}, {
      initValue: _objectSpread(_objectSpread({}, ((_payload$item2 = payload.item) === null || _payload$item2 === void 0 ? void 0 : _payload$item2.initValue) || {}), initValue)
    }),
    productData: _objectSpread(_objectSpread({}, baseProduct), {}, {
      initValue: _objectSpread(_objectSpread({}, baseProduct.initValue || {}), initValue)
    })
  });
}