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 { isWalkInCustomer } from "../selectors";
import { pickIdentity } from "./identity";

/**
 * enrichCartItem 多源合并（仅活在 React 内存，不污染 OS tempOrder.products 提交契约）。
 *
 * 数据来源按字段优先级：
 *  - title / cover / variant_title / option / bundle 标题：bookings*.detail（编辑态权威）
 *      → productsCatalog 兜底（新建态从商品列表点加购，编辑态做兜底）
 *  - duration / product_resource / capacity：bookings*.product（编辑态商品快照）
 *      → productsCatalog 兜底
 *  - start_at / end_at / resource_id：bookings 主资源（仅编辑态）
 *  - note：detail.note → line.note（行级备注）
 *
 * 入参严格遵守 .cursor/rules/data-field-semantics.mdc：bookings 通过 product_uid 反查
 * （不发明 booking_id ↔ product 之间的等价关系），catalog 通过 product_id 字符串比对。
 */
export function enrichCartItem(line, ctx) {
  var _line$metadata, _ctx$bookingDetailMap, _catalog$variants, _ref, _detail$product_title, _ref2, _detail$product_cover, _ref3, _detail$variant_title, _ref4, _detail$note, _productSummary$durat, _productSummary$produ, _matched$relation_id;
  var productUid = (_line$metadata = line.metadata) === null || _line$metadata === void 0 ? void 0 : _line$metadata.unique_identification_number;
  var matched = productUid ? (_ctx$bookingDetailMap = ctx.bookingDetailMap) === null || _ctx$bookingDetailMap === void 0 || (_ctx$bookingDetailMap = _ctx$bookingDetailMap[String(productUid)]) === null || _ctx$bookingDetailMap === void 0 ? void 0 : _ctx$bookingDetailMap[0] : undefined;
  var detail = matched === null || matched === void 0 ? void 0 : matched.detail;
  var productSummary = matched === null || matched === void 0 ? void 0 : matched.product;
  var catalog = ctx.productsCatalog.find(function (p) {
    return String(p.id) === String(line.product_id);
  });
  var variant = catalog === null || catalog === void 0 || (_catalog$variants = catalog.variants) === null || _catalog$variants === void 0 ? void 0 : _catalog$variants.find(function (v) {
    return (v === null || v === void 0 ? void 0 : v.id) === line.product_variant_id;
  });
  var detailBundles = Array.isArray(detail === null || detail === void 0 ? void 0 : detail.product_bundle) ? detail.product_bundle : [];
  var detailOptions = Array.isArray(detail === null || detail === void 0 ? void 0 : detail.product_option_item) ? detail.product_option_item : [];
  var catalogBundles = Array.isArray(catalog === null || catalog === void 0 ? void 0 : catalog.bundles) ? catalog.bundles : [];
  var catalogOptions = Array.isArray(catalog === null || catalog === void 0 ? void 0 : catalog.options) ? catalog.options : [];
  var bundleTitles = (line.product_bundle || []).map(function (b) {
    var matchedDetail = detailBundles.find(function (db) {
      return (db === null || db === void 0 ? void 0 : db.bundle_id) === (b === null || b === void 0 ? void 0 : b.bundle_id);
    });
    if (matchedDetail !== null && matchedDetail !== void 0 && matchedDetail.title) return String(matchedDetail.title);
    var matchedCatalog = catalogBundles.find(function (cb) {
      return (cb === null || cb === void 0 ? void 0 : cb.id) === (b === null || b === void 0 ? void 0 : b.bundle_id);
    });
    if (matchedCatalog !== null && matchedCatalog !== void 0 && matchedCatalog.title) return String(matchedCatalog.title);
    return '';
  });
  var optionTitles = (line.product_option_item || []).map(function (o) {
    var matchedDetail = detailOptions.find(function (d) {
      return (d === null || d === void 0 ? void 0 : d.product_option_item_id) === (o === null || o === void 0 ? void 0 : o.product_option_item_id);
    });
    if (matchedDetail !== null && matchedDetail !== void 0 && matchedDetail.title) return String(matchedDetail.title);
    var matchedCatalog = catalogOptions.find(function (co) {
      return (co === null || co === void 0 ? void 0 : co.id) === (o === null || o === void 0 ? void 0 : o.product_option_item_id);
    });
    if (matchedCatalog !== null && matchedCatalog !== void 0 && matchedCatalog.title) return String(matchedCatalog.title);
    return '';
  });
  var startAt = matched && matched.start_date && matched.start_time ? "".concat(matched.start_date, " ").concat(matched.start_time) : null;
  var endAt = matched && matched.end_date && matched.end_time ? "".concat(matched.end_date, " ").concat(matched.end_time) : null;
  return _objectSpread(_objectSpread({}, line), {}, {
    _identity: pickIdentity(line),
    title: (_ref = (_detail$product_title = detail === null || detail === void 0 ? void 0 : detail.product_title) !== null && _detail$product_title !== void 0 ? _detail$product_title : catalog === null || catalog === void 0 ? void 0 : catalog.title) !== null && _ref !== void 0 ? _ref : '',
    cover: (_ref2 = (_detail$product_cover = detail === null || detail === void 0 ? void 0 : detail.product_cover) !== null && _detail$product_cover !== void 0 ? _detail$product_cover : catalog === null || catalog === void 0 ? void 0 : catalog.cover) !== null && _ref2 !== void 0 ? _ref2 : '',
    variant_title: (_ref3 = (_detail$variant_title = detail === null || detail === void 0 ? void 0 : detail.variant_title) !== null && _detail$variant_title !== void 0 ? _detail$variant_title : variant === null || variant === void 0 ? void 0 : variant.title) !== null && _ref3 !== void 0 ? _ref3 : '',
    bundle_titles: bundleTitles,
    option_titles: optionTitles,
    note: (_ref4 = (_detail$note = detail === null || detail === void 0 ? void 0 : detail.note) !== null && _detail$note !== void 0 ? _detail$note : line.note) !== null && _ref4 !== void 0 ? _ref4 : '',
    duration: (_productSummary$durat = productSummary === null || productSummary === void 0 ? void 0 : productSummary.duration) !== null && _productSummary$durat !== void 0 ? _productSummary$durat : catalog === null || catalog === void 0 ? void 0 : catalog.duration,
    product_resource: (_productSummary$produ = productSummary === null || productSummary === void 0 ? void 0 : productSummary.product_resource) !== null && _productSummary$produ !== void 0 ? _productSummary$produ : catalog === null || catalog === void 0 ? void 0 : catalog.product_resource,
    capacity: productSummary === null || productSummary === void 0 ? void 0 : productSummary.capacity,
    start_at: startAt,
    end_at: endAt,
    resource_id: (_matched$relation_id = matched === null || matched === void 0 ? void 0 : matched.relation_id) !== null && _matched$relation_id !== void 0 ? _matched$relation_id : null
  });
}

/**
 * 批量 enrich，保留行顺序。
 */
export function enrichCartItems(lines, ctx) {
  if (!Array.isArray(lines) || lines.length === 0) return [];
  return lines.map(function (line) {
    return enrichCartItem(line, ctx);
  });
}

/**
 * 客户视图：selected 优先（OS CustomerModule 给出的最完整客户对象），
 * 缺失时回落到 tempOrder.customer_*（编辑态服务端 hydrate 后写入 tempOrder 顶层）。
 *
 * 不复用 ticketBooking 老 _formatBookingDetail 中的兜底链
 * （`customer_id || customer?.id || customer?.customer_id || 1`），避免盲推断 walk-in 语义。
 */
export function enrichCustomer(selected, tempOrder) {
  if (selected && selected.id !== undefined && selected.id !== null && selected.id !== '' && selected.id !== 0) {
    var _selected$name;
    return {
      id: selected.id,
      name: (_selected$name = selected.name) !== null && _selected$name !== void 0 ? _selected$name : '',
      phone: selected.phone,
      email: selected.email,
      country_calling_code: selected.country_calling_code,
      cover: selected.cover,
      isWalkIn: isWalkInCustomer(selected)
    };
  }
  if (tempOrder && (tempOrder.customer_id != null || tempOrder.customer_name)) {
    var _tempOrder$customer_i, _tempOrder$customer_n;
    var id = (_tempOrder$customer_i = tempOrder.customer_id) !== null && _tempOrder$customer_i !== void 0 ? _tempOrder$customer_i : '';
    return {
      id: id,
      name: (_tempOrder$customer_n = tempOrder.customer_name) !== null && _tempOrder$customer_n !== void 0 ? _tempOrder$customer_n : '',
      phone: tempOrder.phone,
      email: tempOrder.email,
      country_calling_code: tempOrder.country_calling_code,
      cover: undefined,
      isWalkIn: isWalkInCustomer({
        id: id
      })
    };
  }
  return null;
}