{"version":3,"sources":["../../src/use-reset-state/index.ts"],"sourcesContent":["import { useLatest } from '../use-latest'\nimport { useSafeState } from '../use-safe-state'\nimport { useStableFn } from '../use-stable-fn'\nimport { useUpdateEffect } from '../use-update-effect'\nimport { isDefined } from '../utils/basic'\n\nimport type { ReactSetState, UseSafeStateOptions } from '../use-safe-state'\nimport type { Gettable } from '../utils/basic'\n\nexport interface UseResetStateOptions extends UseSafeStateOptions {}\n\n/**\n * A React Hook similar to [React.useState](https://react.dev/reference/react/useState), but it additionally provides a `reset` function to reset the state back to the initial value.\n */\nexport function useResetState<T>(\n  initialState: Gettable<T>,\n  options?: UseResetStateOptions,\n): readonly [T, ReactSetState<T>, (initialState?: T) => void, T]\nexport function useResetState<T = undefined>(): readonly [\n  T | undefined,\n  ReactSetState<T | undefined>,\n  (initialState?: T) => void,\n  T | undefined,\n]\nexport function useResetState<T>(initialState?: Gettable<T>, options?: UseResetStateOptions) {\n  const [init, setInit] = useSafeState(initialState, options)\n  const latest = useLatest({ init })\n  const [state, setState] = useSafeState<T | undefined>(init, options)\n\n  const reset = useStableFn((newVal?: T) => {\n    if (isDefined(newVal)) {\n      setInit(newVal)\n      setState(newVal)\n    } else {\n      setState(latest.current.init)\n    }\n  })\n\n  useUpdateEffect(() => setInit(initialState), [initialState])\n\n  return [state, setState, reset, init] as const\n}\n"],"names":["useResetState","initialState","options","useSafeState","init","setInit","latest","useLatest","state","setState","reset","useStableFn","newVal","isDefined","current","useUpdateEffect"],"mappings":";;;;+BAwBgBA;;;eAAAA;;;yBAxBU;4BACG;2BACD;+BACI;qBACN;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoBnB,SAASA,cAAiBC,YAA0B,EAAEC,OAA8B;IACzF,IAAwBC,iCAAAA,IAAAA,0BAAY,EAACF,cAAcC,cAA5CE,OAAiBD,kBAAXE,UAAWF;IACxB,IAAMG,SAASC,IAAAA,oBAAS,EAAC;QAAEH,MAAAA;IAAK;IAChC,IAA0BD,kCAAAA,IAAAA,0BAAY,EAAgBC,MAAMF,cAArDM,QAAmBL,mBAAZM,WAAYN;IAE1B,IAAMO,QAAQC,IAAAA,wBAAW,EAAC,SAACC;QACzB,IAAIC,IAAAA,gBAAS,EAACD,SAAS;YACrBP,QAAQO;YACRH,SAASG;QACX,OAAO;YACLH,SAASH,OAAOQ,OAAO,CAACV,IAAI;QAC9B;IACF;IAEAW,IAAAA,gCAAe,EAAC;eAAMV,QAAQJ;OAAe;QAACA;KAAa;IAE3D,OAAO;QAACO;QAAOC;QAAUC;QAAON;KAAK;AACvC"}