import handleUserClicked from "./userpathhandler";

export default function reduce(state, action) {
    switch (action.type) {
        case 'started_loading': {
            return {
                ...state,
                maze: null,
                loading: true,
                errorMessage: null
            }
        }
        case 'loaded_maze': {
            return {
                ...state,
                loading: false,
                maze: action.maze,
                userPath: []
            }
        }
        case 'loading_failed': {
            return {
                ...state,
                loading: false,
                errorMessage: `Failed to load maze. Reason: ${action.reason}`
            }
        }
        case 'toggled_show_solution': {
            return {
                ...state,
                showSolution: action.value
            }
        }
        case 'closed_message_banner': {
            return {
                ...state,
                errorMessage: null
            }
        }
        case 'clicked_cell': {
            // There's so much logic involved, externalize that into its own file.
            return handleUserClicked(state, action.x, action.y);
        }
        default: {
            throw new Error(`Unknown action: ${action.type}`);
        }
    }
}