{"version":3,"sources":["../../src/use-stepper/index.ts"],"sourcesContent":["import { useCounter } from '../use-counter'\nimport { useLatest } from '../use-latest'\nimport { useStableFn } from '../use-stable-fn'\n\nexport interface UseStepperReturns<StepName, Steps, Step> {\n  /**\n   * List of steps.\n   */\n  steps: Readonly<Steps>\n  /**\n   * Index of the current step.\n   */\n  index: number\n  /**\n   * Current step.\n   */\n  current: Step\n  /**\n   * Next step, or undefined if the current step is the last one.\n   */\n  next: StepName | undefined\n  /**\n   * Previous step, or undefined if the current step is the first one.\n   */\n  previous: StepName | undefined\n  /**\n   * Whether the current step is the first one.\n   */\n  isFirst: boolean\n  /**\n   * Whether the current step is the last one.\n   */\n  isLast: boolean\n  /**\n   * Get the step at the specified index.\n   */\n  at: (index: number) => Step | undefined\n  /**\n   * Get a step by the specified name.\n   */\n  get: (step: StepName) => Step | undefined\n  /**\n   * Go to the specified step.\n   */\n  goTo: (step: StepName) => void\n  /**\n   * Go to the next step. Does nothing if the current step is the last one.\n   */\n  goToNext(): void\n  /**\n   * Go to the previous step. Does nothing if the current step is the previous one.\n   */\n  goToPrevious(): void\n  /**\n   * Go back to the given step, only if the current step is after.\n   */\n  goBackTo: (step: StepName) => void\n  /**\n   * Checks whether the given step is the next step.\n   */\n  isNext: (step: StepName) => boolean\n  /**\n   * Checks whether the given step is the previous step.\n   */\n  isPrevious: (step: StepName) => boolean\n  /**\n   * Checks whether the given step is the current step.\n   */\n  isCurrent: (step: StepName) => boolean\n  /**\n   * Checks if the current step is before the given step.\n   */\n  isBefore: (step: StepName) => boolean\n  /**\n   * Checks if the current step is after the given step.\n   */\n  isAfter: (step: StepName) => boolean\n}\n\n/**\n * A React Hook that helps to create a stepper state.\n */\nexport function useStepper<T>(steps: T[], initialIdx?: number): UseStepperReturns<T, T[], T> {\n  const [idx, actions] = useCounter(initialIdx ?? 0)\n\n  const at = useStableFn((index: number) => {\n    const { steps } = latest.current\n    return steps[index]\n  })\n\n  const get = useStableFn((step: T) => {\n    const { steps } = latest.current\n    if (!steps.includes(step)) return\n    return at(steps.indexOf(step))\n  })\n\n  const goTo = useStableFn((step: T) => {\n    const { steps } = latest.current\n    if (steps.includes(step)) {\n      actions.set(steps.indexOf(step))\n    }\n  })\n\n  const goToNext = useStableFn(() => {\n    actions.setState((pre) => {\n      const isLast = pre === steps.length - 1\n      return isLast ? pre : pre + 1\n    })\n  })\n\n  const goToPrevious = useStableFn(() => {\n    actions.setState((pre) => {\n      const isFirst = pre === 0\n      return isFirst ? pre : pre - 1\n    })\n  })\n\n  const goBackTo = useStableFn((step: T) => isAfter(step) && goTo(step))\n  const isNext = useStableFn((step: T) => latest.current.steps.indexOf(step) === idx + 1)\n  const isPrevious = useStableFn((step: T) => latest.current.steps.indexOf(step) === idx - 1)\n  const isCurrent = useStableFn((step: T) => latest.current.steps.indexOf(step) === idx)\n\n  const isBefore = useStableFn((step: T) => {\n    const { index, steps } = latest.current\n    return index < steps.indexOf(step)\n  })\n\n  const isAfter = useStableFn((step: T) => {\n    const { index, steps } = latest.current\n    return index > steps.indexOf(step)\n  })\n\n  const isFirst = idx === 0\n  const isLast = idx === steps.length - 1\n  const next = steps[idx + 1]\n  const previous = steps[idx - 1]\n\n  const latest = useLatest({ isFirst, isLast, steps, index: idx })\n\n  const current = at(idx)\n\n  return {\n    index: idx,\n    steps,\n    current,\n    next,\n    previous,\n    isFirst,\n    isLast,\n    at,\n    get,\n    goTo,\n    goToNext,\n    goToPrevious,\n    goBackTo,\n    isNext,\n    isPrevious,\n    isCurrent,\n    isBefore,\n    isAfter,\n  }\n}\n"],"names":["useStepper","steps","initialIdx","useCounter","idx","actions","at","useStableFn","index","latest","current","get","step","includes","indexOf","goTo","set","goToNext","setState","pre","isLast","length","goToPrevious","isFirst","goBackTo","isAfter","isNext","isPrevious","isCurrent","isBefore","next","previous","useLatest"],"mappings":";;;;+BAkFgBA;;;eAAAA;;;0BAlFW;yBACD;2BACE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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