{"version":3,"sources":["../../src/use-counter/index.ts"],"sourcesContent":["import { useCreation } from '../use-creation'\nimport { useLatest } from '../use-latest'\nimport { useSetState } from '../use-set-state'\nimport { useStableFn } from '../use-stable-fn'\nimport { useUpdateEffect } from '../use-update-effect'\nimport { clamp, isFunction } from '../utils/basic'\n\nimport type { SetStateAction } from 'react'\n\nexport type UseCounterOptions = {\n  /**\n   * The maximum value of the counter\n   */\n  max?: number\n  /**\n   * The minimum value of the counter\n   */\n  min?: number\n}\n\nexport interface UseCounterReturnsAction {\n  /**\n   * Increment the counter\n   */\n  inc: (delta?: number) => void\n  /**\n   * Decrement the counter\n   */\n  dec: (delta?: number) => void\n  /**\n   * Set the counter\n   */\n  set: (value: number) => void\n  /**\n   * Get the counter\n   */\n  get(): number\n  /**\n   * Reset the counter\n   */\n  reset: (n?: number) => void\n  /**\n   * Set the counter, use React's `setState` style, supports function updater to avoid expired value\n   *\n   * @since 1.9.0\n   */\n  setState: (state: SetStateAction<number>) => void\n}\n\nexport interface UseCounterState {\n  /**\n   * The value of the counter\n   */\n  count: number\n  /**\n   * The maximum value of the counter\n   */\n  max: number\n  /**\n   * The minimum value of the counter\n   */\n  min: number\n  /**\n   * The initial value of the counter\n   */\n  initialCount: number\n}\n\nexport type UseCounterReturns = readonly [\n  /**\n   * The count state of the counter\n   */\n  number,\n  /**\n   * Functions to control the counter\n   */\n  UseCounterReturnsAction,\n  /**\n   * The full internal state of the counter\n   */\n  UseCounterState,\n]\n\n/**\n * A React hook that provides a counter with increment, decrement, and reset functionalities.\n *\n * @param {number} [initialCount=0] - `number`, The initial value of the counter\n * @param {UseCounterOptions} [options] - `UseCounterOptions`, The options of the counter, see {@link UseCounterOptions}\n */\nexport function useCounter(initialCount: number = 0, options: UseCounterOptions = {}): UseCounterReturns {\n  const { min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER } = options\n\n  const [state, _setState] = useSetState<UseCounterState>(\n    {\n      initialCount: initialCount,\n      count: clamp(initialCount, min, max),\n      max,\n      min,\n    },\n    { deep: true },\n  )\n\n  const latest = useLatest(state)\n\n  useUpdateEffect(() => {\n    _setState({ initialCount })\n  }, [initialCount])\n\n  useUpdateEffect(() => {\n    const [max, min] = [options.max ?? Number.MAX_SAFE_INTEGER, options.min ?? Number.MIN_SAFE_INTEGER]\n    _setState((pre) => ({ count: clamp(pre.count, min, max), max, min }))\n  }, [options.max, options.min])\n\n  const inc = useStableFn((delta = 1) => {\n    _setState((pre) => ({ count: clamp(pre.count + delta, pre.min, pre.max) }))\n  })\n\n  const dec = useStableFn((delta = 1) => {\n    _setState((pre) => ({ count: clamp(pre.count - delta, pre.min, pre.max) }))\n  })\n\n  const set = useStableFn((value: number) => {\n    _setState((pre) => ({ count: clamp(value, pre.min, pre.max) }))\n  })\n\n  const get = useStableFn(() => latest.current.count)\n\n  const reset = useStableFn((n = latest.current.initialCount) => {\n    _setState((_pre) => ({\n      initialCount: n,\n      count: clamp(n, latest.current.min, latest.current.max),\n    }))\n  })\n\n  const setState = useStableFn((state: SetStateAction<number>) => {\n    _setState((pre) => ({\n      count: clamp(isFunction(state) ? state(pre.count) : state, pre.min, pre.max),\n    }))\n  })\n\n  const actions = useCreation(() => ({ inc, dec, set, get, reset, setState }))\n\n  return [state.count, actions, state] as const\n}\n"],"names":["useCounter","initialCount","options","min","Number","MIN_SAFE_INTEGER","max","MAX_SAFE_INTEGER","useSetState","count","clamp","deep","state","_setState","latest","useLatest","useUpdateEffect","pre","inc","useStableFn","delta","dec","set","value","get","current","reset","n","_pre","setState","isFunction","actions","useCreation"],"mappings":";;;;+BAyFgBA;;;eAAAA;;;2BAzFY;yBACF;2BACE;2BACA;+BACI;qBACE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoF3B,SAASA;QAAWC,eAAAA,iEAAuB,GAAGC,UAAAA,iEAA6B,CAAC;IACjF,mBAAyEA,QAAjEC,KAAAA,gCAAMC,OAAOC,gBAAgB,gCAAoCH,QAA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