var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellFloorMapLayout/hooks/useDraftConfigHistory.ts
var useDraftConfigHistory_exports = {};
__export(useDraftConfigHistory_exports, {
  useDraftConfigHistory: () => useDraftConfigHistory
});
module.exports = __toCommonJS(useDraftConfigHistory_exports);
var import_react = require("react");
var HISTORY_LIMIT = 100;
function useDraftConfigHistory() {
  const [state, setState] = (0, import_react.useState)({
    past: [],
    present: null,
    future: []
  });
  const lastGroupRef = (0, import_react.useRef)(null);
  const commit = (0, import_react.useCallback)((next, options) => {
    const now = Date.now();
    const last = lastGroupRef.current;
    const inGroup = !!((options == null ? void 0 : options.groupId) && last && last.id === options.groupId && now - last.at <= (options.mergeWindowMs ?? 600));
    lastGroupRef.current = { id: options == null ? void 0 : options.groupId, at: now };
    setState((cur) => {
      const value = typeof next === "function" ? next(cur.present) : next;
      if (Object.is(value, cur.present)) return cur;
      let nextPast = cur.past;
      if (cur.present != null && !inGroup) {
        nextPast = nextPast.length >= HISTORY_LIMIT ? [...nextPast.slice(1), cur.present] : [...nextPast, cur.present];
      }
      return { past: nextPast, present: value, future: [] };
    });
  }, []);
  const replace = (0, import_react.useCallback)((next) => {
    lastGroupRef.current = null;
    setState({ past: [], present: next, future: [] });
  }, []);
  const undo = (0, import_react.useCallback)(() => {
    lastGroupRef.current = null;
    setState((cur) => {
      if (cur.past.length === 0) return cur;
      const prev = cur.past[cur.past.length - 1];
      const nextFuture = cur.present != null ? [cur.present, ...cur.future] : cur.future;
      return {
        past: cur.past.slice(0, -1),
        present: prev,
        future: nextFuture
      };
    });
  }, []);
  const redo = (0, import_react.useCallback)(() => {
    lastGroupRef.current = null;
    setState((cur) => {
      if (cur.future.length === 0) return cur;
      const next = cur.future[0];
      const nextPast = cur.present != null ? [...cur.past, cur.present] : cur.past;
      return {
        past: nextPast,
        present: next,
        future: cur.future.slice(1)
      };
    });
  }, []);
  return (0, import_react.useMemo)(
    () => ({
      present: state.present,
      commit,
      replace,
      undo,
      redo,
      canUndo: state.past.length > 0,
      canRedo: state.future.length > 0
    }),
    [state, commit, replace, undo, redo]
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useDraftConfigHistory
});
