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 _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 _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; }
/**
 * 将 GET /shop/form/resource/page 的 list 转为平面图/表格绑定的 {@link PisellReservationTableRow}
 */

function normalizeMainField(raw) {
  var _first$trim;
  if (raw == null || typeof raw !== 'string') return '';
  var first = raw.split(' ## ')[0];
  return (_first$trim = first === null || first === void 0 ? void 0 : first.trim()) !== null && _first$trim !== void 0 ? _first$trim : '';
}

/** 解析 form/resource/page 项的主键，与预约 resources[].relation_id 对齐 */
function normalizeFormResourceRecordId(item) {
  var raw = item.id;
  if (typeof raw === 'number' && Number.isFinite(raw)) {
    var n = Math.trunc(raw);
    if (n > 0) return String(n);
    return null;
  }
  if (typeof raw === 'string') {
    var t = raw.trim();
    if (!t || t === '0') return null;
    var _n = Number(t);
    if (Number.isFinite(_n) && _n > 0) return String(Math.trunc(_n));
    if (/^\d+$/.test(t)) return t;
  }
  return null;
}

/**
 * 从 `GET form/resource/page` 的 list 提取 form_record 主键（与预约 `resources.relation_id` / 平面图 recordId 对齐），
 * 用于 `GET schedule/booking` 的 `form_record_ids` 按桌筛选，避免拉全量日程。
 */
export function formResourceListToFormRecordIds(list) {
  if (!Array.isArray(list) || list.length === 0) return [];
  var ids = [];
  var _iterator = _createForOfIteratorHelper(list),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var raw = _step.value;
      var s = normalizeFormResourceRecordId(raw);
      if (s == null) continue;
      var n = Number(s);
      if (Number.isFinite(n) && n > 0) ids.push(Math.trunc(n));
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return ids;
}

/**
 * 单条资源记录 → 桌位行（平面图 dataBinding.recordId 与 id 对齐）
 */
export function formResourceItemToReservationRow(item) {
  if (item == null || _typeof(item) !== 'object') return null;
  var id = normalizeFormResourceRecordId(item);
  if (id == null) return null;
  var name = normalizeMainField(item.main_field);
  return {
    id: id,
    code: name || id,
    main_field: typeof item.main_field === 'string' ? item.main_field : undefined,
    form_id: item.form_id,
    cardStatus: 'available',
    area: typeof item.form_code === 'string' && item.form_code.trim() ? item.form_code : undefined,
    capacityLabel: item.capacity != null && String(item.capacity).trim() !== '' ? String(item.capacity) : undefined
  };
}

/**
 * 资源列表 → 桌位数据源
 */
export function formResourceListToReservationTableRows(list) {
  if (!Array.isArray(list) || list.length === 0) return [];
  var rows = [];
  var _iterator2 = _createForOfIteratorHelper(list),
    _step2;
  try {
    for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
      var raw = _step2.value;
      var row = formResourceItemToReservationRow(raw);
      if (row) rows.push(row);
    }
  } catch (err) {
    _iterator2.e(err);
  } finally {
    _iterator2.f();
  }
  return rows;
}

/**
 * 将资源行与「按 resource id 聚合」的预约行合并：保留资源侧 id / code / main_field / area / capacity，
 * 覆盖预约卡展示的 cardStatus、客人、时段、slots 等（平面图 dataBinding.recordId 与 id 对齐）。
 */
export function mergeResourceRowsWithBookingRows(resourceRows, bookingRows) {
  if (resourceRows.length === 0) return [];
  var byId = new Map();
  var _iterator3 = _createForOfIteratorHelper(bookingRows),
    _step3;
  try {
    for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
      var r = _step3.value;
      byId.set(r.id, r);
    }
  } catch (err) {
    _iterator3.e(err);
  } finally {
    _iterator3.f();
  }
  return resourceRows.map(function (base) {
    var b = byId.get(base.id);
    if (!b) return base;
    return _objectSpread(_objectSpread({}, base), {}, {
      cardStatus: b.cardStatus,
      guestName: b.guestName,
      guestPhone: b.guestPhone,
      time: b.time,
      timeEnd: b.timeEnd,
      partySize: b.partySize,
      startsInMinutes: b.startsInMinutes,
      priceDisplay: b.priceDisplay,
      isPaid: b.isPaid,
      delayMinutes: b.delayMinutes,
      lateMinutes: b.lateMinutes,
      elapsedLabel: b.elapsedLabel,
      progress: b.progress,
      progressTone: b.progressTone,
      topAccentOrange: b.topAccentOrange,
      nextGuest: b.nextGuest,
      nextTime: b.nextTime,
      slots: b.slots,
      moreCount: b.moreCount,
      barSlots: b.barSlots,
      barRemainingLabel: b.barRemainingLabel,
      floorMapSourceBookings: b.floorMapSourceBookings,
      floorMapReferenceDay: b.floorMapReferenceDay,
      hostReservedStatus: b.hostReservedStatus,
      hostIsTimeout: b.hostIsTimeout
    });
  });
}