import * as actions from './search-multiple.action';
import { Dataset } from "../../metamodel/model";
import { SearchMultipleQueryParams } from "./model";

export interface State {
    pristine: boolean;
    currentStep: string;
    positionStepChecked: boolean;
    datasetsStepChecked: boolean;
    resultStepChecked: boolean;
    queryParams: SearchMultipleQueryParams;
    selectedDatasets: string[];
}

export const initialState: State = {
    pristine: true,
    currentStep: null,
    positionStepChecked: false,
    datasetsStepChecked: false,
    resultStepChecked: false,
    queryParams: null,
    selectedDatasets: []
};

export function reducer(state: State = initialState, action: actions.Actions): State {
    switch (action.type) {
        case actions.INIT_SELECTED_DATASETS:
            return {
                ...state,
                selectedDatasets: action.payload
            };

        case actions.CHANGE_STEP:
            return {
                ...state,
                currentStep: action.payload
            };

        case actions.POSITION_CHECKED:
            return {
                ...state,
                pristine: false,
                positionStepChecked: true
            };

        case actions.DATASETS_CHECKED:
            return {
                ...state,
                datasetsStepChecked: true
            };

        case actions.UPDATE_SELECTED_DATASETS:
            return {
                ...state,
                selectedDatasets: action.payload
            };

        default:
            return state;
    }
}

export const getCurrentStep = (state: State) => state.currentStep;
export const getPositionStepChecked = (state: State) => state.positionStepChecked;
export const getDatasetsStepChecked = (state: State) => state.datasetsStepChecked;
export const getResultStepChecked = (state: State) => state.resultStepChecked;
export const getQueryParams = (state: State) => state.queryParams;
export const getSelectedDatasets = (state: State) => state.selectedDatasets;