import { useState } from "react"; export type StepProps = { step: number; goToNextStep: () => void; goToPreviousStep: () => void; goToStep: (step: number) => void; currentStepIndex: number; nextStepIndex: number | null; previousStepIndex: number | null; isFirstStep: boolean; isLastStep: boolean; }; export const useStep = ({ steps = 1, initialStep = 1, }: { steps?: number; initialStep?: number; }): StepProps => { const [step, setStep] = useState(initialStep); const goToNextStep = () => { if (step < steps) { setStep(step + 1); } }; const goToPreviousStep = () => { if (step !== 1) { setStep(step - 1); } }; const goToStep = (newStep: number) => { if (newStep > 0 && newStep <= steps) { setStep(newStep); } }; const currentStepIndex = step - 1; const nextStepIndex = step <= steps - 1 ? step : null; const previousStepIndex = step - 2 >= 0 ? step - 2 : null; const isFirstStep = step === 1; const isLastStep = step === steps; return { step, goToNextStep, goToPreviousStep, goToStep, currentStepIndex, nextStepIndex, previousStepIndex, isFirstStep, isLastStep, }; };