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/components/venueBooking/hooks.ts
var hooks_exports = {};
__export(hooks_exports, {
  VENUE_BOOKING_STEPS: () => VENUE_BOOKING_STEPS,
  VENUE_PRODUCT_IDS: () => VENUE_PRODUCT_IDS,
  contextUtils: () => contextUtils,
  createVenueBookingViewModel: () => createVenueBookingViewModel,
  fetchResourceAvailability: () => fetchResourceAvailability,
  getAllSelectedSlots: () => getAllSelectedSlots,
  getDateRangeSummary: () => getDateRangeSummary,
  getOtherParams: () => getOtherParams,
  getSelectedDateKeySet: () => getSelectedDateKeySet,
  getTimeSlotGrid: () => getTimeSlotGrid,
  isSlotDisabledBySelection: () => isSlotDisabledBySelection,
  isSlotSelected: () => isSlotSelected,
  loadVenueProducts: () => loadVenueProducts,
  normalizeEntryContext: () => normalizeEntryContext,
  safeRunModuleAction: () => safeRunModuleAction,
  toggleSlotAction: () => toggleSlotAction,
  useVenueBookingLogin: () => useVenueBookingLogin,
  useVenueBookingModule: () => useVenueBookingModule,
  useVenueBookingSymbol: () => useVenueBookingSymbol
});
module.exports = __toCommonJS(hooks_exports);
var import_react = require("react");
var import_useEngineContext = require("../../hooks/useEngineContext");
var import_useEngineContext2 = __toESM(require("../../hooks/useEngineContext"));
var import_toShoppingCartProducts = require("../scanOrder/utils/toShoppingCartProducts");
var VENUE_BOOKING_STEPS = ["select-venue", "addons", "checkout"];
var VENUE_PRODUCT_IDS = [61947, 61948];
function useVenueBookingModule() {
  var _a;
  const pisellOS = (0, import_useEngineContext.usePisellOS)();
  const moduleRef = (0, import_react.useRef)(null);
  if (!moduleRef.current) {
    moduleRef.current = ((_a = pisellOS == null ? void 0 : pisellOS.getModule) == null ? void 0 : _a.call(pisellOS, "venueBooking")) ?? null;
  }
  return moduleRef.current;
}
function useVenueBookingSymbol() {
  var _a;
  const utils = contextUtils();
  return String(((_a = utils == null ? void 0 : utils.getSymbolic) == null ? void 0 : _a.call(utils)) || "$");
}
function contextUtils() {
  var _a;
  const context = (0, import_useEngineContext2.default)();
  return ((_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils) || {};
}
function normalizeEntryContext(props) {
  return {
    biz: (props == null ? void 0 : props.biz) || "",
    resource_id: props == null ? void 0 : props.resource_id
  };
}
function callModuleGetter(module2, methodName, defaultValue) {
  if (typeof (module2 == null ? void 0 : module2[methodName]) !== "function") return defaultValue;
  const result = module2[methodName]();
  if (result && typeof (result == null ? void 0 : result.then) === "function") return defaultValue;
  return result ?? defaultValue;
}
async function safeRunModuleAction(module2, actionName, ...args) {
  if (!module2) throw new Error("venueBooking module not found");
  if (typeof (module2 == null ? void 0 : module2[actionName]) !== "function") {
    throw new Error(`venueBooking action "${actionName}" not found`);
  }
  return module2[actionName](...args);
}
async function loadVenueProducts(module2, productIds) {
  return safeRunModuleAction(module2, "loadAllProducts", { productIds });
}
async function fetchResourceAvailability(module2, startDate, endDate) {
  return safeRunModuleAction(module2, "fetchResourceAvailability", { startDate, endDate });
}
function getDateRangeSummary(module2, startDate, endDate) {
  if (!module2 || typeof module2.getDateRangeSummary !== "function") return [];
  return module2.getDateRangeSummary({ startDate, endDate }) || [];
}
function getTimeSlotGrid(module2, date) {
  if (!module2 || typeof module2.getTimeSlotGrid !== "function") return null;
  return module2.getTimeSlotGrid(date) || null;
}
async function toggleSlotAction(module2, params) {
  return safeRunModuleAction(module2, "toggleSlot", params);
}
function isSlotSelected(module2, resourceId, startTime, productId) {
  if (!module2 || typeof module2.isSlotSelected !== "function") return false;
  return !!module2.isSlotSelected(resourceId, startTime, productId);
}
function isSlotDisabledBySelection(module2, params) {
  if (!module2 || typeof module2.isSlotDisabledBySelection !== "function") return false;
  return !!module2.isSlotDisabledBySelection(params);
}
function getAllSelectedSlots(module2) {
  if (!module2 || typeof module2.getAllSelectedSlots !== "function") return [];
  return module2.getAllSelectedSlots() || [];
}
function getOtherParams(module2) {
  if (!module2 || typeof module2.getOtherParams !== "function") return {};
  return module2.getOtherParams() || {};
}
function parseDateKeyFromDateTime(value) {
  if (typeof value !== "string") return null;
  const matched = value.trim().match(/^(\d{4}-\d{2}-\d{2})/);
  if (!matched) return null;
  return matched[1];
}
function appendSlotDateKey(target, slot) {
  const candidates = [
    slot == null ? void 0 : slot.startTime,
    slot == null ? void 0 : slot.start_time,
    slot == null ? void 0 : slot.startDate,
    slot == null ? void 0 : slot.start_date
  ];
  for (const candidate of candidates) {
    const dateKey = parseDateKeyFromDateTime(candidate);
    if (!dateKey) continue;
    target.add(dateKey);
    return;
  }
}
function getSelectedDateKeySet(module2) {
  const selectedDateKeySet = /* @__PURE__ */ new Set();
  if (!module2 || typeof module2.getAllSelectedSlots !== "function") return selectedDateKeySet;
  const selectedSlots = module2.getAllSelectedSlots();
  if (!selectedSlots) return selectedDateKeySet;
  if (selectedSlots instanceof Map) {
    selectedSlots.forEach((slots) => {
      if (!Array.isArray(slots)) return;
      slots.forEach((slot) => appendSlotDateKey(selectedDateKeySet, slot));
    });
    return selectedDateKeySet;
  }
  if (!Array.isArray(selectedSlots)) return selectedDateKeySet;
  selectedSlots.forEach((item) => {
    if (Array.isArray(item)) {
      item.forEach((slot) => appendSlotDateKey(selectedDateKeySet, slot));
      return;
    }
    if (Array.isArray(item == null ? void 0 : item.slots)) {
      item.slots.forEach((slot) => appendSlotDateKey(selectedDateKeySet, slot));
      return;
    }
    appendSlotDateKey(selectedDateKeySet, item);
  });
  return selectedDateKeySet;
}
function createVenueBookingViewModel(module2) {
  const orderProducts = callModuleGetter(module2, "getOrderProducts", []);
  const summaryByGetter = callModuleGetter(module2, "getSummary", null);
  const tempOrder = callModuleGetter(module2, "getTempOrder", null);
  const amountSymbol = (tempOrder == null ? void 0 : tempOrder.currency_symbol) || "$";
  const cartItems = (0, import_toShoppingCartProducts.toShoppingCartProducts)({
    orderProducts: Array.isArray(orderProducts) ? orderProducts : [],
    amountSymbol
  });
  const summaryFromGetter = summaryByGetter && typeof summaryByGetter === "object" ? summaryByGetter : null;
  const summary = summaryFromGetter || (tempOrder == null ? void 0 : tempOrder.summary) || {};
  return {
    cartItems: Array.isArray(cartItems) ? cartItems : [],
    summary: summary && typeof summary === "object" ? summary : {},
    note: String((tempOrder == null ? void 0 : tempOrder.note) || ""),
    contactsInfo: Array.isArray(tempOrder == null ? void 0 : tempOrder.contacts_info) ? tempOrder.contacts_info : []
  };
}
function useVenueBookingLogin() {
  var _a, _b;
  const utils = contextUtils();
  const isLogin = !!((_b = (_a = utils == null ? void 0 : utils.token) == null ? void 0 : _a.get) == null ? void 0 : _b.call(_a));
  const openLoginModal = (params) => {
    const { callback } = params || {};
    utils.action({
      type: "pisell1.login",
      data: {},
      callback
    });
  };
  return { isLogin, openLoginModal };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  VENUE_BOOKING_STEPS,
  VENUE_PRODUCT_IDS,
  contextUtils,
  createVenueBookingViewModel,
  fetchResourceAvailability,
  getAllSelectedSlots,
  getDateRangeSummary,
  getOtherParams,
  getSelectedDateKeySet,
  getTimeSlotGrid,
  isSlotDisabledBySelection,
  isSlotSelected,
  loadVenueProducts,
  normalizeEntryContext,
  safeRunModuleAction,
  toggleSlotAction,
  useVenueBookingLogin,
  useVenueBookingModule,
  useVenueBookingSymbol
});
