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/useReservationScheduleDebounced.ts
var useReservationScheduleDebounced_exports = {};
__export(useReservationScheduleDebounced_exports, {
  useReservationScheduleDebounced: () => useReservationScheduleDebounced
});
module.exports = __toCommonJS(useReservationScheduleDebounced_exports);
var import_react = require("react");
var import_dayjs = __toESM(require("dayjs"));
var import_reservationConstants = require("../reservationConstants");
function defaultScheduleValue() {
  return {
    date: (0, import_dayjs.default)().startOf("day"),
    at: (0, import_dayjs.default)()
  };
}
function useReservationScheduleDebounced(options) {
  const { scheduleValueProp } = options;
  const [internalSchedule, setInternalSchedule] = (0, import_react.useState)(
    defaultScheduleValue
  );
  const schedule = scheduleValueProp ?? internalSchedule;
  const scheduleRef = (0, import_react.useRef)(schedule);
  scheduleRef.current = schedule;
  const [debouncedSchedule, setDebouncedSchedule] = (0, import_react.useState)(
    () => scheduleValueProp ?? defaultScheduleValue()
  );
  const scheduleCommitTimerRef = (0, import_react.useRef)(null);
  const flushCommittedSchedule = (0, import_react.useCallback)(
    (next) => {
      if (scheduleCommitTimerRef.current != null) {
        window.clearTimeout(scheduleCommitTimerRef.current);
        scheduleCommitTimerRef.current = null;
      }
      setDebouncedSchedule(next);
    },
    []
  );
  const queueCommittedSchedule = (0, import_react.useCallback)(
    (next) => {
      if (scheduleCommitTimerRef.current != null) {
        window.clearTimeout(scheduleCommitTimerRef.current);
      }
      scheduleCommitTimerRef.current = window.setTimeout(() => {
        scheduleCommitTimerRef.current = null;
        setDebouncedSchedule(next);
      }, import_reservationConstants.RESERVATION_SCHEDULE_DEBOUNCE_MS);
    },
    []
  );
  (0, import_react.useEffect)(
    () => () => {
      if (scheduleCommitTimerRef.current != null) {
        window.clearTimeout(scheduleCommitTimerRef.current);
      }
    },
    []
  );
  const scheduleAtMs = schedule.at != null && (0, import_dayjs.default)(schedule.at).isValid() ? (0, import_dayjs.default)(schedule.at).valueOf() : null;
  const scheduleDayMs = (0, import_dayjs.default)(schedule.date).startOf("day").valueOf();
  (0, import_react.useEffect)(() => {
    if (scheduleValueProp === void 0) return;
    queueCommittedSchedule(scheduleRef.current);
  }, [scheduleAtMs, scheduleDayMs, scheduleValueProp, queueCommittedSchedule]);
  return {
    internalSchedule,
    setInternalSchedule,
    schedule,
    debouncedSchedule,
    scheduleRef,
    flushCommittedSchedule,
    queueCommittedSchedule
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useReservationScheduleDebounced
});
