import { validateAnswers } from '../../validateAnswers';
const initialState = {
    answersById: {},
    examStructure: {
        name: 'exam',
        attributes: {},
        childNodes: []
    },
    focusedQuestionId: null,
    supportsAnswerHistory: false,
    serverQuestionIds: new Set(),
    savedQuestionIds: new Set(),
    validationErrors: []
};
export default function answersReducer(state = initialState, action) {
    switch (action.type) {
        case 'SAVE_ANSWER': {
            const { questionId } = action.payload;
            const synchronizedQuestionIds = setDelete(state.savedQuestionIds, questionId);
            const answersById = { ...state.answersById, [questionId]: action.payload };
            return { ...state, savedQuestionIds: synchronizedQuestionIds, answersById };
        }
        case 'SAVE_ANSWER_FAILED': {
            return state;
        }
        case 'SAVE_ANSWER_SUCCEEDED': {
            const { questionId } = action.payload;
            const savedQuestionIds = setAdd(state.savedQuestionIds, questionId);
            const serverQuestionIds = setAdd(state.serverQuestionIds, questionId);
            const validationErrors = validateAnswers(state.examStructure, state.answersById);
            return { ...state, serverQuestionIds, savedQuestionIds, validationErrors };
        }
        case 'ANSWER_FOCUSED': {
            const questionId = action.payload;
            return { ...state, focusedQuestionId: questionId };
        }
        case 'ANSWER_BLURRED': {
            const questionId = action.payload;
            return { ...state, focusedQuestionId: state.focusedQuestionId === questionId ? null : state.focusedQuestionId };
        }
        default:
            return state;
    }
}
function setAdd(set, value) {
    const copy = new Set(set);
    copy.add(value);
    return copy;
}
function setDelete(set, value) {
    const copy = new Set(set);
    copy.delete(value);
    return copy;
}
//# sourceMappingURL=reducer.js.map