import type { Ref } from '@stacksjs/stx' import { computed, ref } from '@stacksjs/stx' export interface UseStepperReturn { steps: Ref current: Ref index: Ref isFirst: Ref isLast: Ref next: () => void previous: () => void goTo: (step: T) => void goToIndex: (index: number) => void isBefore: (step: T) => boolean isAfter: (step: T) => boolean isCurrent: (step: T) => boolean } /** * Step-by-step navigation composable. * Provides reactive state and controls for stepping through an ordered list. * * @param steps - Array of step values * @param initialStep - Optional initial step (defaults to first step) */ export function useStepper(steps: T[], initialStep?: T): UseStepperReturn { const stepsRef = ref([...steps]) as Ref const initialIndex = initialStep !== undefined ? steps.indexOf(initialStep) : 0 const index = ref(initialIndex >= 0 ? initialIndex : 0) // index is always kept within [0, length); when steps is empty, current is read-only by callers const current = computed(() => stepsRef.value[index.value] as T) const isFirst = computed(() => index.value === 0) const isLast = computed(() => index.value === stepsRef.value.length - 1) function next(): void { if (index.value < stepsRef.value.length - 1) { index.value++ } } function previous(): void { if (index.value > 0) { index.value-- } } function goTo(step: T): void { const idx = stepsRef.value.indexOf(step) if (idx >= 0) { index.value = idx } } function goToIndex(idx: number): void { if (idx >= 0 && idx < stepsRef.value.length) { index.value = idx } } function isBefore(step: T): boolean { const stepIndex = stepsRef.value.indexOf(step) return stepIndex >= 0 && index.value < stepIndex } function isAfter(step: T): boolean { const stepIndex = stepsRef.value.indexOf(step) return stepIndex >= 0 && index.value > stepIndex } function isCurrent(step: T): boolean { return stepsRef.value[index.value] === step } return { steps: stepsRef, current, index, isFirst, isLast, next, previous, goTo, goToIndex, isBefore, isAfter, isCurrent, } }