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); }
var _excluded = ["bundle_selling_price"];
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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { getProductSkuOptions } from "../../Order/utils";
import { buildProductOptionStringFromOrderLine } from "./orderLineDisplay";
/**
 * 从已加车的普通商品 order line 反查 SkuDetailModal edit 所需的 cacheItem。
 *
 * 与 `buildCacheItemFromOrderLine`（预约行 + booking）并列；不含 resource / duration / holder 等预约字段。
 */
/**
 * 解析 v2 主商品 source 价（不含 option、不含 bundle），供 SkuDetailModal / transform 使用。
 */
function resolveSourceProductPrice(product, sourceProduct) {
  var _price, _price2, _metadata$origin2, _metadata$origin3, _product$selling_pric;
  var metadata = product.metadata || {};
  if (metadata.source_product_price !== undefined && metadata.source_product_price !== null) {
    var parsed = Number(metadata.source_product_price);
    if (Number.isFinite(parsed)) return parsed;
  }
  var variantId = Number(product.product_variant_id || 0);
  if (variantId > 0) {
    var _variant, _metadata$origin;
    var variantList = (_variant = (_metadata$origin = metadata.origin) === null || _metadata$origin === void 0 ? void 0 : _metadata$origin.variant) !== null && _variant !== void 0 ? _variant : sourceProduct === null || sourceProduct === void 0 ? void 0 : sourceProduct.variant;
    if (Array.isArray(variantList)) {
      var _matched$price;
      var matched = variantList.find(function (v) {
        return Number(v === null || v === void 0 ? void 0 : v.id) === variantId;
      });
      var variantPrice = (_matched$price = matched === null || matched === void 0 ? void 0 : matched.price) !== null && _matched$price !== void 0 ? _matched$price : matched === null || matched === void 0 ? void 0 : matched.base_price;
      if (variantPrice !== undefined && variantPrice !== null) {
        var _parsed = Number(variantPrice);
        if (Number.isFinite(_parsed)) return _parsed;
      }
    }
  }
  var catalogPrice = (_price = sourceProduct === null || sourceProduct === void 0 ? void 0 : sourceProduct.price) !== null && _price !== void 0 ? _price : sourceProduct === null || sourceProduct === void 0 ? void 0 : sourceProduct.base_price;
  if (catalogPrice !== undefined && catalogPrice !== null) {
    var _parsed2 = Number(catalogPrice);
    if (Number.isFinite(_parsed2)) return _parsed2;
  }
  var originPrice = (_price2 = (_metadata$origin2 = metadata.origin) === null || _metadata$origin2 === void 0 ? void 0 : _metadata$origin2.price) !== null && _price2 !== void 0 ? _price2 : (_metadata$origin3 = metadata.origin) === null || _metadata$origin3 === void 0 ? void 0 : _metadata$origin3.base_price;
  if (originPrice !== undefined && originPrice !== null) {
    var _parsed3 = Number(originPrice);
    if (Number.isFinite(_parsed3)) return _parsed3;
  }
  return Number((_product$selling_pric = product.selling_price) !== null && _product$selling_pric !== void 0 ? _product$selling_pric : 0);
}

/** 订单行 bundle → booking other.bundle（保留 original_price 供 setProductPrice 按比例分摊） */
function mapOrderBundleToOtherBundle(bundle) {
  return bundle.map(function (item) {
    var _ref, _ref2, _item$original_price, _ref3, _item$bundle_selling_, _item$original_total, _ref4, _item$num, _ref5, _item$quantity;
    var catalogOriginal = (_ref = (_ref2 = (_item$original_price = item.original_price) !== null && _item$original_price !== void 0 ? _item$original_price : item.product_price) !== null && _ref2 !== void 0 ? _ref2 : item.price) !== null && _ref !== void 0 ? _ref : item.bundle_selling_price;
    var selling = (_ref3 = (_item$bundle_selling_ = item.bundle_selling_price) !== null && _item$bundle_selling_ !== void 0 ? _item$bundle_selling_ : item.price) !== null && _ref3 !== void 0 ? _ref3 : catalogOriginal;
    var _omitBundleSelling = item.bundle_selling_price,
      rest = _objectWithoutProperties(item, _excluded);
    var isMarkupOrMarkdown = (item.price_type === 'markdown' || item.price_type === 'markup') && (item.price_type_ext === '' || item.price_type_ext === undefined || item.price_type_ext === null);
    if (isMarkupOrMarkdown) delete rest.original_total;
    return _objectSpread(_objectSpread(_objectSpread({}, rest), {}, {
      price: selling,
      original_price: catalogOriginal
    }, isMarkupOrMarkdown ? {} : {
      original_total: (_item$original_total = item.original_total) !== null && _item$original_total !== void 0 ? _item$original_total : catalogOriginal
    }), {}, {
      num: (_ref4 = (_item$num = item.num) !== null && _item$num !== void 0 ? _item$num : item.quantity) !== null && _ref4 !== void 0 ? _ref4 : 1,
      quantity: (_ref5 = (_item$quantity = item.quantity) !== null && _item$quantity !== void 0 ? _item$quantity : item.num) !== null && _ref5 !== void 0 ? _ref5 : 1
    });
  });
}
export function buildNormalProductCacheItemFromOrderLine(input) {
  var _product$product_id, _product$selling_pric2, _product$original_pri, _product$metadata, _product$metadata2, _product$product_vari, _originExtend$other, _product$num, _product$product_vari2, _product$num2, _title, _product$note, _originExtend$price, _originExtend$total, _originExtend$origin_, _price3, _product$order_detail;
  var product = input.product,
    sourceProduct = input.sourceProduct;
  var resolvedId = (_product$product_id = product.product_id) !== null && _product$product_id !== void 0 ? _product$product_id : sourceProduct === null || sourceProduct === void 0 ? void 0 : sourceProduct.id;
  var sourcePrice = resolveSourceProductPrice(product, sourceProduct);
  var selling = Number((_product$selling_pric2 = product.selling_price) !== null && _product$selling_pric2 !== void 0 ? _product$selling_pric2 : 0);
  var original = Number((_product$original_pri = product.original_price) !== null && _product$original_pri !== void 0 ? _product$original_pri : selling);
  var uid = (_product$metadata = product.metadata) === null || _product$metadata === void 0 ? void 0 : _product$metadata.unique_identification_number;
  var option = getProductSkuOptions(product);
  var bundle = mapOrderBundleToOtherBundle(Array.isArray(product.product_bundle) ? product.product_bundle : []);
  var originExtend = (_product$metadata2 = product.metadata) === null || _product$metadata2 === void 0 || (_product$metadata2 = _product$metadata2.origin) === null || _product$metadata2 === void 0 ? void 0 : _product$metadata2._extend;
  var productOptionString = buildProductOptionStringFromOrderLine(product);
  var discountList = product.discount_list || [];
  var other = {
    product_id: resolvedId,
    product_variant_id: (_product$product_vari = product.product_variant_id) !== null && _product$product_vari !== void 0 ? _product$product_vari : 0,
    option: option,
    bundle: originExtend !== null && originExtend !== void 0 && (_originExtend$other = originExtend.other) !== null && _originExtend$other !== void 0 && _originExtend$other.bundle ? JSON.parse(JSON.stringify(originExtend.other.bundle)) : bundle,
    quantity: (_product$num = product.num) !== null && _product$num !== void 0 ? _product$num : 1
  };
  var skuValue = {
    product_id: resolvedId,
    product_variant_id: (_product$product_vari2 = product.product_variant_id) !== null && _product$product_vari2 !== void 0 ? _product$product_vari2 : 0,
    option: other.option,
    bundle: other.bundle
  };
  var extend = _objectSpread(_objectSpread(_objectSpread({
    quantity: (_product$num2 = product.num) !== null && _product$num2 !== void 0 ? _product$num2 : 1,
    product_name: (_title = sourceProduct === null || sourceProduct === void 0 ? void 0 : sourceProduct.title) !== null && _title !== void 0 ? _title : '',
    note: (_product$note = product.note) !== null && _product$note !== void 0 ? _product$note : '',
    // v2：_extend.price 为主商品 source 价；行 composite 用 total / origin_total 展示
    price: (_originExtend$price = originExtend === null || originExtend === void 0 ? void 0 : originExtend.price) !== null && _originExtend$price !== void 0 ? _originExtend$price : sourcePrice,
    total: (_originExtend$total = originExtend === null || originExtend === void 0 ? void 0 : originExtend.total) !== null && _originExtend$total !== void 0 ? _originExtend$total : selling,
    origin_total: (_originExtend$origin_ = originExtend === null || originExtend === void 0 ? void 0 : originExtend.origin_total) !== null && _originExtend$origin_ !== void 0 ? _originExtend$origin_ : original,
    other: other,
    skuValue: JSON.parse(JSON.stringify(skuValue)),
    _skuValue: JSON.parse(JSON.stringify(skuValue)),
    isNormalProduct: true
  }, productOptionString ? {
    product_option_string: productOptionString
  } : {}), (originExtend === null || originExtend === void 0 ? void 0 : originExtend.priceOverride) !== undefined ? {
    priceOverride: originExtend.priceOverride
  } : {}), (originExtend === null || originExtend === void 0 ? void 0 : originExtend.bundle_edit) !== undefined ? {
    bundle_edit: originExtend.bundle_edit
  } : {});
  if (originExtend !== null && originExtend !== void 0 && originExtend.skuValue || originExtend !== null && originExtend !== void 0 && originExtend._skuValue) {
    extend.skuValue = JSON.parse(JSON.stringify(originExtend.skuValue || originExtend._skuValue));
    extend._skuValue = JSON.parse(JSON.stringify(extend.skuValue));
  }
  return _objectSpread(_objectSpread(_objectSpread({}, sourceProduct || {}), {}, {
    id: resolvedId,
    product_id: resolvedId,
    _id: uid,
    title: sourceProduct === null || sourceProduct === void 0 ? void 0 : sourceProduct.title,
    price: (_price3 = sourceProduct === null || sourceProduct === void 0 ? void 0 : sourceProduct.price) !== null && _price3 !== void 0 ? _price3 : sourcePrice,
    selling_price: product.selling_price,
    original_price: product.original_price
  }, productOptionString ? {
    product_option_string: productOptionString
  } : {}), {}, {
    note: product.note,
    order_detail_id: (_product$order_detail = product.order_detail_id) !== null && _product$order_detail !== void 0 ? _product$order_detail : null,
    discount_list: discountList,
    new: 0,
    autoClose: true,
    _extend: extend
  });
}