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/hooks/useReservationToolBarMerged.tsx
var useReservationToolBarMerged_exports = {};
__export(useReservationToolBarMerged_exports, {
  useReservationToolBarMerged: () => useReservationToolBarMerged
});
module.exports = __toCommonJS(useReservationToolBarMerged_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_materials = require("@pisell/materials");
var import_reservationDensity = require("../data/reservationDensity");
var import_PisellReservationSubcomponents = require("../components/PisellReservationSubcomponents");
var import_ReservationResourceWallResourceSelect = require("../components/ReservationResourceWallResourceSelect");
var import_reservationConstants = require("../reservationConstants");
var import_buildReservationMobileTableConfig = require("../mobileTable/buildReservationMobileTableConfig");
function useReservationToolBarMerged(p) {
  const {
    toolBarProp,
    bookingToolBar,
    isInternalListData,
    tables,
    schedule,
    handleScheduleChange,
    scheduleStartSlotProp,
    scheduleEndSlotProp,
    schedulePropsProp,
    mergedTimeNavigatorProps,
    followWallClock,
    onFollowWallClockToggle,
    onTimeNavigatorUserInteraction,
    calendarGranularity,
    setCalendarGranularity,
    onNewReservation,
    salesTimelineHighlights,
    floorDayBookingData,
    data,
    effectiveTimelineRange,
    bodyView,
    phoneLayout
  } = p;
  const defaultScheduleEnd = (0, import_react.useMemo)(
    () => /* @__PURE__ */ import_react.default.createElement(
      import_PisellReservationSubcomponents.PisellReservationScheduleEndActions,
      {
        onNewReservation,
        calendarGranularity,
        onCalendarGranularityChange: setCalendarGranularity,
        phoneLayout
      }
    ),
    [onNewReservation, calendarGranularity, setCalendarGranularity, phoneLayout]
  );
  const effectiveScheduleEndSlot = (0, import_react.useMemo)(() => {
    const slotNode = scheduleEndSlotProp ?? defaultScheduleEnd;
    return /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        onClick: (event) => {
          const target = event.target;
          if (!(target == null ? void 0 : target.closest(".pisell-reservation-schedule-end-slots")))
            return;
          event.stopPropagation();
        }
      },
      slotNode
    );
  }, [scheduleEndSlotProp, defaultScheduleEnd]);
  const resourceWallFilterSelectOptions = (0, import_react.useMemo)(() => {
    return tables.map((row) => {
      const id = String(row.id);
      const r = row;
      const code = r.code != null && String(r.code).trim() !== "" ? String(r.code) : "";
      const name = r.name != null && String(r.name).trim() !== "" ? String(r.name) : "";
      return { value: id, label: code || name || id };
    });
  }, [tables]);
  const bookingQuickFilterWithResourceWall = (0, import_react.useMemo)(() => {
    const base = bookingToolBar == null ? void 0 : bookingToolBar.quickFilter;
    if (base === false || base == null) return base;
    if (!isInternalListData) return base;
    const rawList = base.filterList;
    const filterList = Array.isArray(rawList) ? [...rawList] : [];
    const exists = filterList.some(
      (i) => String(
        i.key ?? i.name ?? ""
      ) === import_materials.DEFAULT_RESOURCE_WALL_FILTER_FIELD_KEY
    );
    if (exists) return base;
    return {
      ...typeof base === "object" && base !== null ? base : {},
      filterList: [
        ...filterList,
        {
          type: "custom",
          key: import_materials.DEFAULT_RESOURCE_WALL_FILTER_FIELD_KEY,
          name: import_materials.DEFAULT_RESOURCE_WALL_FILTER_FIELD_KEY,
          label: import_utils.locales.getText(import_reservationConstants.I18N_RESOURCE_WALL_RESOURCES),
          other: {
            component: import_ReservationResourceWallResourceSelect.ReservationResourceWallResourceSelect,
            options: resourceWallFilterSelectOptions
          }
        }
      ]
    };
  }, [bookingToolBar, isInternalListData, resourceWallFilterSelectOptions]);
  const loadReservationDensity = (0, import_react.useCallback)(
    async ({
      referenceDate,
      stepMinutes
    }) => {
      const slotCount = (0, import_reservationDensity.reservationTimelineAxisSlotCount)(
        referenceDate,
        stepMinutes,
        effectiveTimelineRange
      );
      if (salesTimelineHighlights != null && salesTimelineHighlights.length > 0) {
        return (0, import_reservationDensity.fitTimelineHighlightsToSlotCount)(
          salesTimelineHighlights,
          slotCount
        );
      }
      const densityRows = isInternalListData ? floorDayBookingData : data;
      return (0, import_reservationDensity.computeReservationDensityBuckets)(
        densityRows,
        referenceDate,
        stepMinutes
      );
    },
    [
      isInternalListData,
      floorDayBookingData,
      data,
      salesTimelineHighlights,
      effectiveTimelineRange.start,
      effectiveTimelineRange.end
    ]
  );
  const toolBarMerged = (0, import_react.useMemo)(() => {
    const {
      tabs: extraTabs,
      topLeft: topLeftProp,
      topRight: topRightProp,
      bottomLeft: bottomLeftProp,
      quickFilter: toolBarQuickFilterOverride,
      ...restToolbar
    } = toolBarProp ?? {};
    const booking = bookingToolBar ?? {};
    const effectiveQuickFilter = toolBarQuickFilterOverride !== void 0 ? toolBarQuickFilterOverride : bookingQuickFilterWithResourceWall;
    const baseToolbar = {
      ...booking,
      ...restToolbar
    };
    const mobileToolbar = phoneLayout ? bodyView === "grid" || bodyView === "resourceWall" ? (0, import_buildReservationMobileTableConfig.buildReservationMobileTableToolbarConfig)({
      baseToolBar: baseToolbar,
      quickFilterFilterList: effectiveQuickFilter && typeof effectiveQuickFilter === "object" && Array.isArray(
        effectiveQuickFilter.filterList
      ) ? effectiveQuickFilter.filterList : void 0
    }) : (0, import_buildReservationMobileTableConfig.buildReservationPhoneNarrowStackToolbarConfig)(baseToolbar) : baseToolbar;
    const phoneQuickFilter = phoneLayout && effectiveQuickFilter && typeof effectiveQuickFilter === "object" && Array.isArray(
      effectiveQuickFilter.filterList
    ) ? {
      ...effectiveQuickFilter,
      size: "small",
      filterList: (0, import_buildReservationMobileTableConfig.adaptQuickFiltersForMobileToolbar)(
        effectiveQuickFilter.filterList
      )
    } : effectiveQuickFilter;
    const mergedFilter = mobileToolbar.filter;
    const phoneFilter = phoneLayout && mergedFilter && typeof mergedFilter === "object" ? (0, import_buildReservationMobileTableConfig.adaptFilterConfigForMobile)(mergedFilter) : mergedFilter;
    return {
      ...mobileToolbar,
      ...phoneFilter ? { filter: phoneFilter } : {},
      quickFilter: phoneQuickFilter,
      hideBodyViewToggle: phoneLayout && (bodyView === "grid" || bodyView === "resourceWall"),
      tabs: phoneLayout && (bodyView === "grid" || bodyView === "resourceWall") ? null : /* @__PURE__ */ import_react.default.createElement(
        import_PisellReservationSubcomponents.PisellReservationToolbarTabs,
        {
          extraTabs,
          schedule,
          onScheduleChange: handleScheduleChange,
          scheduleProps: schedulePropsProp,
          timeNavigatorProps: mergedTimeNavigatorProps,
          scheduleStartSlot: scheduleStartSlotProp ?? (phoneLayout ? null : /* @__PURE__ */ import_react.default.createElement(import_PisellReservationSubcomponents.ReservationDefaultScheduleStart, null)),
          scheduleEndSlot: effectiveScheduleEndSlot,
          followWallClock,
          onFollowWallClockToggle,
          onTimeNavigatorUserInteraction,
          loadDensity: loadReservationDensity,
          calendarGranularity,
          phoneLayout
        }
      ),
      topLeft: /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, phoneLayout ? /* @__PURE__ */ import_react.default.createElement(import_PisellReservationSubcomponents.ReservationMobileToolbarBodyViewSelect, null) : null, topLeftProp),
      topRight: /* @__PURE__ */ import_react.default.createElement(import_PisellReservationSubcomponents.ReservationGridTopRight, { extra: topRightProp }),
      bottomLeft: bottomLeftProp ?? null
    };
  }, [
    bookingToolBar,
    bookingQuickFilterWithResourceWall,
    toolBarProp,
    schedule,
    handleScheduleChange,
    scheduleStartSlotProp,
    effectiveScheduleEndSlot,
    followWallClock,
    onFollowWallClockToggle,
    onTimeNavigatorUserInteraction,
    mergedTimeNavigatorProps,
    schedulePropsProp,
    loadReservationDensity,
    calendarGranularity,
    phoneLayout,
    bodyView
  ]);
  return { toolBarMerged };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useReservationToolBarMerged
});
