{"version":3,"file":"index.mjs","names":[],"sources":["../../../src/hooks/useStep/index.ts"],"sourcesContent":["import { SetStateAction, useCallback, useState } from 'react';\nimport { isFunction } from '@modern-kit/utils';\n\ntype StepType = 'nextStep' | 'prevStep';\n\nexport type StepAction = ({\n  prev,\n  next,\n}: {\n  prev: number;\n  next: number;\n}) => void;\n\nexport interface UseStepProps {\n  maxStep: number;\n  initialStep?: number;\n  infinite?: boolean;\n}\n\ninterface UseStepReturnType {\n  currentStep: number;\n  hasNextStep: boolean;\n  hasPrevStep: boolean;\n  goToNextStep: (action?: StepAction) => void;\n  goToPrevStep: (action?: StepAction) => void;\n  setStep: (step: SetStateAction<number>, action?: StepAction) => void;\n  resetStep: (action?: StepAction) => void;\n}\n\n/**\n * @description 단계별로 이동할 수 있는 기능을 제공하며, 초기 단계 설정, 다음 단계로의 이동, 이전 단계로의 이동, 특정 단계로의 설정 등의 기능을 포함합니다.\n * 스텝 설정 함수 호출 시 `action` 함수를 인자로 넣어 원하는 action을 실행 시킬 수 있습니다.\n *\n * @param {Object} props - 훅에 전달되는 매개변수 객체\n * @property {number} props.maxStep - 가능한 최대 단계. 이 값은 필수이며, 최종 단계의 번호를 정의합니다.\n * @property {number} [props.initialStep=0] - 초기 단계. 기본값은 0이며, 이 값으로 훅을 처음 사용할 때 설정될 단계가 결정됩니다.\n * @property {boolean} [props.infinite=false] - 무한 반복 여부. true일 경우, 마지막 단계에서 'nextStep'을 호출하면 첫 단계로 돌아가며, 첫 단계에서 'prevStep'을 호출하면 마지막 단계로 이동합니다. 기본값은 false입니다.\n *\n * @returns {UseStepReturnType} 단계 관리 기능을 제공하는 여러 함수 및 값들을 반환합니다.\n * - `currentStep`: 현재 단계. 현재 진행 중인 단계를 나타냅니다.\n * - `hasNextStep`: 다음 단계가 있는지 여부를 반환합니다.\n * - `hasPrevStep`: 이전 단계가 있는지 여부를 반환합니다.\n * - `goToNextStep`: 다음 단계로 이동하는 함수입니다.\n * - `goToPrevStep`: 이전 단계로 이동하는 함수입니다.\n * - `setStep`: 특정 단계로 이동하는 함수입니다.\n * - `resetStep`: 초기 단계로 리셋하는 함수입니다.\n *\n * @example\n * const {\n *   currentStep,\n *   goToNextStep,\n *   goToPrevStep,\n *   setStep,\n *   resetStep,\n * } = useStep({ maxStep: 5 });\n *\n * currentStep; // 0, 초기값\n *\n * goToNextStep(); // 다음 단계로 이동\n * currentStep; // 1\n *\n * goToPrevStep(); // 이전 단계로 이동\n * currentStep; // 0\n *\n * setStep(3); // 3단계로 이동\n * currentStep // 3;\n *\n * resetStep(); // 초기 단계로 리셋\n * currentStep; // 0\n */\nexport function useStep({\n  maxStep,\n  initialStep = 0,\n  infinite = false,\n}: UseStepProps): UseStepReturnType {\n  const [currentStep, setCurrentStep] = useState(initialStep);\n  const hasNextStep = currentStep < maxStep;\n  const hasPrevStep = currentStep > 0;\n\n  const getNextStep = useCallback(\n    (type: StepType, isOverflow: boolean) => {\n      const isNextStepType = type === 'nextStep';\n\n      if (isOverflow) {\n        return isNextStepType ? 0 : maxStep;\n      }\n      return isNextStepType ? currentStep + 1 : currentStep - 1;\n    },\n    [maxStep, currentStep]\n  );\n\n  const handleStep = useCallback(\n    (type: StepType, isOverflow: boolean, action?: StepAction) => {\n      if (isOverflow && !infinite) return;\n\n      const nextStep = getNextStep(type, isOverflow);\n\n      if (action) {\n        action({ prev: currentStep, next: nextStep });\n      }\n      setCurrentStep(nextStep);\n    },\n    [infinite, currentStep, getNextStep]\n  );\n\n  const goToNextStep = useCallback(\n    (action?: StepAction) => {\n      handleStep('nextStep', !hasNextStep, action);\n    },\n    [hasNextStep, handleStep]\n  );\n\n  const goToPrevStep = useCallback(\n    (action?: StepAction) => {\n      handleStep('prevStep', !hasPrevStep, action);\n    },\n    [hasPrevStep, handleStep]\n  );\n\n  const setStep = useCallback(\n    (step: SetStateAction<number>, action?: StepAction) => {\n      const nextStep = isFunction(step) ? step(currentStep) : step;\n      const isValidNextStep = nextStep >= 0 && nextStep <= maxStep;\n\n      if (isValidNextStep) {\n        if (action) {\n          action({ prev: currentStep, next: nextStep });\n        }\n        setCurrentStep(step);\n        return;\n      }\n      throw new Error('유효하지 않은 step입니다.');\n    },\n    [currentStep, maxStep]\n  );\n\n  const resetStep = useCallback(\n    (action?: StepAction) => {\n      if (action) {\n        action({ prev: currentStep, next: initialStep });\n      }\n      setCurrentStep(initialStep);\n    },\n    [currentStep, initialStep]\n  );\n\n  return {\n    currentStep,\n    hasNextStep,\n    hasPrevStep,\n    goToNextStep,\n    goToPrevStep,\n    setStep,\n    resetStep,\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsEA,SAAgB,QAAQ,EACtB,SACA,cAAc,GACd,WAAW,SACuB;CAClC,MAAM,CAAC,aAAa,kBAAkB,SAAS,YAAY;CAC3D,MAAM,cAAc,cAAc;CAClC,MAAM,cAAc,cAAc;CAElC,MAAM,cAAc,aACjB,MAAgB,eAAwB;EACvC,MAAM,iBAAiB,SAAS;EAEhC,IAAI,YACF,OAAO,iBAAiB,IAAI;EAE9B,OAAO,iBAAiB,cAAc,IAAI,cAAc;IAE1D,CAAC,SAAS,YAAY,CACvB;CAED,MAAM,aAAa,aAChB,MAAgB,YAAqB,WAAwB;EAC5D,IAAI,cAAc,CAAC,UAAU;EAE7B,MAAM,WAAW,YAAY,MAAM,WAAW;EAE9C,IAAI,QACF,OAAO;GAAE,MAAM;GAAa,MAAM;GAAU,CAAC;EAE/C,eAAe,SAAS;IAE1B;EAAC;EAAU;EAAa;EAAY,CACrC;CA2CD,OAAO;EACL;EACA;EACA;EACA,cA7CmB,aAClB,WAAwB;GACvB,WAAW,YAAY,CAAC,aAAa,OAAO;KAE9C,CAAC,aAAa,WAAW,CAyCb;EACZ,cAvCmB,aAClB,WAAwB;GACvB,WAAW,YAAY,CAAC,aAAa,OAAO;KAE9C,CAAC,aAAa,WAAW,CAmCb;EACZ,SAjCc,aACb,MAA8B,WAAwB;GACrD,MAAM,WAAW,WAAW,KAAK,GAAG,KAAK,YAAY,GAAG;GAGxD,IAFwB,YAAY,KAAK,YAAY,SAEhC;IACnB,IAAI,QACF,OAAO;KAAE,MAAM;KAAa,MAAM;KAAU,CAAC;IAE/C,eAAe,KAAK;IACpB;;GAEF,MAAM,IAAI,MAAM,mBAAmB;KAErC,CAAC,aAAa,QAAQ,CAmBf;EACP,WAjBgB,aACf,WAAwB;GACvB,IAAI,QACF,OAAO;IAAE,MAAM;IAAa,MAAM;IAAa,CAAC;GAElD,eAAe,YAAY;KAE7B,CAAC,aAAa,YAAY,CAUjB;EACV"}