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 { normalizeCatalogProduct, resolveCatalogProductId } from "./resolveCatalogProductId";
/**
 * 赠品弹窗专用：从规格弹窗 callback 拼 cacheItem（不依赖 OS order transform）。
 * 对齐 `buildCacheItemFromDetail` 的 UI 侧字段，供 usePromotionModal 写回列表。
 *
 * @example
 * const cacheItem = buildGiftCacheItemFromDetail(catalogProduct, detailResult, 1, '2026-05-26 12:00:00');
 */
export function buildGiftCacheItemFromDetail(catalogProduct, detailResult, confirmQuantity, flowDate) {
  var _detailResult$detail, _detailResult$e, _ref, _e$key, _detailExtend$other, _ref2, _ref3, _detail$title, _ref4, _ref5, _detail$name, _ref6, _detail$cover, _detail$extension_typ, _ref7, _e$key2, _ref8, _detailExtend$price, _detailExtend$total, _ref9, _ref10, _ref11, _detailExtend$origin_, _detailExtend$skuValu, _detailExtend$_skuVal;
  var normalizedCatalog = normalizeCatalogProduct(catalogProduct);
  var productId = resolveCatalogProductId(normalizedCatalog);
  if (!productId) return null;
  var detail = (_detailResult$detail = detailResult === null || detailResult === void 0 ? void 0 : detailResult.detail) !== null && _detailResult$detail !== void 0 ? _detailResult$detail : {};
  var e = (_detailResult$e = detailResult === null || detailResult === void 0 ? void 0 : detailResult.e) !== null && _detailResult$e !== void 0 ? _detailResult$e : {};
  var detailExtend = (detail === null || detail === void 0 ? void 0 : detail._extend) || {};
  var other = _objectSpread(_objectSpread(_objectSpread({}, detailExtend.other || {}), e), {}, {
    product_id: productId,
    quantity: confirmQuantity
  });
  var cartDetailValue = _objectSpread(_objectSpread({}, other), {}, {
    key: (_ref = (_e$key = e.key) !== null && _e$key !== void 0 ? _e$key : detail._key) !== null && _ref !== void 0 ? _ref : (_detailExtend$other = detailExtend.other) === null || _detailExtend$other === void 0 ? void 0 : _detailExtend$other.key,
    quantity: confirmQuantity
  });
  return _objectSpread(_objectSpread(_objectSpread({}, normalizedCatalog), detail), {}, {
    id: productId,
    product_id: productId,
    cartDetailValue: cartDetailValue,
    isOpenDetailModal: false,
    title: (_ref2 = (_ref3 = (_detail$title = detail === null || detail === void 0 ? void 0 : detail.title) !== null && _detail$title !== void 0 ? _detail$title : normalizedCatalog === null || normalizedCatalog === void 0 ? void 0 : normalizedCatalog.title) !== null && _ref3 !== void 0 ? _ref3 : detail === null || detail === void 0 ? void 0 : detail.name) !== null && _ref2 !== void 0 ? _ref2 : normalizedCatalog === null || normalizedCatalog === void 0 ? void 0 : normalizedCatalog.name,
    name: (_ref4 = (_ref5 = (_detail$name = detail === null || detail === void 0 ? void 0 : detail.name) !== null && _detail$name !== void 0 ? _detail$name : normalizedCatalog === null || normalizedCatalog === void 0 ? void 0 : normalizedCatalog.name) !== null && _ref5 !== void 0 ? _ref5 : detail === null || detail === void 0 ? void 0 : detail.title) !== null && _ref4 !== void 0 ? _ref4 : normalizedCatalog === null || normalizedCatalog === void 0 ? void 0 : normalizedCatalog.title,
    cover: (_ref6 = (_detail$cover = detail === null || detail === void 0 ? void 0 : detail.cover) !== null && _detail$cover !== void 0 ? _detail$cover : normalizedCatalog === null || normalizedCatalog === void 0 ? void 0 : normalizedCatalog.cover) !== null && _ref6 !== void 0 ? _ref6 : normalizedCatalog === null || normalizedCatalog === void 0 ? void 0 : normalizedCatalog.image,
    num: confirmQuantity,
    extension_type: (_detail$extension_typ = detail.extension_type) !== null && _detail$extension_typ !== void 0 ? _detail$extension_typ : normalizedCatalog.extension_type,
    _key: (_ref7 = (_e$key2 = e.key) !== null && _e$key2 !== void 0 ? _e$key2 : detail._key) !== null && _ref7 !== void 0 ? _ref7 : normalizedCatalog._key,
    _extend: _objectSpread(_objectSpread(_objectSpread({}, normalizedCatalog._extend || {}), detailExtend), {}, {
      start_date: flowDate || detailExtend.start_date,
      quantity: confirmQuantity,
      price: (_ref8 = (_detailExtend$price = detailExtend.price) !== null && _detailExtend$price !== void 0 ? _detailExtend$price : normalizedCatalog.price) !== null && _ref8 !== void 0 ? _ref8 : 0,
      total: (_detailExtend$total = detailExtend.total) !== null && _detailExtend$total !== void 0 ? _detailExtend$total : 0,
      origin_total: (_ref9 = (_ref10 = (_ref11 = (_detailExtend$origin_ = detailExtend.origin_total) !== null && _detailExtend$origin_ !== void 0 ? _detailExtend$origin_ : normalizedCatalog._initPrice) !== null && _ref11 !== void 0 ? _ref11 : normalizedCatalog.origin_price) !== null && _ref10 !== void 0 ? _ref10 : normalizedCatalog.price) !== null && _ref9 !== void 0 ? _ref9 : 0,
      other: other,
      skuValue: (_detailExtend$skuValu = detailExtend.skuValue) !== null && _detailExtend$skuValu !== void 0 ? _detailExtend$skuValu : detailExtend._skuValue,
      _skuValue: (_detailExtend$_skuVal = detailExtend._skuValue) !== null && _detailExtend$_skuVal !== void 0 ? _detailExtend$_skuVal : detailExtend.skuValue
    }),
    new: 1
  });
}

/**
 * 优先走 OS `transformDetailToProductAndBooking`；失败时 fallback 到赠品专用 cacheItem 拼装。
 *
 * @example
 * const cacheItem = await resolveGiftCacheItemAfterDetail(bookingTicket, product, detailResult, options);
 */
export function resolveGiftCacheItemAfterDetail(bookingTicket, catalogProduct, detailResult, options) {
  var normalizedProduct = normalizeCatalogProduct(catalogProduct);
  var productId = resolveCatalogProductId(normalizedProduct);
  if (!productId) {
    console.warn('[SalesSdk/giftSelect] 商品缺少有效 id/product_id');
    return null;
  }
  var normalizedDetailResult = _objectSpread(_objectSpread({}, detailResult), {}, {
    e: _objectSpread(_objectSpread({}, (detailResult === null || detailResult === void 0 ? void 0 : detailResult.e) || {}), {}, {
      product_id: productId
    }),
    detail: detailResult !== null && detailResult !== void 0 && detailResult.detail ? normalizeCatalogProduct(detailResult.detail) : {
      id: productId,
      product_id: productId
    }
  });
  if (typeof bookingTicket.transformDetailToProductAndBooking === 'function') {
    try {
      var draft = bookingTicket.transformDetailToProductAndBooking({
        item: normalizedProduct,
        detailResult: normalizedDetailResult,
        options: {
          quantity: options.quantity,
          date: options.date,
          customerId: options.customerId
        }
      });
      if (draft !== null && draft !== void 0 && draft.cacheItem && resolveCatalogProductId(draft.cacheItem)) {
        return draft.cacheItem;
      }
    } catch (error) {
      console.warn('[SalesSdk/giftSelect] transformDetailToProductAndBooking 失败，使用 fallback cacheItem', error);
    }
  }
  return buildGiftCacheItemFromDetail(normalizedProduct, normalizedDetailResult, options.quantity, options.date);
}