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/useReservationWallClockFollow.ts
var useReservationWallClockFollow_exports = {};
__export(useReservationWallClockFollow_exports, {
  useReservationWallClockFollow: () => useReservationWallClockFollow
});
module.exports = __toCommonJS(useReservationWallClockFollow_exports);
var import_react = require("react");
var import_dayjs = __toESM(require("dayjs"));
function useReservationWallClockFollow(scheduleValueProp, setInternalSchedule, flushCommittedSchedule, onScheduleChange, wallClockFollowLocked = false) {
  const [followWallClock, setFollowWallClockInternal] = (0, import_react.useState)(true);
  const onScheduleChangeRef = (0, import_react.useRef)(onScheduleChange);
  const scheduleValuePropRef = (0, import_react.useRef)(scheduleValueProp);
  scheduleValuePropRef.current = scheduleValueProp;
  const scheduleControlled = scheduleValueProp !== void 0;
  const followWallClockEffective = wallClockFollowLocked || followWallClock;
  (0, import_react.useEffect)(() => {
    if (wallClockFollowLocked) setFollowWallClockInternal(true);
  }, [wallClockFollowLocked]);
  (0, import_react.useEffect)(() => {
    onScheduleChangeRef.current = onScheduleChange;
  });
  (0, import_react.useEffect)(() => {
    if (!followWallClockEffective) return;
    let timeoutId = null;
    let cancelled = false;
    const msUntilNextMinuteBoundary = () => {
      const now = (0, import_dayjs.default)();
      return Math.max(0, now.startOf("minute").add(1, "minute").diff(now));
    };
    const tick = () => {
      var _a;
      const now = (0, import_dayjs.default)().startOf("minute");
      const next = {
        date: now.startOf("day"),
        at: now
      };
      if (scheduleValuePropRef.current === void 0) {
        setInternalSchedule(next);
      }
      (_a = onScheduleChangeRef.current) == null ? void 0 : _a.call(onScheduleChangeRef, next);
      flushCommittedSchedule(next);
    };
    const scheduleNextBoundary = () => {
      if (cancelled) return;
      timeoutId = window.setTimeout(() => {
        if (cancelled) return;
        tick();
        scheduleNextBoundary();
      }, msUntilNextMinuteBoundary());
    };
    tick();
    scheduleNextBoundary();
    return () => {
      cancelled = true;
      if (timeoutId != null) window.clearTimeout(timeoutId);
    };
  }, [followWallClockEffective, flushCommittedSchedule, scheduleControlled]);
  const setFollowWallClock = (0, import_react.useCallback)(
    (value) => {
      if (wallClockFollowLocked) return;
      setFollowWallClockInternal(value);
    },
    [wallClockFollowLocked]
  );
  const onFollowWallClockToggle = (0, import_react.useCallback)(() => {
    if (wallClockFollowLocked) return;
    setFollowWallClockInternal((v) => !v);
  }, [wallClockFollowLocked]);
  const onTimeNavigatorUserInteraction = (0, import_react.useCallback)(() => {
    if (wallClockFollowLocked) return;
    setFollowWallClockInternal(false);
  }, [wallClockFollowLocked]);
  return {
    followWallClock: followWallClockEffective,
    setFollowWallClock,
    onFollowWallClockToggle,
    onTimeNavigatorUserInteraction
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useReservationWallClockFollow
});
