var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/pisellReservation/data/salesEmbeddedResourceToTableRows.ts
var salesEmbeddedResourceToTableRows_exports = {};
__export(salesEmbeddedResourceToTableRows_exports, {
  isSalesEmbeddedResourceBookingsList: () => isSalesEmbeddedResourceBookingsList,
  salesEmbeddedResourceListToTableRows: () => salesEmbeddedResourceListToTableRows
});
module.exports = __toCommonJS(salesEmbeddedResourceToTableRows_exports);
var import_bookingToReservationTables = require("./bookingToReservationTables");
var import_formResourceToReservationTables = require("./formResourceToReservationTables");
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) {
  const idRaw = o.form_record_id ?? o.resource_id ?? o.id;
  if (idRaw == null) return null;
  const fid = o.form_id;
  const 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) : void 0,
    main_field: typeof o.main_field === "string" ? o.main_field : void 0,
    form_code: typeof o.form_code === "string" ? o.form_code : void 0,
    capacity: typeof cap === "number" ? cap : cap != null && String(cap).trim() !== "" ? Number(cap) : void 0
  };
}
function isSalesEmbeddedResourceBookingsList(items) {
  if (!Array.isArray(items) || items.length === 0) return false;
  const first = items[0];
  if (!first || typeof first !== "object") return false;
  const r = first;
  if (!Array.isArray(r.bookings)) return false;
  return osResourceRowLooksValid(r);
}
function patchRowFromHostBooking(row, hostBookings) {
  if (!hostBookings.length) return row;
  const b0 = hostBookings[0];
  const next = { ...row, ...(0, import_bookingToReservationTables.hostBookingUiPatchFromRecord)(b0) };
  const ord = b0.order;
  const pay = ord == null ? 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;
}
function salesEmbeddedResourceListToTableRows(items) {
  const rows = [];
  for (const raw of items) {
    if (!raw || typeof raw !== "object") continue;
    const o = raw;
    const pageItem = salesOsItemToFormResourcePageItem(o);
    if (!pageItem) continue;
    const base = (0, import_formResourceToReservationTables.formResourceItemToReservationRow)(pageItem);
    if (!base) continue;
    const rawBookings = Array.isArray(o.bookings) ? o.bookings : [];
    const bookingRow = (0, import_bookingToReservationTables.mergeBookingsForResourceRecord)(base.id, rawBookings);
    if (bookingRow) {
      const merged = (0, import_formResourceToReservationTables.mergeResourceRowsWithBookingRows)([base], [bookingRow])[0];
      rows.push(patchRowFromHostBooking(merged, rawBookings));
    } else {
      rows.push((0, import_bookingToReservationTables.applyHostResourceShellToBaseRow)(base, o));
    }
  }
  return rows;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  isSalesEmbeddedResourceBookingsList,
  salesEmbeddedResourceListToTableRows
});
