function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
/**
 * 平面图草稿历史栈：past/present/future + commit/replace/undo/redo
 *
 * 设计：
 * - `commit(value, opts?)`：写入新值并入栈（覆盖 future）。同一 `groupId` 在
 *   `mergeWindowMs` 内的多次提交会合并为同一条历史，避免高频微调污染栈。
 *   实际项目里 `EditableItemLayer` 只在 mouseup 时回调一次，因此默认场景下不会
 *   触发合并；保留 group 能力是为后续如「输入框边输入边写草稿」等连续操作准备。
 * - `replace(value)`：直接重置 present 并清空历史；用于上游 props 同步落地，避免
 *   把"上游回写"也变成可撤销项。
 * - `undo` / `redo`：在 past/future 栈内迁移 present。
 */
import { useCallback, useMemo, useRef, useState } from 'react';
/** 历史最大长度，超出从最旧端裁剪 */
var HISTORY_LIMIT = 100;
export function useDraftConfigHistory() {
  var _useState = useState({
      past: [],
      present: null,
      future: []
    }),
    _useState2 = _slicedToArray(_useState, 2),
    state = _useState2[0],
    setState = _useState2[1];
  var lastGroupRef = useRef(null);
  var commit = useCallback(function (next, options) {
    var _options$mergeWindowM;
    var now = Date.now();
    var last = lastGroupRef.current;
    var inGroup = !!(options !== null && options !== void 0 && options.groupId && last && last.id === options.groupId && now - last.at <= ((_options$mergeWindowM = options.mergeWindowMs) !== null && _options$mergeWindowM !== void 0 ? _options$mergeWindowM : 600));
    lastGroupRef.current = {
      id: options === null || options === void 0 ? void 0 : options.groupId,
      at: now
    };
    setState(function (cur) {
      var value = typeof next === 'function' ? next(cur.present) : next;
      if (Object.is(value, cur.present)) return cur;
      var nextPast = cur.past;
      if (cur.present != null && !inGroup) {
        nextPast = nextPast.length >= HISTORY_LIMIT ? [].concat(_toConsumableArray(nextPast.slice(1)), [cur.present]) : [].concat(_toConsumableArray(nextPast), [cur.present]);
      }
      return {
        past: nextPast,
        present: value,
        future: []
      };
    });
  }, []);
  var replace = useCallback(function (next) {
    lastGroupRef.current = null;
    setState({
      past: [],
      present: next,
      future: []
    });
  }, []);
  var undo = useCallback(function () {
    lastGroupRef.current = null;
    setState(function (cur) {
      if (cur.past.length === 0) return cur;
      var prev = cur.past[cur.past.length - 1];
      var nextFuture = cur.present != null ? [cur.present].concat(_toConsumableArray(cur.future)) : cur.future;
      return {
        past: cur.past.slice(0, -1),
        present: prev,
        future: nextFuture
      };
    });
  }, []);
  var redo = useCallback(function () {
    lastGroupRef.current = null;
    setState(function (cur) {
      if (cur.future.length === 0) return cur;
      var next = cur.future[0];
      var nextPast = cur.present != null ? [].concat(_toConsumableArray(cur.past), [cur.present]) : cur.past;
      return {
        past: nextPast,
        present: next,
        future: cur.future.slice(1)
      };
    });
  }, []);
  return useMemo(function () {
    return {
      present: state.present,
      commit: commit,
      replace: replace,
      undo: undo,
      redo: redo,
      canUndo: state.past.length > 0,
      canRedo: state.future.length > 0
    };
  }, [state, commit, replace, undo, redo]);
}