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 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); }
/**
 * `sales.getResourceBookingList` 另一种返回形态：按桌（资源）一行，内含 `bookings[]`。
 * 检测到该形态时直接生成平面图 `tables`，无需再与 form/resource/page + 扁平预约做合并。
 */
import { applyHostResourceShellToBaseRow, hostBookingUiPatchFromRecord, mergeBookingsForResourceRecord } from "./bookingToReservationTables";
import { formResourceItemToReservationRow, mergeResourceRowsWithBookingRows } from "./formResourceToReservationTables";
/** 宿主返回的单条「桌台 + 预约列表」是否像资源行（与扁平 schedule/booking 项区分） */
function osResourceRowLooksValid(o) {
  if (o.schedule_event_id != null && o.form_record_id == null && o.resource_id == null && typeof o.main_field !== 'string') {
    return false;
  }
  return o.form_record_id != null || o.resource_id != null || typeof o.main_field === 'string' || o.id != null && o.schedule_event_id == null;
}
function salesOsItemToFormResourcePageItem(o) {
  var _ref, _o$form_record_id;
  var idRaw = (_ref = (_o$form_record_id = o.form_record_id) !== null && _o$form_record_id !== void 0 ? _o$form_record_id : o.resource_id) !== null && _ref !== void 0 ? _ref : o.id;
  if (idRaw == null) return null;
  var fid = o.form_id;
  var cap = o.capacity;
  return {
    id: typeof idRaw === 'number' ? idRaw : typeof idRaw === 'string' ? idRaw : String(idRaw),
    form_id: typeof fid === 'number' ? fid : fid != null && fid !== '' ? Number(fid) : undefined,
    main_field: typeof o.main_field === 'string' ? o.main_field : undefined,
    form_code: typeof o.form_code === 'string' ? o.form_code : undefined,
    capacity: typeof cap === 'number' ? cap : cap != null && String(cap).trim() !== '' ? Number(cap) : undefined
  };
}

/**
 * 是否为「资源行 + bookings」列表（与扁平预约项数组区分：后者一般无 `bookings` 数组字段）。
 */
export function isSalesEmbeddedResourceBookingsList(items) {
  if (!Array.isArray(items) || items.length === 0) return false;
  var first = items[0];
  if (!first || _typeof(first) !== 'object') return false;
  var r = first;
  if (!Array.isArray(r.bookings)) return false;
  return osResourceRowLooksValid(r);
}

/** 从宿主 booking 项上摘平面图展示用补丁（payment / status 信号等） */
function patchRowFromHostBooking(row, hostBookings) {
  if (!hostBookings.length) return row;
  var b0 = hostBookings[0];
  var next = _objectSpread(_objectSpread({}, row), hostBookingUiPatchFromRecord(b0));
  var ord = b0.order;
  var pay = ord === null || ord === void 0 ? void 0 : ord.payment_status;
  if (pay === 'paid') {
    next.isPaid = true;
  }
  if (next.hostReservedStatus !== 'late' && String(b0.reserved_status) === 'late' && String(b0.status) !== 'reserved') {
    next.hostReservedStatus = 'late';
    if (next.lateMinutes == null) next.lateMinutes = 1;
  }
  return next;
}

/**
 * `getResourceBookingList` 返回的带 `bookings` 的资源列表 → `PisellReservationTableRow[]`
 */
export function salesEmbeddedResourceListToTableRows(items) {
  var rows = [];
  var _iterator = _createForOfIteratorHelper(items),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var raw = _step.value;
      if (!raw || _typeof(raw) !== 'object') continue;
      var o = raw;
      var pageItem = salesOsItemToFormResourcePageItem(o);
      if (!pageItem) continue;
      var base = formResourceItemToReservationRow(pageItem);
      if (!base) continue;
      var rawBookings = Array.isArray(o.bookings) ? o.bookings : [];
      var bookingRow = mergeBookingsForResourceRecord(base.id, rawBookings);
      if (bookingRow) {
        var merged = mergeResourceRowsWithBookingRows([base], [bookingRow])[0];
        rows.push(patchRowFromHostBooking(merged, rawBookings));
      } else {
        rows.push(applyHostResourceShellToBaseRow(base, o));
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return rows;
}