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/components/bookingDetailModal/bookingDetailResourceBookingItem.ts
var bookingDetailResourceBookingItem_exports = {};
__export(bookingDetailResourceBookingItem_exports, {
  normalizeResourceBookingItemLists: () => normalizeResourceBookingItemLists,
  useResourceBookingItem: () => useResourceBookingItem
});
module.exports = __toCommonJS(bookingDetailResourceBookingItem_exports);
var import_react = require("react");
var import_dayjs = __toESM(require("dayjs"));
var import_bookingDetailHostActions = require("./bookingDetailHostActions");
var import_bookingDetailModalConstants = require("./bookingDetailModalConstants");
function normalizeResourceBookingItemLists(raw) {
  if (raw == null || typeof raw !== "object") {
    return import_bookingDetailModalConstants.EMPTY_RESOURCE_BOOKING_ITEM_LISTS;
  }
  const r = raw;
  const arr = (v) => Array.isArray(v) ? v : [];
  return {
    bookings: arr(r.bookings),
    completedBookings: arr(r.completedBookings),
    unpaidBookings: arr(r.unpaidBookings)
  };
}
function useResourceBookingItem(activeTab, options) {
  const {
    open,
    raw,
    tableRow,
    floorDayBookingList,
    currentTime,
    followWallClock,
    pisellos
  } = options;
  const [resourceBookingItem, setResourceBookingItem] = (0, import_react.useState)(null);
  const [resourceBookingItemLoading, setResourceBookingItemLoading] = (0, import_react.useState)(false);
  const resourceBookingItemReqIdRef = (0, import_react.useRef)(0);
  (0, import_react.useEffect)(() => {
    if (!open || raw == null) {
      setResourceBookingItem(null);
      setResourceBookingItemLoading(false);
      resourceBookingItemReqIdRef.current += 1;
    }
  }, [open, raw]);
  (0, import_react.useEffect)(() => {
    if (!open || raw == null) return;
    const getModule = pisellos == null ? void 0 : pisellos.getModule;
    if (typeof getModule !== "function") return;
    let salesModule;
    try {
      salesModule = getModule.call(pisellos, "sales");
    } catch {
      return;
    }
    const fn = salesModule == null ? void 0 : salesModule.getResourceBookingItem;
    if (typeof fn !== "function") return;
    const resourceId = (0, import_bookingDetailHostActions.resolveResourceIdFromRow)(tableRow);
    if (resourceId == null) return;
    const fmt = "YYYY-MM-DD HH:mm";
    const currentTimeStr = currentTime ? (0, import_dayjs.default)(currentTime).format(fmt) : (0, import_dayjs.default)().format(fmt);
    const deviceTimeStr = followWallClock ? currentTimeStr : (0, import_dayjs.default)().format(fmt);
    const bookingList = Array.isArray(floorDayBookingList) ? floorDayBookingList : [];
    const reqId = ++resourceBookingItemReqIdRef.current;
    setResourceBookingItemLoading(true);
    const callParams = {
      resourceId,
      currentTime: currentTimeStr,
      bookingList,
      deviceTime: deviceTimeStr
    };
    const apply = (result) => {
      if (resourceBookingItemReqIdRef.current !== reqId) return;
      setResourceBookingItem(normalizeResourceBookingItemLists(result));
      setResourceBookingItemLoading(false);
    };
    const fail = () => {
      if (resourceBookingItemReqIdRef.current !== reqId) return;
      setResourceBookingItem(import_bookingDetailModalConstants.EMPTY_RESOURCE_BOOKING_ITEM_LISTS);
      setResourceBookingItemLoading(false);
    };
    try {
      const result = fn.call(salesModule, callParams);
      if (result != null && typeof result.then === "function") {
        result.then(apply).catch(fail);
      } else {
        apply(result);
      }
    } catch {
      fail();
    }
  }, [
    open,
    raw,
    tableRow,
    floorDayBookingList,
    currentTime,
    followWallClock,
    pisellos
  ]);
  const activeTabBookings = (0, import_react.useMemo)(() => {
    if (!resourceBookingItem) return [];
    const arr = activeTab === "current-occupied" ? resourceBookingItem.bookings : activeTab === "ended" ? resourceBookingItem.completedBookings : resourceBookingItem.unpaidBookings;
    return arr.filter(
      (x) => x != null && typeof x === "object"
    );
  }, [resourceBookingItem, activeTab]);
  return {
    resourceBookingItem,
    resourceBookingItemLoading,
    activeTabBookings
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  normalizeResourceBookingItemLists,
  useResourceBookingItem
});
