import {
  CONTRACT_FORMS,
  FETCHING_CONTRACT_FORMS,
  SetFormsActionTypes,
  DELETING_CONTRACT_FORM,
  CONTRACT_FORM_DELETED,
  ADDING_CONTRACT_FORM,
  CONTRACT_FORM_ADDED,
  EDITING_CONTRACT_FORM,
  CONTRACT_FORM_EDITED,
} from './actions';

const initialState = {
  data: [],
  fetchingContractForms: false,
  deletingContractForm: false,
  contractFormDeleted: null,
  addingContractForm: false,
  contractFormAdded: false,
  editingContractForm: false,
  contractFormEdited: false,
  formErrors: {},
};

export const formsReducer = (state = initialState, action: SetFormsActionTypes) => {
  switch (action.type) {
    case CONTRACT_FORMS: {
      return {
        ...state,
        data: action.payload,
      };
    }
    case FETCHING_CONTRACT_FORMS: {
      return {
        ...state,
        fetchingContractForms: action.payload,
      };
    }
    case DELETING_CONTRACT_FORM: {
      return {
        ...state,
        deletingContractForm: action.payload,
      };
    }
    case CONTRACT_FORM_DELETED: {
      return {
        ...state,
        contractFormDeleted: action.payload,
      };
    }
    case ADDING_CONTRACT_FORM: {
      return {
        ...state,
        addingContractForm: action.payload,
      };
    }
    case CONTRACT_FORM_ADDED: {
      return {
        ...state,
        contractFormAdded: action.payload,
      };
    }
    case EDITING_CONTRACT_FORM: {
      return {
        ...state,
        editingContractForm: action.payload,
      };
    }
    case CONTRACT_FORM_EDITED: {
      return {
        ...state,
        contractFormEdited: action.payload,
      };
    }
    case 'CONTRACT_FORM_ERRORS': {
      return {
        ...state,
        formErrors: action.payload || {},
      };
    }
    default:
      return state;
  }
};
