import _ from 'lodash';

import { PR_CHECK_MERGEABILITY } from '../actions/pull-request';
import { PR_SHOW_MERGE_ERRORS, PR_HIDE_MERGE_ERRORS } from '../pull-request-header/actions';
import createReducer from '../utils/create-reducer';
import mergeWithRollback from '../utils/merge-state-with-rollback';

const defaultState = {
    canMerge: false,
    conflicted: null,
    vetoes: null,
    properties: null,
    isChecking: false,
    showDialog: false,
};

export default createReducer(defaultState, {
    [PR_CHECK_MERGEABILITY](state, action) {
        return mergeWithRollback(state, action, {
            forward() {
                return {
                    isChecking: true,
                };
            },
            commit() {
                return {
                    ...action.payload,
                    isChecking: false,
                };
            },
        });
    },
    [PR_SHOW_MERGE_ERRORS](state, action) {
        return _.assign({}, state, {
            showDialog: true,
        });
    },
    [PR_HIDE_MERGE_ERRORS](state, action) {
        return _.assign({}, state, {
            showDialog: false,
        });
    },
});
