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 { resolveLocalizedText } from "./resolveLocalizedText";

/**
 * 读取订单行规格：优先 OS v2 的 product_sku.option，回落 legacy product_option_item。
 *
 * @example
 * getLineProductOptions({ product_sku: { option: [{ name: 'Large' }] } });
 * // => [{ name: 'Large' }]
 */
export function getLineProductOptions(line) {
  var _line$product_sku;
  if (!line) return [];
  var skuOptions = (_line$product_sku = line.product_sku) === null || _line$product_sku === void 0 ? void 0 : _line$product_sku.option;
  if (Array.isArray(skuOptions)) {
    return skuOptions;
  }
  var legacyOptions = line.product_option_item;
  if (Array.isArray(legacyOptions)) {
    return legacyOptions;
  }
  return [];
}

/**
 * 读取 booking detail 规格：优先 product_sku.option，回落 legacy product_option_item。
 *
 * @example
 * getDetailProductOptions({ product_sku: { option: [{ id: 1 }] } });
 */
export function getDetailProductOptions(detail) {
  var _detail$product_sku;
  if (!detail) return [];
  var skuOptions = (_detail$product_sku = detail.product_sku) === null || _detail$product_sku === void 0 ? void 0 : _detail$product_sku.option;
  if (Array.isArray(skuOptions)) {
    return skuOptions;
  }
  var legacyOptions = detail.product_option_item;
  if (Array.isArray(legacyOptions)) {
    return legacyOptions;
  }
  return [];
}

/**
 * 计算单个规格项单价合计：add_price/price × num。
 *
 * @example
 * normalizeOptionUnitPrice({ add_price: 79.2, num: 1 }); // 79.2
 */
export function normalizeOptionUnitPrice(option) {
  var _option$add_price, _option$num;
  var price = Number((_option$add_price = option === null || option === void 0 ? void 0 : option.add_price) !== null && _option$add_price !== void 0 ? _option$add_price : option === null || option === void 0 ? void 0 : option.price) || 0;
  var num = Number((_option$num = option === null || option === void 0 ? void 0 : option.num) !== null && _option$num !== void 0 ? _option$num : option === null || option === void 0 ? void 0 : option.quantity) || 0;
  return price * num;
}

/**
 * 计算订单行规格加价合计，对齐 OS sumOptionUnitPrice。
 *
 * @example
 * sumLineOptionUnitPrice({ product_sku: { option: [{ add_price: 2, num: 3 }] } }); // 6
 */
export function sumLineOptionUnitPrice(line) {
  return getLineProductOptions(line).reduce(function (total, option) {
    return total + normalizeOptionUnitPrice(option);
  }, 0);
}

/**
 * 将 OS 规格项映射为购物车 UI 可直接消费的 shape（name / price / num）。
 *
 * @example
 * normalizeOptionForDisplay({ name: 'Large', add_price: 1, num: 2 });
 */
export function normalizeOptionForDisplay(option) {
  var _option$name, _option$add_price2, _ref, _option$num2;
  var name = resolveLocalizedText((_option$name = option === null || option === void 0 ? void 0 : option.name) !== null && _option$name !== void 0 ? _option$name : option === null || option === void 0 ? void 0 : option.title);
  var price = (_option$add_price2 = option === null || option === void 0 ? void 0 : option.add_price) !== null && _option$add_price2 !== void 0 ? _option$add_price2 : option === null || option === void 0 ? void 0 : option.price;
  var num = (_ref = (_option$num2 = option === null || option === void 0 ? void 0 : option.num) !== null && _option$num2 !== void 0 ? _option$num2 : option === null || option === void 0 ? void 0 : option.quantity) !== null && _ref !== void 0 ? _ref : 1;
  return _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, option), name ? {
    name: name
  } : {}), price !== undefined && price !== null && price !== '' ? {
    price: price
  } : {}), {}, {
    num: num
  });
}

/**
 * 构造 updateOrderProduct 所需的 product_sku patch。
 *
 * @example
 * buildProductSkuPatch(line, [{ option_group_item_id: 1, add_price: 2, num: 1 }]);
 */
export function buildProductSkuPatch(line, options) {
  return _objectSpread(_objectSpread({}, line !== null && line !== void 0 && line.product_sku && _typeof(line.product_sku) === 'object' ? line.product_sku : {}), {}, {
    option: options
  });
}

/**
 * 解析编辑 patch 里的规格项：优先 product_sku.option，其次 legacy product_option_item，最后回落行内规格。
 *
 * @example
 * resolvePatchProductOptions(orderLine, { product_sku: { option: [{ id: 1 }] } });
 */
export function resolvePatchProductOptions(orderLine, productPatch) {
  var _productPatch$product;
  if (Array.isArray(productPatch === null || productPatch === void 0 || (_productPatch$product = productPatch.product_sku) === null || _productPatch$product === void 0 ? void 0 : _productPatch$product.option)) {
    return productPatch.product_sku.option;
  }
  if (Array.isArray(productPatch === null || productPatch === void 0 ? void 0 : productPatch.product_option_item)) {
    return productPatch.product_option_item;
  }
  return getLineProductOptions(orderLine);
}