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/mapReservationToVenueWallCards.ts
var mapReservationToVenueWallCards_exports = {};
__export(mapReservationToVenueWallCards_exports, {
  mapReservationRowsToVenueWallCards: () => mapReservationRowsToVenueWallCards
});
module.exports = __toCommonJS(mapReservationToVenueWallCards_exports);
var import_floorRoomCardUtils = require("../floorRoomCard/floorRoomCardUtils");
var import_mapRowToFloorRoomCardData = require("../floorRoomCard/mapRowToFloorRoomCardData");
function mapReservationRowsToVenueWallCards(rows) {
  return rows.map(mapReservationTableRowToVenueWallCard);
}
function formatResourceWallContactLine(row) {
  const roomTitle = String(row.code || row.main_field || row.id || "").trim();
  const data = (0, import_mapRowToFloorRoomCardData.mapPisellReservationRowToFloorRoomCardData)(row, roomTitle);
  const primary = (0, import_floorRoomCardUtils.getFloorRoomCardPrimaryBooking)(data);
  const showPhoneOnRow = row.cardStatus === "booked";
  const primaryLine = primary ? (0, import_floorRoomCardUtils.formatFloorRoomCardGuestDisplayLine)(primary, showPhoneOnRow).trim() : "";
  if (primaryLine) return primaryLine;
  for (const b of data.bookings) {
    const t = (0, import_floorRoomCardUtils.formatFloorRoomCardGuestDisplayLine)(b, showPhoneOnRow).trim();
    if (t) return t;
  }
  return "";
}
function formatResourceWallNextBookingGuestLine(row, nextBooking) {
  const showPhoneOnRow = row.cardStatus === "booked";
  const fromModel = (0, import_floorRoomCardUtils.formatFloorRoomCardGuestDisplayLine)(
    nextBooking,
    showPhoneOnRow
  ).trim() || (0, import_floorRoomCardUtils.formatFloorRoomCardGuestDisplayLine)(nextBooking, true).trim();
  if (fromModel) return fromModel;
  const raw = resolveFloorRawForNextBooking(row, nextBooking);
  if (raw) {
    const h = (0, import_mapRowToFloorRoomCardData.resolveFloorRoomCardHolder)(raw);
    const fromRaw = (0, import_floorRoomCardUtils.formatFloorRoomCardGuestDisplayLineFromHolder)(h, showPhoneOnRow).trim() || (0, import_floorRoomCardUtils.formatFloorRoomCardGuestDisplayLineFromHolder)(h, true).trim();
    if (fromRaw) return fromRaw;
  }
  return (0, import_floorRoomCardUtils.formatFloorRoomCardNextGuestStripLine)(nextBooking).trim();
}
function resolveFloorRawForNextBooking(row, nextBooking) {
  const src = row.floorMapSourceBookings;
  if (!Array.isArray(src) || src.length === 0) return void 0;
  const wantBid = String(nextBooking.booking_id ?? "").trim();
  if (wantBid && !wantBid.startsWith("syn_")) {
    const byId = src.find((x) => {
      const r = x;
      const id = String(r.booking_id ?? r.id ?? "").trim();
      return id !== "" && id === wantBid;
    });
    if (byId) return byId;
  }
  const byNext = src.find((x) => {
    const r = x;
    const n = r.is_next ?? r.isNext;
    return n === true || n === "true" || n === 1 || n === "1";
  });
  return byNext;
}
function sortBookingsByStartAsc(bookings) {
  return [...bookings].sort((a, b) => {
    const as = (0, import_floorRoomCardUtils.floorRoomCardBookingStartEndMs)(a).start;
    const bs = (0, import_floorRoomCardUtils.floorRoomCardBookingStartEndMs)(b).start;
    return as - bs;
  });
}
function resolveVenueWallCurrentNextByTime(bookings, nowMs) {
  if (bookings.length === 0) {
    return { mode: "empty", current: null, next: null };
  }
  const s = sortBookingsByStartAsc(bookings);
  const activeIdx = s.findIndex(
    (b) => (0, import_floorRoomCardUtils.isFloorRoomCardBookingInProgress)(b, nowMs)
  );
  if (activeIdx >= 0) {
    const cur = s[activeIdx];
    const nx = activeIdx + 1 < s.length ? s[activeIdx + 1] : null;
    return {
      mode: nx ? "both" : "currentOnly",
      current: cur,
      next: nx
    };
  }
  const upcomingIdx = s.findIndex((b) => {
    const { start } = (0, import_floorRoomCardUtils.floorRoomCardBookingStartEndMs)(b);
    return Number.isFinite(start) && nowMs < start;
  });
  if (upcomingIdx >= 0) {
    const up = s[upcomingIdx];
    const { start: sUp } = (0, import_floorRoomCardUtils.floorRoomCardBookingStartEndMs)(up);
    if (upcomingIdx > 0) {
      const prev = s[upcomingIdx - 1];
      const { end: ePrev } = (0, import_floorRoomCardUtils.floorRoomCardBookingStartEndMs)(prev);
      if (Number.isFinite(ePrev) && nowMs >= ePrev && Number.isFinite(sUp) && nowMs < sUp) {
        return { mode: "nextOnly", current: null, next: up };
      }
    }
    const nx = upcomingIdx + 1 < s.length ? s[upcomingIdx + 1] : null;
    return {
      mode: nx ? "both" : "single",
      current: up,
      next: nx
    };
  }
  return { mode: "empty", current: null, next: null };
}
function buildNextListEntry(row, nextBooking, nowMs) {
  const booking = formatResourceWallNextBookingGuestLine(row, nextBooking).trim();
  const time = (0, import_floorRoomCardUtils.formatFloorRoomCardBookingTimeRange)(nextBooking).trim();
  const { start } = (0, import_floorRoomCardUtils.floorRoomCardBookingStartEndMs)(nextBooking);
  let startInMin = 0;
  if (Number.isFinite(start)) {
    startInMin = Math.max(0, Math.ceil((start - nowMs) / 6e4));
  }
  return {
    time: time || "—",
    booking: booking || "—",
    startInMin
  };
}
function applyStatusFromPrimaryBooking(primary, nowMs) {
  let status = "available";
  let remainingMin = void 0;
  let startsInMin = void 0;
  let lateMin = void 0;
  let sessionStartMs = void 0;
  let sessionEndMs = void 0;
  const host = primary.hostBookingStatus;
  if (host === "reserved") {
    const { start: s } = (0, import_floorRoomCardUtils.floorRoomCardBookingStartEndMs)(primary);
    if (Number.isFinite(s)) {
      const startMinuteMs = (0, import_floorRoomCardUtils.reservationScheduleMinuteFloorMs)(s);
      const lateThresholdMs = (0, import_floorRoomCardUtils.reservationScheduleReservedLateThresholdMs)(s);
      if (nowMs >= lateThresholdMs) {
        status = "late";
        lateMin = Math.max(0, Math.ceil((nowMs - lateThresholdMs) / 6e4));
      } else {
        status = "boardingSoon";
        startsInMin = Math.max(
          0,
          Math.ceil((startMinuteMs - nowMs) / 6e4)
        );
      }
    } else {
      status = "boardingSoon";
    }
  } else if (host === "occupied") {
    const se = (0, import_floorRoomCardUtils.floorRoomCardBookingStartEndMs)(primary);
    const { start: s0, end: e } = se;
    if (Number.isFinite(s0)) {
      sessionStartMs = (0, import_floorRoomCardUtils.reservationScheduleMinuteFloorMs)(s0);
    }
    if (Number.isFinite(e)) {
      const overtimeMs = (0, import_floorRoomCardUtils.reservationScheduleOccupiedOvertimeThresholdMs)(e);
      sessionEndMs = overtimeMs;
      const remainingNominalMin = Math.ceil((e - nowMs) / 6e4);
      remainingMin = Math.max(0, remainingNominalMin);
      const remainingUntilOvertimeMin = Math.ceil(
        (overtimeMs - nowMs) / 6e4
      );
      if (nowMs >= overtimeMs) status = "expired";
      else if (remainingUntilOvertimeMin <= 10 && remainingUntilOvertimeMin > 0)
        status = "endingSoon";
      else status = "inUse";
    } else {
      status = "inUse";
    }
  }
  return {
    status,
    remainingMin,
    startsInMin,
    lateMin,
    sessionStartMs,
    sessionEndMs
  };
}
function mapReservationTableRowToVenueWallCard(row) {
  const { cardStatus } = row;
  const isAvailable = cardStatus === "available" || cardStatus === "maintenance";
  const nowMs = Date.now();
  const roomTitle = String(row.code || row.main_field || row.id || "").trim();
  const roomData = (0, import_mapRowToFloorRoomCardData.mapPisellReservationRowToFloorRoomCardData)(row, roomTitle);
  const bookings = roomData.bookings ?? [];
  let nextList = void 0;
  let wallUseNextLabelHideNextRow = void 0;
  let currentTime = "";
  let currentBookingDisplay = "";
  let statusPack = {
    status: "available",
    remainingMin: void 0,
    startsInMin: void 0,
    lateMin: void 0,
    sessionStartMs: void 0,
    sessionEndMs: void 0
  };
  if (bookings.length > 0) {
    const slot = resolveVenueWallCurrentNextByTime(bookings, nowMs);
    if (slot.mode === "nextOnly" && slot.next) {
      const b = slot.next;
      currentTime = (0, import_floorRoomCardUtils.formatFloorRoomCardBookingTimeRange)(b).trim();
      currentBookingDisplay = formatResourceWallNextBookingGuestLine(
        row,
        b
      ).trim();
      if (!isAvailable) {
        statusPack = applyStatusFromPrimaryBooking(b, nowMs);
      } else {
        statusPack = {
          status: "available",
          remainingMin: void 0,
          startsInMin: void 0,
          lateMin: void 0,
          sessionStartMs: void 0,
          sessionEndMs: void 0
        };
      }
      nextList = void 0;
      wallUseNextLabelHideNextRow = true;
    } else if (slot.mode === "empty") {
      nextList = void 0;
      currentTime = "";
      currentBookingDisplay = "Available to Book";
      statusPack = {
        status: "available",
        remainingMin: void 0,
        startsInMin: void 0,
        lateMin: void 0,
        sessionStartMs: void 0,
        sessionEndMs: void 0
      };
    } else if (slot.current) {
      const primary = slot.current;
      const currentNotStarted = (0, import_floorRoomCardUtils.isFloorRoomCardBookingNotStarted)(
        primary,
        nowMs
      );
      currentTime = (0, import_floorRoomCardUtils.formatFloorRoomCardBookingTimeRange)(primary).trim();
      currentBookingDisplay = formatResourceWallNextBookingGuestLine(
        row,
        primary
      ).trim();
      if (!isAvailable) {
        statusPack = applyStatusFromPrimaryBooking(primary, nowMs);
      } else {
        statusPack = {
          status: "available",
          remainingMin: void 0,
          startsInMin: void 0,
          lateMin: void 0,
          sessionStartMs: void 0,
          sessionEndMs: void 0
        };
      }
      if (slot.next && !currentNotStarted) {
        nextList = [buildNextListEntry(row, slot.next, nowMs)];
      }
      if (currentNotStarted) {
        wallUseNextLabelHideNextRow = true;
      }
    }
  } else {
    const primary = (0, import_floorRoomCardUtils.getFloorRoomCardPrimaryBooking)(roomData);
    const currentTimeFromPrimary = primary ? (0, import_floorRoomCardUtils.formatFloorRoomCardBookingTimeRange)(primary).trim() : "";
    const start = String(row.time ?? "").trim();
    const end = String(row.timeEnd ?? "").trim();
    const currentTimeFallback = start && end ? `${start.replace(/ .*/, "")} - ${end.replace(/ .*/, "")}` : start || "";
    currentTime = currentTimeFromPrimary || currentTimeFallback;
    if (!isAvailable && primary) {
      statusPack = applyStatusFromPrimaryBooking(primary, nowMs);
    }
    currentBookingDisplay = isAvailable ? "Available to Book" : formatResourceWallContactLine(row);
    nextList = void 0;
  }
  if (!currentTime && bookings.length === 0) {
    const fbStart = String(row.time ?? "").trim();
    const fbEnd = String(row.timeEnd ?? "").trim();
    currentTime = fbStart && fbEnd ? `${fbStart.replace(/ .*/, "")} - ${fbEnd.replace(/ .*/, "")}` : fbStart || "";
  }
  return {
    resourceId: String(row.id),
    resource: row.code || row.main_field || String(row.id),
    area: row.area,
    status: statusPack.status,
    currentTime,
    currentBooking: currentBookingDisplay,
    remainingMin: statusPack.remainingMin,
    startsInMin: statusPack.startsInMin,
    lateMin: statusPack.lateMin,
    sessionStartMs: statusPack.sessionStartMs,
    sessionEndMs: statusPack.sessionEndMs,
    nextList,
    ...wallUseNextLabelHideNextRow ? { wallUseNextLabelHideNextRow: true } : {}
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  mapReservationRowsToVenueWallCards
});
