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 _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); }
/**
 * 将 booking 顶层 products[] 行映射为 ProductCard 的 dataSource（只读展示）。
 * 金额字段直接透传：payment_price → price，selling_price → total，original_price → origin_total。
 * 数据源必须为 raw.products，不得使用 order.products。
 */

function str(v) {
  if (v == null) return '';
  return String(v).trim();
}

/** 与宿主多语言对象一致：优先 auto / zh-CN / en 等 */
function pickLocalizedText(obj) {
  if (obj == null) return '';
  if (typeof obj === 'string') return obj.trim();
  if (_typeof(obj) !== 'object') return '';
  var o = obj;
  var keys = ['auto', 'zh-CN', 'zh-HK', 'en', 'ja', 'pt', 'original', 'kitchen'];
  for (var _i = 0, _keys = keys; _i < _keys.length; _i++) {
    var k = _keys[_i];
    var v = o[k];
    if (v != null && str(v)) return str(v);
  }
  for (var _i2 = 0, _Object$values = Object.values(o); _i2 < _Object$values.length; _i2++) {
    var _v = _Object$values[_i2];
    if (_v != null && typeof _v === 'string' && str(_v)) return str(_v);
    if (_v != null && _typeof(_v) === 'object' && _v !== null && !Array.isArray(_v)) {
      var inner = pickLocalizedText(_v);
      if (inner) return inner;
    }
  }
  return '';
}
function parseMoneyNumber(v) {
  if (v == null || v === '') return 0;
  if (typeof v === 'number' && Number.isFinite(v)) return v;
  var n = Number(String(v).replace(/,/g, '').trim());
  return Number.isFinite(n) ? n : 0;
}
function stableLineId(line, index) {
  var od = line.order_detail_id;
  if (od != null && String(od) !== '') return od;
  var bu = line.booking_uid;
  if (bu != null && String(bu) !== '') return String(bu);
  var pid = line.product_id;
  if (pid != null && String(pid) !== '') return "".concat(String(pid), "-").concat(index);
  return "line-".concat(index);
}

/** product_sku.option[] → ProductCard 单规格 optionType */
function mapSkuOptionRowToOptionType(opt) {
  var _opt$option_group_ite;
  var idRaw = (_opt$option_group_ite = opt.option_group_item_id) !== null && _opt$option_group_ite !== void 0 ? _opt$option_group_ite : opt.id;
  var idNum = typeof idRaw === 'number' && Number.isFinite(idRaw) ? idRaw : Number(String(idRaw !== null && idRaw !== void 0 ? idRaw : 0).replace(/\D/g, '')) || 0;
  var namePart = pickLocalizedText(opt.name);
  var groupPart = pickLocalizedText(opt.group_name);
  var label = groupPart && namePart ? "".concat(groupPart, "\uFF1A").concat(namePart) : namePart || groupPart || '—';
  var num = Math.max(1, Math.round(parseMoneyNumber(opt.num) || 1));
  var price = parseMoneyNumber(opt.add_price);
  return {
    id: idNum,
    name: label,
    num: num,
    price: price,
    total: price * num
  };
}

/** product_sku.variant[] → 一行展示文案 */
function variantRowsToSubtitle(variant) {
  if (!Array.isArray(variant)) return '';
  var parts = [];
  var _iterator = _createForOfIteratorHelper(variant),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var row = _step.value;
      if (!row || _typeof(row) !== 'object') continue;
      var r = row;
      var g = pickLocalizedText(r.group);
      var it = pickLocalizedText(r.item);
      if (g && it) parts.push("".concat(g, "\uFF1A").concat(it));else if (it) parts.push(it);else if (g) parts.push(g);
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return parts.join(' · ');
}

/**
 * 副标题：subtitle → variant 拼接 → option 名称摘要（无 add 价时）
 */
function buildProductOptionString(sku) {
  if (!sku) return undefined;
  var sub = pickLocalizedText(sku.subtitle);
  if (sub) return sub;
  var varStr = variantRowsToSubtitle(sku.variant);
  if (varStr) return varStr;
  var opts = sku.option;
  if (!Array.isArray(opts) || opts.length === 0) return undefined;
  var names = opts.filter(function (x) {
    return x && _typeof(x) === 'object';
  }).map(function (x) {
    var o = x;
    var gn = pickLocalizedText(o.group_name);
    var nn = pickLocalizedText(o.name);
    if (gn && nn) return "".concat(gn, "\uFF1A").concat(nn);
    return nn || gn;
  }).filter(Boolean);
  return names.length ? names.join(' · ') : undefined;
}

/** product_sku.option[] → 主商品 Header 下 Options 列表 */
function mapProductSkuOptions(sku) {
  if (!sku) return undefined;
  var opts = sku.option;
  if (!Array.isArray(opts) || opts.length === 0) return undefined;
  var out = opts.filter(function (x) {
    return x && _typeof(x) === 'object';
  }).map(function (x) {
    return mapSkuOptionRowToOptionType(x);
  });
  return out.length ? out : undefined;
}

/** product_bundle[] 子项 → bundleType（套餐折叠行） */
function mapBundleRowToBundleType(b, bi) {
  var _b$custom_price;
  var bundleId = b.bundle_id;
  var idNum = typeof bundleId === 'number' && Number.isFinite(bundleId) ? bundleId : Number(String(bundleId !== null && bundleId !== void 0 ? bundleId : bi).replace(/\D/g, '')) || bi;
  var num = Math.max(1, Math.round(parseMoneyNumber(b.num) || 1));
  var unit = parseMoneyNumber((_b$custom_price = b.custom_price) !== null && _b$custom_price !== void 0 ? _b$custom_price : b.price);
  var payLine = parseMoneyNumber(b.bundle_payment_price);
  var total = payLine > 0 ? payLine : unit * num;
  var optRaw = b.option;
  var nestedOpts = Array.isArray(optRaw) ? optRaw.filter(function (x) {
    return x && _typeof(x) === 'object';
  }).map(function (x) {
    return mapSkuOptionRowToOptionType(x);
  }) : undefined;
  return {
    id: idNum,
    name: pickLocalizedText(b.bundle_title) || '—',
    num: num,
    price: num > 0 ? total / num : unit,
    total: total,
    origin_total: undefined,
    price_type: str(b.price_type) || undefined,
    options: nestedOpts && nestedOpts.length ? nestedOpts : undefined
  };
}
function mapProductBundles(line) {
  var raw = line.product_bundle;
  if (!Array.isArray(raw) || raw.length === 0) return undefined;
  var out = raw.filter(function (x) {
    return x && _typeof(x) === 'object';
  }).map(function (x, i) {
    return mapBundleRowToBundleType(x, i);
  });
  return out.length ? out : undefined;
}

/** 主行金额：直接透传接口字段，不做乘除换算 */
function resolveMainLineAmounts(line) {
  var price = parseMoneyNumber(line.payment_price);
  var total = parseMoneyNumber(line.selling_price);
  var origin = parseMoneyNumber(line.original_price);
  return {
    price: price,
    total: total,
    origin_total: origin > 0 ? origin : undefined
  };
}
function resolveProductNumericId(line, index) {
  var pid = line.product_id;
  if (typeof pid === 'number' && Number.isFinite(pid)) return pid;
  var n = Number(String(pid !== null && pid !== void 0 ? pid : '').replace(/\D/g, ''));
  return Number.isFinite(n) && n > 0 ? n : index;
}

/**
 * @param line booking.products 或 order.products 同源结构（与 api-bookings-demo 一致）
 */
export function bookingProductLineToProductCardData(line, index, setting) {
  var _setting$app, _setting$app$translat, _line$resource_id, _ref, _line$image;
  var titleObj = line.product_title;
  var name = (setting === null || setting === void 0 || (_setting$app = setting.app) === null || _setting$app === void 0 || (_setting$app = _setting$app.locales) === null || _setting$app === void 0 || (_setting$app$translat = _setting$app.translation) === null || _setting$app$translat === void 0 ? void 0 : _setting$app$translat.call(_setting$app, titleObj)) || pickLocalizedText(titleObj) || str(line.name) || str(line.title) || '—';
  var sku = line.product_sku;
  var qty = Math.max(1, Math.round(parseMoneyNumber(line.product_quantity) || 1));
  var _resolveMainLineAmoun = resolveMainLineAmounts(line),
    price = _resolveMainLineAmoun.price,
    total = _resolveMainLineAmoun.total,
    origin_total = _resolveMainLineAmoun.origin_total;
  var product_option_string = buildProductOptionString(sku);
  var options = mapProductSkuOptions(sku);
  var bundle = mapProductBundles(line);
  var noteRaw = line.note;
  var note = str(noteRaw) || undefined;
  var likeRaw = line.like_status;
  var like_status = likeRaw != null && str(likeRaw) ? str(likeRaw) : undefined;
  var resId = (_line$resource_id = line.resource_id) !== null && _line$resource_id !== void 0 ? _line$resource_id : line.relation_id;
  var resource_id = resId != null && String(resId) !== '' ? resId : undefined;
  var rfn = line.relation_form_name;
  var relation_form_name = rfn != null && str(rfn) ? str(rfn) : undefined;
  var imageRaw = (_ref = (_line$image = line.image) !== null && _line$image !== void 0 ? _line$image : line.product_image) !== null && _ref !== void 0 ? _ref : sku === null || sku === void 0 ? void 0 : sku.image;
  var image = imageRaw != null && str(imageRaw) ? str(imageRaw) : undefined;
  return {
    _id: stableLineId(line, index),
    id: resolveProductNumericId(line, index),
    name: name,
    price: price,
    total: total,
    num: qty,
    origin_total: origin_total,
    image: image,
    isShowNote: Boolean(note),
    note: note,
    like_status: like_status,
    bundle: bundle,
    options: options,
    isShowAction: false,
    product_option_string: product_option_string,
    discount_reason: undefined,
    relation_products: undefined,
    resource_id: resource_id,
    relation_form_name: relation_form_name,
    holder_title: undefined,
    start_date: str(line.start_date) || undefined,
    end_date: str(line.end_date) || undefined,
    is_show_duration: undefined,
    is_show_week: undefined
  };
}