var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/pisellReservation/floorRoomCard/floorRoomCardUtils.ts
var floorRoomCardUtils_exports = {};
__export(floorRoomCardUtils_exports, {
  displayRoomShowNameFromRow: () => displayRoomShowNameFromRow,
  displayRoomTitleFromRow: () => displayRoomTitleFromRow,
  formatFloorRoomCardDurationMinutes: () => formatFloorRoomCardDurationMinutes,
  getFloorRoomCardArrivalMinutes: () => getFloorRoomCardArrivalMinutes,
  getFloorRoomCardExplicitNextBooking: () => getFloorRoomCardExplicitNextBooking,
  getFloorRoomCardLateTime: () => getFloorRoomCardLateTime,
  getFloorRoomCardNextAfterPrimaryReserved: () => getFloorRoomCardNextAfterPrimaryReserved,
  getFloorRoomCardNextAfterSingleOccupiedBooking: () => getFloorRoomCardNextAfterSingleOccupiedBooking,
  getFloorRoomCardNextNotStartedBooking: () => getFloorRoomCardNextNotStartedBooking,
  getFloorRoomCardOvertime: () => getFloorRoomCardOvertime,
  getFloorRoomCardPrimaryBooking: () => getFloorRoomCardPrimaryBooking,
  getFloorRoomCardProgressPercent: () => getFloorRoomCardProgressPercent,
  getFloorRoomCardReservedRowBodyType: () => getFloorRoomCardReservedRowBodyType,
  getFloorRoomCardType: () => getFloorRoomCardType,
  isFloorRoomCardBookingInProgress: () => isFloorRoomCardBookingInProgress,
  isFloorRoomCardBookingNotStarted: () => isFloorRoomCardBookingNotStarted,
  maskPisellReservationPhoneTail: () => maskPisellReservationPhoneTail,
  reorderBookingsAllReservedChronological: () => reorderBookingsAllReservedChronological,
  reorderBookingsForSingleOccupiedPrimary: () => reorderBookingsForSingleOccupiedPrimary,
  resolveFloorRoomCardProgressPercent: () => resolveFloorRoomCardProgressPercent,
  resolveFloorRoomCardReservedCountdownMinutes: () => resolveFloorRoomCardReservedCountdownMinutes,
  resolveFloorRoomCardReservedCountdownMinutesForBooking: () => resolveFloorRoomCardReservedCountdownMinutesForBooking
});
module.exports = __toCommonJS(floorRoomCardUtils_exports);
var import_dayjs = __toESM(require("dayjs"));
function normalizeHmsForDayjs(time) {
  const t = time.trim();
  const parts = t.split(":").map((p) => p.trim());
  if (parts.length >= 2) {
    const h = String(parts[0]).padStart(2, "0");
    const m = String(parts[1]).padStart(2, "0");
    const s = parts[2] != null ? String(parts[2]).padStart(2, "0") : "00";
    return `${h}:${m}:${s}`;
  }
  return "00:00:00";
}
function bookingStartEndMs(startDate, startTime, endDate, endTime) {
  const s = (0, import_dayjs.default)(`${startDate}T${normalizeHmsForDayjs(startTime)}`).valueOf();
  const e = (0, import_dayjs.default)(`${endDate}T${normalizeHmsForDayjs(endTime)}`).valueOf();
  return { start: s, end: e };
}
function getFloorRoomCardProgressPercent(startDate, startTime, endDate, endTime, nowMs = Date.now()) {
  const { start, end } = bookingStartEndMs(
    startDate,
    startTime,
    endDate,
    endTime
  );
  if (!Number.isFinite(start) || !Number.isFinite(end) || end <= start)
    return 0;
  if (nowMs <= start) return 0;
  if (nowMs >= end) return 100;
  return Math.round((nowMs - start) / (end - start) * 100);
}
function resolveFloorRoomCardProgressPercent(booking, nowMs = Date.now()) {
  const p = booking.progressPercent;
  if (p != null && Number.isFinite(p)) {
    return Math.min(100, Math.max(0, Math.round(p)));
  }
  return getFloorRoomCardProgressPercent(
    booking.start_date,
    booking.start_time,
    booking.end_date,
    booking.end_time,
    nowMs
  );
}
function formatFloorRoomCardDurationMinutes(totalMin) {
  if (totalMin <= 0) return "0m";
  const h = Math.floor(totalMin / 60);
  const m = totalMin % 60;
  if (h > 0) return m > 0 ? `${h}h ${m}m` : `${h}h`;
  return `${m}m`;
}
function getFloorRoomCardOvertime(endDate, endTime, nowMs = Date.now()) {
  const { end } = bookingStartEndMs(endDate, endTime, endDate, endTime);
  if (!Number.isFinite(end)) return null;
  const diffMin = Math.floor((nowMs - end) / 6e4);
  if (diffMin <= 0) return null;
  return formatFloorRoomCardDurationMinutes(diffMin);
}
function getFloorRoomCardLateTime(startDate, startTime, nowMs = Date.now()) {
  const { start } = bookingStartEndMs(
    startDate,
    startTime,
    startDate,
    startTime
  );
  if (!Number.isFinite(start)) return null;
  const diffMin = Math.floor((nowMs - start) / 6e4);
  if (diffMin <= 0) return null;
  return formatFloorRoomCardDurationMinutes(diffMin);
}
function getFloorRoomCardArrivalMinutes(room, nowMs = Date.now()) {
  const b = room.bookings[0];
  if (!b || room.status !== "reserved") return null;
  const { start } = bookingStartEndMs(
    b.start_date,
    b.start_time,
    b.end_date,
    b.end_time
  );
  if (!Number.isFinite(start)) return null;
  const diffMin = Math.floor((start - nowMs) / 6e4);
  if (diffMin < 0) return null;
  return diffMin;
}
function resolveFloorRoomCardReservedCountdownMinutes(room, nowMs = Date.now()) {
  const b = room.bookings[0];
  if (!b || room.status !== "reserved") return null;
  const r = b.remainingReserveTime;
  if (r != null && Number.isFinite(r)) {
    const n = Math.max(0, Math.round(r));
    if (n > 0) return n;
  }
  return getFloorRoomCardArrivalMinutes(room, nowMs);
}
function resolveFloorRoomCardReservedCountdownMinutesForBooking(booking, nowMs = Date.now()) {
  const r = booking.remainingReserveTime;
  if (r != null && Number.isFinite(r)) {
    const n = Math.max(0, Math.round(r));
    if (n > 0) return n;
  }
  const { start } = bookingStartEndMs(
    booking.start_date,
    booking.start_time,
    booking.end_date,
    booking.end_time
  );
  if (!Number.isFinite(start)) return null;
  const diffMin = Math.floor((start - nowMs) / 6e4);
  if (diffMin < 0) return null;
  return diffMin;
}
function getFloorRoomCardReservedRowBodyType(booking, _nowMs = Date.now()) {
  if (booking.lateTime != null && Number.isFinite(booking.lateTime) && booking.lateTime > 0) {
    return "late";
  }
  return "not_arrived";
}
function isFloorRoomCardBookingInProgress(booking, nowMs = Date.now()) {
  const { start, end } = bookingStartEndMs(
    booking.start_date,
    booking.start_time,
    booking.end_date,
    booking.end_time
  );
  if (!Number.isFinite(start) || !Number.isFinite(end) || end <= start)
    return false;
  return nowMs >= start && nowMs < end;
}
function isFloorRoomCardBookingNotStarted(booking, nowMs = Date.now()) {
  const { start } = bookingStartEndMs(
    booking.start_date,
    booking.start_time,
    booking.end_date,
    booking.end_time
  );
  if (!Number.isFinite(start)) return false;
  return nowMs < start;
}
function getFloorRoomCardNextNotStartedBooking(data, nowMs = Date.now()) {
  const list = data.bookings;
  if (list.length < 2) return null;
  if (!isFloorRoomCardBookingInProgress(list[0], nowMs)) return null;
  for (let i = 1; i < list.length; i++) {
    if (isFloorRoomCardBookingNotStarted(list[i], nowMs)) return list[i];
  }
  return null;
}
function sortFloorRoomCardBookingsByStart(list) {
  return [...list].sort((a, b) => {
    const { start: as } = bookingStartEndMs(
      a.start_date,
      a.start_time,
      a.end_date,
      a.end_time
    );
    const { start: bs } = bookingStartEndMs(
      b.start_date,
      b.start_time,
      b.end_date,
      b.end_time
    );
    return as - bs;
  });
}
function reorderBookingsForSingleOccupiedPrimary(list) {
  if (list.length <= 1) return list;
  const occ = list.filter((b) => b.hostBookingStatus === "occupied");
  if (occ.length !== 1) return list;
  const sorted = sortFloorRoomCardBookingsByStart(list);
  const occIdx = sorted.findIndex((b) => b.hostBookingStatus === "occupied");
  if (occIdx < 0) return list;
  const primary = sorted[occIdx];
  const after = sorted.slice(occIdx + 1);
  const before = sorted.slice(0, occIdx);
  return [primary, ...after, ...before];
}
function getFloorRoomCardNextAfterSingleOccupiedBooking(data) {
  const list = data.bookings;
  if (list.length < 2) return null;
  if (list.filter((b) => b.hostBookingStatus === "occupied").length !== 1) {
    return null;
  }
  const sorted = sortFloorRoomCardBookingsByStart(list);
  const i = sorted.findIndex((b) => b.hostBookingStatus === "occupied");
  if (i < 0 || i >= sorted.length - 1) return null;
  return sorted[i + 1];
}
function getFloorRoomCardNextAfterPrimaryReserved(data) {
  if (data.status !== "reserved" || data.bookings.length < 2) return null;
  if (data.bookings.some((b) => b.hostBookingStatus === "occupied")) {
    return null;
  }
  return data.bookings[1] ?? null;
}
function getFloorRoomCardExplicitNextBooking(data) {
  const marked = data.bookings.filter((b) => b.isNext === true);
  if (marked.length === 0) return null;
  return sortFloorRoomCardBookingsByStart(marked)[0] ?? null;
}
function getFloorRoomCardPrimaryBooking(data) {
  const non = data.bookings.find((b) => b.isNext !== true);
  return non ?? data.bookings[0];
}
function reorderBookingsAllReservedChronological(list) {
  if (list.length <= 1) return list;
  if (list.some((b) => b.hostBookingStatus === "occupied")) return list;
  if (!list.every(
    (b) => b.hostBookingStatus == null || b.hostBookingStatus === "reserved"
  )) {
    return list;
  }
  return sortFloorRoomCardBookingsByStart(list);
}
function getFloorRoomCardType(room) {
  if (room.status === "locked") return "locked";
  if (room.status === "empty") return "empty";
  const n = room.bookings.length;
  const occCount = room.bookings.filter(
    (b) => b.hostBookingStatus === "occupied"
  ).length;
  if (n > 1) {
    if (room.status === "reserved" && occCount === 0) {
      const sorted = reorderBookingsAllReservedChronological([
        ...room.bookings
      ]);
      const mainNonNext = sorted.filter((b) => b.isNext !== true);
      const nextMarked = sorted.filter((b) => b.isNext === true);
      const isSingleMainWithExplicitNext = sorted.length === 2 && mainNonNext.length === 1 && nextMarked.length === 1;
      if (!isSingleMainWithExplicitNext) {
        return "multi_reserved";
      }
    }
    const withHost = room.bookings.filter((b) => b.hostBookingStatus != null);
    if (withHost.length > 0) {
      if (occCount >= 2) {
        return "multi_occupied";
      }
      if (occCount === 1 && room.status === "occupied") {
        const s = room.sub_status;
        if (s === "payed" || s === "payment_required" || s === "timeout")
          return s;
        return "payed";
      }
      if (withHost.every((b) => b.hostBookingStatus === "reserved")) {
        const s = room.sub_status;
        if (s === "not_arrived" || s === "late") return s;
        return "not_arrived";
      }
    }
    if (room.status === "occupied") {
      const s = room.sub_status;
      if (s === "payed" || s === "payment_required" || s === "timeout")
        return s;
      return "payed";
    }
    if (room.status === "reserved") {
      const s = room.sub_status;
      if (s === "not_arrived" || s === "late") return s;
      return "not_arrived";
    }
  }
  if (room.status === "occupied") {
    const s = room.sub_status;
    if (s === "payed" || s === "payment_required" || s === "timeout") return s;
    return "payed";
  }
  if (room.status === "reserved") {
    const s = room.sub_status;
    if (s === "not_arrived" || s === "late") return s;
    return "not_arrived";
  }
  return "empty";
}
function maskPisellReservationPhoneTail(phone) {
  return phone.length >= 4 ? `***${phone.slice(-4)}` : null;
}
function displayRoomTitleFromRow(row, sceneElementName) {
  var _a, _b;
  const rawMain = row.main_field;
  if (typeof rawMain === "string" && rawMain.trim()) {
    const first = (_a = rawMain.split(" ## ")[0]) == null ? void 0 : _a.trim();
    if (first) return first;
  }
  const c = (_b = row.code) == null ? void 0 : _b.trim();
  if (c) return c;
  const n = sceneElementName == null ? void 0 : sceneElementName.trim();
  if (n) return n;
  return "";
}
function displayRoomShowNameFromRow(row, sceneElementName) {
  const rawMain = row.main_field;
  if (typeof rawMain === "string" && rawMain.trim()) {
    const parts = rawMain.split(" ## ").map((p) => p.trim()).filter((p) => p.length > 0);
    if (parts.length >= 2) return parts[1];
    if (parts.length === 1) return parts[0];
  }
  return displayRoomTitleFromRow(row, sceneElementName);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  displayRoomShowNameFromRow,
  displayRoomTitleFromRow,
  formatFloorRoomCardDurationMinutes,
  getFloorRoomCardArrivalMinutes,
  getFloorRoomCardExplicitNextBooking,
  getFloorRoomCardLateTime,
  getFloorRoomCardNextAfterPrimaryReserved,
  getFloorRoomCardNextAfterSingleOccupiedBooking,
  getFloorRoomCardNextNotStartedBooking,
  getFloorRoomCardOvertime,
  getFloorRoomCardPrimaryBooking,
  getFloorRoomCardProgressPercent,
  getFloorRoomCardReservedRowBodyType,
  getFloorRoomCardType,
  isFloorRoomCardBookingInProgress,
  isFloorRoomCardBookingNotStarted,
  maskPisellReservationPhoneTail,
  reorderBookingsAllReservedChronological,
  reorderBookingsForSingleOccupiedPrimary,
  resolveFloorRoomCardProgressPercent,
  resolveFloorRoomCardReservedCountdownMinutes,
  resolveFloorRoomCardReservedCountdownMinutesForBooking
});
