{"version":3,"sources":["../../src/use-state-history/index.ts"],"sourcesContent":["import { useLatest } from '../use-latest'\nimport { useManualStateHistory } from '../use-manual-state-history'\nimport { usePausableUpdateDeepCompareEffect } from '../use-pausable-update-deep-compare-effect'\nimport { usePausableUpdateEffect } from '../use-pausable-update-effect'\nimport { useStableFn } from '../use-stable-fn'\n\nimport type { UseManualStateHistoryOptions, UseManualStateHistoryReturns } from '../use-manual-state-history'\nimport type { Pausable } from '../use-pausable'\n\nexport interface UseStateHistoryOptions<Raw, Serialized = Raw> extends UseManualStateHistoryOptions<Raw, Serialized> {\n  deep?: boolean\n}\n\nexport interface UseStateHistoryReturns<Raw, Serialized>\n  extends UseManualStateHistoryReturns<Raw, Serialized>,\n    Pausable<[commit?: boolean], [commit?: boolean]> {}\n\n/**\n * A React Hook that track and manipulate the history of a state automatically.\n */\nexport function useStateHistory<Raw, Serialized = Raw>(\n  source: Raw,\n  options: UseStateHistoryOptions<Raw, Serialized> = {},\n): UseStateHistoryReturns<Raw, Serialized> {\n  const isDeep = options.deep || false\n\n  const manualHistory = useManualStateHistory(source, { ...options, clone: options.clone || isDeep })\n\n  const pausable = usePausableUpdateEffect(() => latest.current.manualHistory.commit(), isDeep ? [] : [source])\n\n  const deepPausable = usePausableUpdateDeepCompareEffect(\n    () => latest.current.manualHistory.commit(),\n    isDeep ? [source] : [],\n  )\n\n  const latest = useLatest({ manualHistory, isDeep, deepPausable, pausable })\n\n  const pause = useStableFn((update = false, commit = false) => {\n    const { manualHistory, isDeep, deepPausable, pausable } = latest.current\n    commit && manualHistory.commit()\n    ;(isDeep ? deepPausable : pausable).pause(update)\n  })\n\n  const resume = useStableFn((update = false, commit = false) => {\n    const { manualHistory, isDeep, deepPausable, pausable } = latest.current\n    commit && manualHistory.commit()\n    ;(isDeep ? deepPausable : pausable).resume(update)\n  })\n\n  const isActive = (isDeep ? deepPausable : pausable).isActive\n\n  return {\n    ...manualHistory,\n    isActive,\n    resume,\n    pause,\n  }\n}\n"],"names":["useStateHistory","source","options","isDeep","deep","manualHistory","useManualStateHistory","clone","pausable","usePausableUpdateEffect","latest","current","commit","deepPausable","usePausableUpdateDeepCompareEffect","useLatest","pause","useStableFn","update","resume","isActive"],"mappings":";;;;+BAoBgBA;;;eAAAA;;;yBApBU;qCACY;kDACa;uCACX;2BACZ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgBrB,SAASA,gBACdC,MAAW;QACXC,UAAAA,iEAAmD,CAAC;IAEpD,IAAMC,SAASD,QAAQE,IAAI,IAAI;IAE/B,IAAMC,gBAAgBC,IAAAA,4CAAqB,EAACL,QAAQ,wCAAKC;QAASK,OAAOL,QAAQK,KAAK,IAAIJ;;IAE1F,IAAMK,WAAWC,IAAAA,gDAAuB,EAAC;eAAMC,OAAOC,OAAO,CAACN,aAAa,CAACO,MAAM;OAAIT,SAAS,EAAE,GAAG;QAACF;KAAO;IAE5G,IAAMY,eAAeC,IAAAA,sEAAkC,EACrD;eAAMJ,OAAOC,OAAO,CAACN,aAAa,CAACO,MAAM;OACzCT,SAAS;QAACF;KAAO,GAAG,EAAE;IAGxB,IAAMS,SAASK,IAAAA,oBAAS,EAAC;QAAEV,eAAAA;QAAeF,QAAAA;QAAQU,cAAAA;QAAcL,UAAAA;IAAS;IAEzE,IAAMQ,QAAQC,IAAAA,wBAAW,EAAC;YAACC,0EAAS,OAAON,0EAAS;QAClD,IAA0DF,kBAAAA,OAAOC,OAAO,EAAhEN,gBAAkDK,gBAAlDL,eAAeF,SAAmCO,gBAAnCP,QAAQU,eAA2BH,gBAA3BG,cAAcL,WAAaE,gBAAbF;QAC7CI,UAAUP,cAAcO,MAAM;QAC5BT,CAAAA,SAASU,eAAeL,QAAO,EAAGQ,KAAK,CAACE;IAC5C;IAEA,IAAMC,SAASF,IAAAA,wBAAW,EAAC;YAACC,0EAAS,OAAON,0EAAS;QACnD,IAA0DF,kBAAAA,OAAOC,OAAO,EAAhEN,gBAAkDK,gBAAlD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