import { clientActionTypes } from '../action-types/client-action.types';
const initialState = {
  sharedForm: {},
  pages: [],
  countries: {},
  currentPage: 1,
  failedValidations: [],
};
export default (state = initialState, action) => {
  switch (action.type) {
    case clientActionTypes.GET_SHARED_FORM: {
      const tempPages = [];
      action.payload.form.formData.map(page => {
        const tempElements = [];
        page.elements.map(elem => {
          tempElements.push({
            answer: '',
            id: elem.unique_id,
            type: elem.type,
            question: elem.label,
          });
        });
        tempPages.push(tempElements);
      });
      return { ...state, sharedForm: action.payload, pages: tempPages };
    }

    case clientActionTypes.ENTER_REGULAR_ANSWERS: {
      const copyAnswers = [...state.pages];
      const { index, value, activePage } = action.payload;
      copyAnswers[activePage - 1][index].answer = value;
      return { ...state, pages: copyAnswers };
    }

    case clientActionTypes.UPDATE_LOCATION_ANSWER: {
      const tempUpdateLocation = [...state.pages];
      const { index, value, activePage, locationType } = action.payload;
      tempUpdateLocation[activePage - 1][index].answer = {
        ...tempUpdateLocation[activePage - 1][index].answer,
        [locationType]: value,
      };
      return { ...state, pages: tempUpdateLocation };
    }

    case clientActionTypes.UPDATE_ADDRESS_DATA: {
      const copyAddress = [...state.pages];
      const { index, value, activePage, label } = action.payload;
      copyAddress[activePage - 1][index].answer = {
        ...copyAddress[activePage - 1][index].answer,
        [label]: value,
      };
      return { ...state, pages: copyAddress };
    }

    case clientActionTypes.UPDATE_MULTI_ANSWER: {
      const copyMultiAnswers = [...state.pages];
      const { index, value, activePage } = action.payload;

      if (copyMultiAnswers[activePage - 1][index].answer.length < 1) {
        copyMultiAnswers[activePage - 1][index].answer = [value];
      } else {
        if (copyMultiAnswers[activePage - 1][index].answer.includes(value)) {
          const idx = copyMultiAnswers[activePage - 1][index].answer.indexOf(
            value
          );
          copyMultiAnswers[activePage - 1][index].answer.splice(idx, 1);
        } else {
          copyMultiAnswers[activePage - 1][index].answer.push(value);
        }
      }

      return { ...state, pages: copyMultiAnswers };
    }

    case clientActionTypes.ANSWER_RESET_ACTION: {
      const { targetId, targetPage } = action.payload;
      const pagesToResetAnswer = [...state.pages];
      pagesToResetAnswer[targetPage - 1].map(elem => {
        if (elem.id === targetId) {
          elem.answer = '';
        }
      });
      return { ...state };
    }

    case clientActionTypes.FETCH_ALL_COUNTRIES:
      return { ...state, countries: action.payload };

    case clientActionTypes.ADD_FAILED_VALIDATIONS:
      return { ...state, failedValidations: action.payload };

    case clientActionTypes.CLEAR_FAILED_VALIDATIONS:
      return { ...state, failedValidations: [] };

    case clientActionTypes.CHANGE_PAGE: {
      let newPage = 1;

      if (action.payload === 'increase') {
        newPage = state.currentPage + 1;
      } else {
        newPage = state.currentPage - 1;
      }

      return { ...state, currentPage: newPage };
    }

    default:
      return state;
  }
};
