{"version":3,"file":"undoRedo.mjs","sources":["../../../../admin/src/components/DataManager/undoRedo.ts"],"sourcesContent":["import {\n  CaseReducer,\n  createSlice,\n  CreateSliceOptions,\n  Draft,\n  original,\n  PayloadAction,\n  SliceCaseReducers,\n} from '@reduxjs/toolkit';\n\nexport type UndoRedoState<T> = {\n  past: Array<Partial<T>>;\n  future: Array<Partial<T>>;\n  current: T;\n};\n\ntype WrappedUndoRedoReducer<TState, TReducers extends SliceCaseReducers<TState>> = {\n  [K in keyof TReducers]: TReducers[K] extends CaseReducer<TState, infer A>\n    ? CaseReducer<UndoRedoState<TState>, A>\n    : never;\n};\n\ntype UndoRedoReducer<TState, TReducers extends SliceCaseReducers<TState>> = WrappedUndoRedoReducer<\n  TState,\n  TReducers\n> & {\n  undo: CaseReducer<UndoRedoState<TState>>;\n  redo: CaseReducer<UndoRedoState<TState>>;\n  discardAll: CaseReducer<UndoRedoState<TState>>;\n  clearHistory: CaseReducer<UndoRedoState<TState>>;\n};\n\ntype Opts<TState> = {\n  limit?: number;\n  excludeActionsFromHistory?: string[];\n  stateSelector?: (state: Draft<TState> | undefined) => Draft<Partial<TState>>;\n  discard?: (state: Draft<TState>) => void;\n};\n\nconst isCallable = (obj: unknown): obj is (...args: unknown[]) => unknown => {\n  return typeof obj === 'function';\n};\n\nexport const createUndoRedoSlice = <State, CaseReducers extends SliceCaseReducers<State>>(\n  sliceOpts: CreateSliceOptions<State, CaseReducers, string>,\n  opts: Opts<State>\n) => {\n  const initialState: UndoRedoState<State> = {\n    past: [],\n    future: [],\n    current: isCallable(sliceOpts.initialState) ? sliceOpts.initialState() : sliceOpts.initialState,\n  };\n\n  const { limit = 10 } = opts ?? {};\n\n  const selector = opts.stateSelector || (<T>(state: Draft<T>): Draft<T> => state);\n\n  const wrappedReducers = Object.keys(sliceOpts.reducers).reduce(\n    (acc, actionName: string) => {\n      const reducer = sliceOpts.reducers[actionName];\n\n      if (!isCallable(reducer)) {\n        throw new Error('Reducer must be a function. prepapre not support in UndoRedo wrapper');\n      }\n\n      acc[actionName] = (state, action) => {\n        const newCurrent = reducer(state.current as Draft<State>, action);\n\n        if (opts.excludeActionsFromHistory?.includes(actionName)) {\n          if (newCurrent !== undefined) {\n            state.current = newCurrent as Draft<State>;\n          }\n\n          return;\n        }\n\n        const originalCurrent = original(state.current);\n\n        state.past.push(selector(originalCurrent)!);\n        if (state.past.length > limit) {\n          state.past.shift();\n        }\n        state.future = [];\n\n        if (newCurrent !== undefined) {\n          state.current = newCurrent as Draft<State>;\n        }\n      };\n\n      return acc;\n    },\n    {} as Record<string, CaseReducer<UndoRedoState<State>, PayloadAction<unknown>>>\n  ) as WrappedUndoRedoReducer<State, CaseReducers>;\n\n  return createSlice<UndoRedoState<State>, UndoRedoReducer<State, CaseReducers>>({\n    name: sliceOpts.name,\n    initialState,\n    // @ts-expect-error - TS doesn't like the fact that we're adding extra reducers\n    reducers: {\n      ...wrappedReducers,\n      undo: (state) => {\n        if (state.past.length === 0) {\n          return;\n        }\n\n        const previous = state.past.pop();\n\n        if (previous !== undefined) {\n          state.future = [state.current, ...state.future];\n          // reapply the previous state partially\n          // @ts-expect-error - TS doesn't like the fact that we're mutating the state\n          state.current = { ...state.current, ...previous };\n        }\n      },\n\n      redo: (state) => {\n        if (state.future.length === 0) {\n          return;\n        }\n\n        const next = state.future.shift();\n        if (next != undefined) {\n          state.past = [...state.past, state.current];\n          // reapply the previous state partially\n          // @ts-expect-error - TS doesn't like the fact that we're mutating the state\n          state.current = { ...state.current, ...next };\n        }\n      },\n\n      discardAll: (state) => {\n        if (opts.discard) {\n          opts.discard(state.current);\n        } else {\n          // @ts-expect-error - TS doesn't like the fact that we're mutating the state\n          state.current = initialState.current;\n        }\n        state.past = [];\n        state.future = [];\n      },\n\n      clearHistory: (state) => {\n        state.past = [];\n        state.future = [];\n      },\n    },\n  });\n};\n"],"names":["isCallable","obj","createUndoRedoSlice","sliceOpts","opts","initialState","past","future","current","limit","selector","stateSelector","state","wrappedReducers","Object","keys","reducers","reduce","acc","actionName","reducer","Error","action","newCurrent","excludeActionsFromHistory","includes","undefined","originalCurrent","original","push","length","shift","createSlice","name","undo","previous","pop","redo","next","discardAll","discard","clearHistory"],"mappings":";;AAuCA,MAAMA,aAAa,CAACC,GAAAA,GAAAA;AAClB,IAAA,OAAO,OAAOA,GAAAA,KAAQ,UAAA;AACxB,CAAA;AAEO,MAAMC,mBAAAA,GAAsB,CACjCC,SAAAA,EACAC,IAAAA,GAAAA;AAEA,IAAA,MAAMC,YAAAA,GAAqC;AACzCC,QAAAA,IAAAA,EAAM,EAAE;AACRC,QAAAA,MAAAA,EAAQ,EAAE;QACVC,OAAAA,EAASR,UAAAA,CAAWG,UAAUE,YAAY,CAAA,GAAIF,UAAUE,YAAY,EAAA,GAAKF,UAAUE;AACrF,KAAA;AAEA,IAAA,MAAM,EAAEI,KAAAA,GAAQ,EAAE,EAAE,GAAGL,QAAQ,EAAC;AAEhC,IAAA,MAAMM,WAAWN,IAAAA,CAAKO,aAAa,KAAK,CAAIC,QAA8BA,KAAI,CAAA;IAE9E,MAAMC,eAAAA,GAAkBC,MAAAA,CAAOC,IAAI,CAACZ,SAAAA,CAAUa,QAAQ,CAAA,CAAEC,MAAM,CAC5D,CAACC,GAAAA,EAAKC,UAAAA,GAAAA;AACJ,QAAA,MAAMC,OAAAA,GAAUjB,SAAAA,CAAUa,QAAQ,CAACG,UAAAA,CAAW;QAE9C,IAAI,CAACnB,WAAWoB,OAAAA,CAAAA,EAAU;AACxB,YAAA,MAAM,IAAIC,KAAAA,CAAM,sEAAA,CAAA;AAClB,QAAA;AAEAH,QAAAA,GAAG,CAACC,UAAAA,CAAW,GAAG,CAACP,KAAAA,EAAOU,MAAAA,GAAAA;AACxB,YAAA,MAAMC,UAAAA,GAAaH,OAAAA,CAAQR,KAAAA,CAAMJ,OAAO,EAAkBc,MAAAA,CAAAA;AAE1D,YAAA,IAAIlB,IAAAA,CAAKoB,yBAAyB,EAAEC,QAAAA,CAASN,UAAAA,CAA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