import { capitalizeWords, } from './utils.js';
import { create } from 'zustand';
// Factory function to create the store
export function createWizardStore(wizard) {
    const initialState = {
        stepId: wizard[0].id,
    };
    const store = create((set, get) => {
        const goToPreviousStep = () => {
            const currentState = get().wizardState;
            const currentStepIndex = wizard.findIndex(step => step.id === currentState.stepId);
            if (currentStepIndex <= 0)
                return; // Can't go back from first step
            const prevStepId = wizard[currentStepIndex - 1].id;
            const prevState = {
                ...currentState,
                stepId: prevStepId,
            };
            set({ wizardState: prevState });
        };
        const submitFunctions = wizard.reduce((acc, step, index) => {
            const currentStepId = step.id;
            const nextStepId = wizard[index + 1]?.id || 'completed';
            const functionName = `submit${capitalizeWords(currentStepId)}`;
            acc[functionName] = (value) => {
                const currentState = get().wizardState;
                const nextState = {
                    ...currentState,
                    ...value,
                    stepId: nextStepId,
                };
                set({
                    wizardState: nextState,
                });
            };
            return acc;
        }, {});
        return {
            wizardState: initialState,
            steps: wizard,
            setWizardState: (state) => {
                set({ wizardState: state });
            },
            goToPreviousStep,
            ...submitFunctions,
        };
    });
    return store;
}
