const c = {
  UNDO: "@@redux-undo/UNDO",
  REDO: "@@redux-undo/REDO",
  JUMP_TO_FUTURE: "@@redux-undo/JUMP_TO_FUTURE",
  JUMP_TO_PAST: "@@redux-undo/JUMP_TO_PAST",
  JUMP: "@@redux-undo/JUMP",
  CLEAR_HISTORY: "@@redux-undo/CLEAR_HISTORY"
}, I = {
  undo() {
    return { type: c.UNDO };
  },
  redo() {
    return { type: c.REDO };
  },
  jumpToFuture(t) {
    return { type: c.JUMP_TO_FUTURE, index: t };
  },
  jumpToPast(t) {
    return { type: c.JUMP_TO_PAST, index: t };
  },
  jump(t) {
    return { type: c.JUMP, index: t };
  },
  clearHistory() {
    return { type: c.CLEAR_HISTORY };
  }
};
function S(t, e = []) {
  return Array.isArray(t) ? t : typeof t == "string" ? [t] : e;
}
function P(t) {
  return typeof t.present < "u" && typeof t.future < "u" && typeof t.past < "u" && Array.isArray(t.future) && Array.isArray(t.past);
}
function T(t, e, n, s = null) {
  return {
    past: t,
    present: e,
    future: n,
    group: s,
    _latestUnfiltered: e,
    index: t.length,
    limit: t.length + n.length + 1
  };
}
let U, d;
const m = {
  prevState: "#9E9E9E",
  action: "#03A9F4",
  nextState: "#4CAF50"
};
function R() {
  d = {
    header: [],
    prev: [],
    action: [],
    next: [],
    msgs: []
  };
}
function E() {
  const { header: t, prev: e, next: n, action: s, msgs: l } = d;
  console.group ? (console.groupCollapsed(...t), console.log(...e), console.log(...s), console.log(...n), console.log(...l), console.groupEnd()) : (console.log(...t), console.log(...e), console.log(...s), console.log(...n), console.log(...l));
}
function h(t, e, n) {
  return [
    `%c${t}`,
    `color: ${e}; font-weight: bold`,
    n
  ];
}
function H(t, e) {
  R(), U && (console.group ? (d.header = ["%credux-undo", "font-style: italic", "action", t.type], d.action = h("action", m.action, t), d.prev = h("prev history", m.prevState, e)) : (d.header = ["redux-undo action", t.type], d.action = ["action", t], d.prev = ["prev history", e]));
}
function f(t) {
  U && (console.group ? d.next = h("next history", m.nextState, t) : d.next = ["next history", t], E());
}
function p(...t) {
  U && (d.msgs = d.msgs.concat([...t, `
`]));
}
function v(t) {
  U = t;
}
function x(t, e) {
  const n = T([], t, []);
  return e && (n._latestUnfiltered = null), n;
}
function j(t, e, n, s) {
  const l = t.past.length + 1;
  p("inserting", e), p("new free: ", n - l);
  const { past: u, _latestUnfiltered: o } = t, a = n && n <= l, r = u.slice(a ? 1 : 0), i = o != null ? [
    ...r,
    o
  ] : r;
  return T(i, e, [], s);
}
function O(t, e) {
  if (e < 0 || e >= t.future.length)
    return t;
  const { past: n, future: s, _latestUnfiltered: l } = t, u = [...n, l, ...s.slice(0, e)], o = s[e], a = s.slice(e + 1);
  return T(u, o, a);
}
function A(t, e) {
  if (e < 0 || e >= t.past.length)
    return t;
  const { past: n, future: s, _latestUnfiltered: l } = t, u = n.slice(0, e), o = [...n.slice(e + 1), l, ...s], a = n[e];
  return T(u, a, o);
}
function _(t, e) {
  return e > 0 ? O(t, e - 1) : e < 0 ? A(t, t.past.length + e) : t;
}
function F(t, e) {
  return e.indexOf(t) > -1 ? t : !t;
}
function J(t, e = {}) {
  v(e.debug);
  const n = {
    limit: void 0,
    filter: () => !0,
    groupBy: () => null,
    undoType: c.UNDO,
    redoType: c.REDO,
    jumpToPastType: c.JUMP_TO_PAST,
    jumpToFutureType: c.JUMP_TO_FUTURE,
    jumpType: c.JUMP,
    neverSkipReducer: !1,
    ignoreInitialState: !1,
    syncFilter: !1,
    ...e,
    initTypes: S(e.initTypes, ["@@redux-undo/INIT"]),
    clearHistoryType: S(
      e.clearHistoryType,
      [c.CLEAR_HISTORY]
    )
  }, s = n.neverSkipReducer ? (u, o, ...a) => ({
    ...u,
    present: t(u.present, o, ...a)
  }) : (u) => u;
  let l;
  return (u = l, o = {}, ...a) => {
    H(o, u);
    let r = u;
    if (!l)
      if (p("history is uninitialized"), u === void 0) {
        const g = t(u, { type: "@@redux-undo/CREATE_HISTORY" }, ...a);
        return r = x(
          g,
          n.ignoreInitialState
        ), p("do not set initialState on probe actions"), f(r), r;
      } else
        P(u) ? (r = l = n.ignoreInitialState ? u : T(
          u.past,
          u.present,
          u.future
        ), p(
          "initialHistory initialized: initialState is a history",
          l
        )) : (r = l = x(
          u,
          n.ignoreInitialState
        ), p(
          "initialHistory initialized: initialState is not a history",
          l
        ));
    let i;
    switch (o.type) {
      case void 0:
        return r;
      case n.undoType:
        return i = _(r, -1), p("perform undo"), f(i), s(i, o, ...a);
      case n.redoType:
        return i = _(r, 1), p("perform redo"), f(i), s(i, o, ...a);
      case n.jumpToPastType:
        return i = A(r, o.index), p(`perform jumpToPast to ${o.index}`), f(i), s(i, o, ...a);
      case n.jumpToFutureType:
        return i = O(r, o.index), p(`perform jumpToFuture to ${o.index}`), f(i), s(i, o, ...a);
      case n.jumpType:
        return i = _(r, o.index), p(`perform jump to ${o.index}`), f(i), s(i, o, ...a);
      case F(o.type, n.clearHistoryType):
        return i = x(r.present, n.ignoreInitialState), p("perform clearHistory"), f(i), s(i, o, ...a);
      default:
        if (i = t(
          r.present,
          o,
          ...a
        ), n.initTypes.some((y) => y === o.type))
          return p("reset history due to init action"), f(l), l;
        if (r._latestUnfiltered === i)
          return r;
        if (typeof n.filter == "function" && !n.filter(
          o,
          i,
          r
        )) {
          const y = T(
            r.past,
            i,
            r.future,
            r.group
          );
          return n.syncFilter || (y._latestUnfiltered = r._latestUnfiltered), p("filter ignored action, not storing it in past"), f(y), y;
        }
        const g = n.groupBy(o, i, r);
        if (g != null && g === r.group) {
          const y = T(
            r.past,
            i,
            r.future,
            r.group
          );
          return p("groupBy grouped the action with the previous action"), f(y), y;
        }
        return r = j(r, i, n.limit, g), p("inserted new state into history"), f(r), r;
    }
  };
}
export {
  I as ActionCreators,
  c as ActionTypes,
  J as default,
  P as isHistory,
  T as newHistory,
  S as parseActions
};
//# sourceMappingURL=redux-undo.js.map
