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); }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function toNumber(value) {
  var fallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  var parsedValue = Number(value);
  if (Number.isNaN(parsedValue)) return fallback;
  return parsedValue;
}
function toSafeNum(value) {
  var parsedValue = Math.floor(Number(value));
  if (Number.isNaN(parsedValue) || parsedValue < 1) return 1;
  return parsedValue;
}
function getDisplayName(product) {
  var _origin$resources, _product$metadata, _product$product_id;
  var origin = (product === null || product === void 0 ? void 0 : product._origin) || {};
  var callbackData = (origin === null || origin === void 0 ? void 0 : origin.callbackData) || {};
  var callbackOrigin = (callbackData === null || callbackData === void 0 ? void 0 : callbackData.origin) || {};
  var sourceItem = (origin === null || origin === void 0 ? void 0 : origin.sourceItem) || {};
  return (callbackData === null || callbackData === void 0 ? void 0 : callbackData.name) || (callbackData === null || callbackData === void 0 ? void 0 : callbackData.title) || (callbackOrigin === null || callbackOrigin === void 0 ? void 0 : callbackOrigin.name) || (callbackOrigin === null || callbackOrigin === void 0 ? void 0 : callbackOrigin.title) || (sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.name) || (sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.title) || (origin === null || origin === void 0 ? void 0 : origin.name) || (origin === null || origin === void 0 ? void 0 : origin.title) || (origin === null || origin === void 0 || (_origin$resources = origin.resources) === null || _origin$resources === void 0 || (_origin$resources = _origin$resources[0]) === null || _origin$resources === void 0 ? void 0 : _origin$resources.main_field) || (product === null || product === void 0 || (_product$metadata = product.metadata) === null || _product$metadata === void 0 ? void 0 : _product$metadata.resource_name) || "Product #".concat((_product$product_id = product.product_id) !== null && _product$product_id !== void 0 ? _product$product_id : 'unknown');
}
function getDisplayImage(product) {
  var origin = (product === null || product === void 0 ? void 0 : product._origin) || {};
  var callbackData = (origin === null || origin === void 0 ? void 0 : origin.callbackData) || {};
  var callbackOrigin = (callbackData === null || callbackData === void 0 ? void 0 : callbackData.origin) || {};
  var sourceItem = (origin === null || origin === void 0 ? void 0 : origin.sourceItem) || {};
  return (callbackData === null || callbackData === void 0 ? void 0 : callbackData.image) || (callbackData === null || callbackData === void 0 ? void 0 : callbackData.cover) || (callbackOrigin === null || callbackOrigin === void 0 ? void 0 : callbackOrigin.image) || (callbackOrigin === null || callbackOrigin === void 0 ? void 0 : callbackOrigin.cover) || (sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.image) || (sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.cover) || (origin === null || origin === void 0 ? void 0 : origin.image) || (origin === null || origin === void 0 ? void 0 : origin.cover) || '';
}
function getUnitPrice(product) {
  var _product$_origin, _product$_origin2;
  var callbackData = (product === null || product === void 0 || (_product$_origin = product._origin) === null || _product$_origin === void 0 ? void 0 : _product$_origin.callbackData) || {};
  var callbackOrigin = (callbackData === null || callbackData === void 0 ? void 0 : callbackData.origin) || {};
  var sourceItem = (product === null || product === void 0 || (_product$_origin2 = product._origin) === null || _product$_origin2 === void 0 ? void 0 : _product$_origin2.sourceItem) || {};
  // 注：本地临时订单链路不再用 payment_price 作为展示单价。
  // payment_price 语义为「券后单价」，仅用于订单提交，不参与购物车展示；
  // 券带来的节省由 origin_total 与 total 的对照以及 SubTotal 汇总体现。
  return toNumber(product.selling_price, NaN) || toNumber(product.original_price, NaN) || toNumber(callbackData === null || callbackData === void 0 ? void 0 : callbackData.price, NaN) || toNumber(callbackOrigin === null || callbackOrigin === void 0 ? void 0 : callbackOrigin.price, NaN) || toNumber(sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.price, 0);
}
function normalizeOptions(product) {
  var _product$_origin3;
  var callbackData = (product === null || product === void 0 || (_product$_origin3 = product._origin) === null || _product$_origin3 === void 0 ? void 0 : _product$_origin3.callbackData) || {};
  var sourceOptions = Array.isArray(callbackData === null || callbackData === void 0 ? void 0 : callbackData.option) ? callbackData.option : (product === null || product === void 0 ? void 0 : product.product_option_item) || [];
  return sourceOptions.map(function (optionItem) {
    var _optionItem$product_o;
    var optionPrice = toNumber(optionItem === null || optionItem === void 0 ? void 0 : optionItem.price, 0);
    var optionNum = toSafeNum(optionItem === null || optionItem === void 0 ? void 0 : optionItem.num);
    return {
      id: toNumber((_optionItem$product_o = optionItem === null || optionItem === void 0 ? void 0 : optionItem.product_option_item_id) !== null && _optionItem$product_o !== void 0 ? _optionItem$product_o : optionItem === null || optionItem === void 0 ? void 0 : optionItem.option_group_item_id),
      name: (optionItem === null || optionItem === void 0 ? void 0 : optionItem.name) || (optionItem === null || optionItem === void 0 ? void 0 : optionItem.title) || (optionItem === null || optionItem === void 0 ? void 0 : optionItem.label) || (optionItem === null || optionItem === void 0 ? void 0 : optionItem.option_group_item_name) || '',
      num: optionNum,
      price: optionPrice,
      total: optionPrice * optionNum,
      origin_total: optionPrice * optionNum
    };
  });
}

/** 与 getDisplayName 一致：优先 metadata 上的完整商品快照 */
function getProductOriginRich(product) {
  var _product$metadata2, _product$_origin4;
  return (product === null || product === void 0 || (_product$metadata2 = product.metadata) === null || _product$metadata2 === void 0 ? void 0 : _product$metadata2.origin) || (product === null || product === void 0 || (_product$_origin4 = product._origin) === null || _product$_origin4 === void 0 || (_product$_origin4 = _product$_origin4.callbackData) === null || _product$_origin4 === void 0 ? void 0 : _product$_origin4.origin) || {};
}
function findVariantGroupById(groups, groupId) {
  return groups.find(function (g) {
    return Number(g === null || g === void 0 ? void 0 : g.id) === Number(groupId);
  });
}
function findVariantItemDisplayName(variantGroup, variantItemId) {
  var _ref, _found$name;
  var items = Array.isArray(variantGroup === null || variantGroup === void 0 ? void 0 : variantGroup.variant_item) ? variantGroup.variant_item : [];
  var found = items.find(function (it) {
    return Number(it === null || it === void 0 ? void 0 : it.id) === Number(variantItemId);
  });
  var raw = (_ref = (_found$name = found === null || found === void 0 ? void 0 : found.name) !== null && _found$name !== void 0 ? _found$name : typeof (found === null || found === void 0 ? void 0 : found.title) === 'string' ? found.title : found === null || found === void 0 ? void 0 : found.title) !== null && _ref !== void 0 ? _ref : '';
  return String(raw !== null && raw !== void 0 ? raw : '').trim();
}

/**
 * 按 sku_ids（如 "7006:19533" 或 "g1:i1_g2:i2"）与 variant_group 拼「组名: 选项名」，与选品器 transformSelectorValueToValue 风格一致（", " 连接）。
 */
function buildVariantStringFromSkuIds(origin, skuIdsRaw) {
  var groups = Array.isArray(origin === null || origin === void 0 ? void 0 : origin.variant_group) ? origin.variant_group : [];
  var parts = [];
  var segments = String(skuIdsRaw || '').split('_').map(function (s) {
    return s.trim();
  }).filter(Boolean);
  var _iterator = _createForOfIteratorHelper(segments),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var _vg$name;
      var seg = _step.value;
      var colonIdx = seg.indexOf(':');
      if (colonIdx <= 0) continue;
      var groupId = Number(seg.slice(0, colonIdx));
      var itemId = Number(seg.slice(colonIdx + 1));
      if (Number.isNaN(groupId) || Number.isNaN(itemId)) continue;
      var vg = findVariantGroupById(groups, groupId);
      if (!vg) continue;
      var groupName = String((_vg$name = vg === null || vg === void 0 ? void 0 : vg.name) !== null && _vg$name !== void 0 ? _vg$name : '').trim();
      var itemName = findVariantItemDisplayName(vg, itemId);
      if (groupName && itemName) parts.push("".concat(groupName, ": ").concat(itemName));else if (itemName) parts.push(itemName);else if (groupName) parts.push(groupName);
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return parts.join(', ');
}

/** sku_ids 解析失败时用 variant 的 name/title 与 variant_group.position_id 对齐 */
function fallbackVariantLabelFromTitles(origin, variantRow) {
  var groups = Array.isArray(origin === null || origin === void 0 ? void 0 : origin.variant_group) ? origin.variant_group : [];
  var nameObj = (variantRow === null || variantRow === void 0 ? void 0 : variantRow.name) || (variantRow === null || variantRow === void 0 ? void 0 : variantRow.title);
  if (!nameObj || _typeof(nameObj) !== 'object') return '';
  var parts = [];
  var _iterator2 = _createForOfIteratorHelper(groups),
    _step2;
  try {
    for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
      var _String, _g$name;
      var g = _step2.value;
      var pid = g === null || g === void 0 ? void 0 : g.position_id;
      if (pid == null) continue;
      var val = (_String = nameObj[String(pid)]) !== null && _String !== void 0 ? _String : nameObj[pid];
      if (val == null || val === '') continue;
      var groupName = String((_g$name = g === null || g === void 0 ? void 0 : g.name) !== null && _g$name !== void 0 ? _g$name : '').trim();
      var itemLabel = String(val).trim();
      if (groupName && itemLabel) parts.push("".concat(groupName, ": ").concat(itemLabel));else if (itemLabel) parts.push(itemLabel);
    }
  } catch (err) {
    _iterator2.e(err);
  } finally {
    _iterator2.f();
  }
  if (parts.length) return parts.join(', ');
  var vals = Object.values(nameObj).filter(function (v) {
    return v != null && String(v).trim() !== '';
  });
  return vals.map(function (v) {
    return String(v).trim();
  }).join(' / ');
}
function getVariantProductOptionString(product) {
  var variantId = product === null || product === void 0 ? void 0 : product.product_variant_id;
  if (variantId == null || variantId === '' || Number(variantId) <= 0) return '';
  var origin = getProductOriginRich(product);
  var variants = Array.isArray(origin === null || origin === void 0 ? void 0 : origin.variant) ? origin.variant : [];
  var matched = variants.find(function (v) {
    return Number(v === null || v === void 0 ? void 0 : v.id) === Number(variantId);
  });
  if (!matched) return '';
  var skuIds = matched === null || matched === void 0 ? void 0 : matched.sku_ids;
  if (skuIds != null && String(skuIds).trim() !== '') {
    var fromSku = buildVariantStringFromSkuIds(origin, String(skuIds));
    if (fromSku) return fromSku;
  }
  return fallbackVariantLabelFromTitles(origin, matched);
}
function normalizeBundles(product) {
  var _product$_origin5;
  var callbackData = (product === null || product === void 0 || (_product$_origin5 = product._origin) === null || _product$_origin5 === void 0 ? void 0 : _product$_origin5.callbackData) || {};
  var sourceBundles = Array.isArray(callbackData === null || callbackData === void 0 ? void 0 : callbackData.bundle) ? callbackData.bundle : (product === null || product === void 0 ? void 0 : product.product_bundle) || [];
  return sourceBundles.map(function (bundleItem, bundleIndex) {
    var _ref2, _bundleItem$price, _bundleItem$id;
    var bundlePrice = toNumber((_ref2 = (_bundleItem$price = bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.price) !== null && _bundleItem$price !== void 0 ? _bundleItem$price : bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.product_price) !== null && _ref2 !== void 0 ? _ref2 : bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.custom_price, 0);
    var bundleNum = toSafeNum(bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.num);
    var bundleOptions = Array.isArray(bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.option) ? bundleItem.option : [];
    return {
      id: toNumber((_bundleItem$id = bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.id) !== null && _bundleItem$id !== void 0 ? _bundleItem$id : bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.bundle_product_id, bundleIndex),
      name: (bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.name) || (bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.title) || '',
      num: bundleNum,
      price: bundlePrice,
      total: bundlePrice * bundleNum,
      origin_total: bundlePrice * bundleNum,
      options: bundleOptions.map(function (optionItem) {
        var _optionItem$product_o2;
        var optionPrice = toNumber(optionItem === null || optionItem === void 0 ? void 0 : optionItem.price, 0);
        var optionNum = toSafeNum(optionItem === null || optionItem === void 0 ? void 0 : optionItem.num);
        return {
          id: toNumber((_optionItem$product_o2 = optionItem === null || optionItem === void 0 ? void 0 : optionItem.product_option_item_id) !== null && _optionItem$product_o2 !== void 0 ? _optionItem$product_o2 : optionItem === null || optionItem === void 0 ? void 0 : optionItem.option_group_item_id),
          name: (optionItem === null || optionItem === void 0 ? void 0 : optionItem.name) || (optionItem === null || optionItem === void 0 ? void 0 : optionItem.title) || (optionItem === null || optionItem === void 0 ? void 0 : optionItem.label) || (optionItem === null || optionItem === void 0 ? void 0 : optionItem.option_group_item_name) || '',
          num: optionNum,
          price: optionPrice,
          total: optionPrice * optionNum,
          origin_total: optionPrice * optionNum
        };
      })
    };
  });
}
export function toShoppingCartProducts(params) {
  var orderProducts = params.orderProducts,
    _params$amountSymbol = params.amountSymbol,
    amountSymbol = _params$amountSymbol === void 0 ? '$' : _params$amountSymbol;
  if (!Array.isArray(orderProducts)) return [];
  return orderProducts.map(function (productItem, index) {
    var _productItem$_origin, _productItem$product_, _productItem$metadata;
    var productNum = toSafeNum(productItem === null || productItem === void 0 ? void 0 : productItem.num);
    var unitPrice = getUnitPrice(productItem);
    var callbackData = (productItem === null || productItem === void 0 || (_productItem$_origin = productItem._origin) === null || _productItem$_origin === void 0 ? void 0 : _productItem$_origin.callbackData) || {};
    // 不透明 identity 契约：rowKey / _id 直接复用 SDK 回灌的 identity_key；
    // 仅当（升级路径或非常规调用）identity_key 缺失时退到 SKU + index 兜底。
    var identityKey = typeof (productItem === null || productItem === void 0 ? void 0 : productItem.identity_key) === 'string' && productItem.identity_key.length > 0 ? productItem.identity_key : undefined;
    var rowKey = identityKey !== null && identityKey !== void 0 ? identityKey : "".concat((_productItem$product_ = productItem.product_id) !== null && _productItem$product_ !== void 0 ? _productItem$product_ : 'p', "_").concat(productItem.product_variant_id, "_").concat(index);
    var fromCallbackOptionString = typeof (callbackData === null || callbackData === void 0 ? void 0 : callbackData.product_option_string) === 'string' ? callbackData.product_option_string.trim() : '';
    var productOptionString = fromCallbackOptionString || getVariantProductOptionString(productItem) || undefined;
    var base = _objectSpread({
      _id: rowKey,
      rowKey: rowKey,
      identity_key: identityKey,
      id: productItem.product_id,
      product_id: productItem.product_id,
      product_variant_id: productItem.product_variant_id,
      note: (productItem === null || productItem === void 0 ? void 0 : productItem.note) != null ? String(productItem.note) : '',
      name: getDisplayName(productItem),
      image: getDisplayImage(productItem),
      num: productNum,
      price: unitPrice,
      total: unitPrice,
      origin_total: toNumber(productItem.original_price, unitPrice),
      symbol: amountSymbol,
      options: normalizeOptions(productItem),
      bundle: normalizeBundles(productItem),
      _origin: productItem === null || productItem === void 0 ? void 0 : productItem._origin,
      isNormalProduct: true
    }, productOptionString ? {
      product_option_string: productOptionString
    } : {});
    if (Array.isArray(productItem.product_option_item) && productItem.product_option_item.length) {
      base.product_option_item = productItem.product_option_item;
    }
    if (Array.isArray(productItem.product_bundle) && productItem.product_bundle.length) {
      base.product_bundle = productItem.product_bundle;
    }
    if (productItem !== null && productItem !== void 0 && (_productItem$metadata = productItem.metadata) !== null && _productItem$metadata !== void 0 && _productItem$metadata.venue_booking) {
      var _origin$resources2;
      var origin = productItem._origin || {};
      var meta = productItem.metadata || {};
      var startDate = origin.start_date;
      var startTime = origin.start_time;
      var endDate = origin.end_date;
      var endTime = origin.end_time;
      base.start_date = startDate && startTime ? "".concat(startDate, " ").concat(startTime) : meta.start_time || '';
      base.end_date = endDate && endTime ? "".concat(endDate, " ").concat(endTime) : meta.end_time || '';
      base.start_time = startTime || '';
      base.end_time = endTime || '';
      base.duration_type = origin.sub_type || 'minutes';
      base.resource_id = meta.resource_id;
      base.relation_form_name = meta.resource_name || ((_origin$resources2 = origin.resources) === null || _origin$resources2 === void 0 || (_origin$resources2 = _origin$resources2[0]) === null || _origin$resources2 === void 0 ? void 0 : _origin$resources2.main_field) || '';
      if (Array.isArray(origin.resources)) base.resources = origin.resources;
    }
    return base;
  });
}