import axios from 'axios';
import { clientActionTypes } from '../../action-types/client-action.types';

export const getSharedForm = urlSlug => async dispatch => {
  const url = `/form-shares/url/${urlSlug}`;
  try {
    const response = await axios.get(url);
    const { data, success } = response.data;
    if (success) {
      dispatch({
        type: clientActionTypes.GET_SHARED_FORM,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    return error.response;
  }
};

export const submitEntry = details => async () => {
  const url = '/entries/noauth';
  try {
    const response = await axios.post(url, details);
    const { success } = response.data;
    if (success) {
      return response.data;
    }
  } catch (error) {
    return error?.response;
  }
};

/**
 * @description  update answers for each client input field
 * @param {*} value
 * @param {*} index
 * @param {*} activePage
 */
export const enterRegularAnswer = (
  value,
  index,
  activePage
) => async dispatch => {
  dispatch({
    type: clientActionTypes.ENTER_REGULAR_ANSWERS,
    payload: {
      index,
      value,
      activePage,
    },
  });
};

/**
 * @description  update answers for each client input field
 * @param {*} value
 * @param {*} index
 * @param {*} activePage
 */
export const updateMultiAnswer = (
  value,
  index,
  activePage
) => async dispatch => {
  dispatch({
    type: clientActionTypes.UPDATE_MULTI_ANSWER,
    payload: {
      index,
      value,
      activePage,
    },
  });
};

/**
 * @description  updateAddress
 * @param {*} value
 * @param {*} index
 * @param {*} activePage
 */
export const updateAddress = (
  value,
  index,
  activePage,
  label
) => async dispatch => {
  dispatch({
    type: clientActionTypes.UPDATE_ADDRESS_DATA,
    payload: {
      index,
      value,
      activePage,
      label,
    },
  });
};

/**
 * @description: reset answer when a logic is applied
 * @param {*} unique_id
 * @param {*} page
 */
export const answerResetAction = (unique_id, page) => dispatch => {
  dispatch({
    type: clientActionTypes.ANSWER_RESET_ACTION,
    payload: {
      targetId: unique_id,
      targetPage: page,
    },
  });
};

/**
 * @description: fetch all countries
 */
export const fetchCountries = () => async dispatch => {
  const url = '/countries';
  try {
    const response = await axios.get(url);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: clientActionTypes.FETCH_ALL_COUNTRIES,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    return error?.response;
  }
};

/**
 * @description: fetch all countries
 */
export const fetchCountryStates = code => async () => {
  const url = `/countries/${code}/states`;
  try {
    const response = await axios.get(url);
    const { success } = response.data;
    if (success) {
      return response.data;
    }
  } catch (error) {
    return error?.response;
  }
};

export const updateLocationAnswer = (
  value,
  index,
  activePage,
  locationType
) => async dispatch => {
  dispatch({
    type: clientActionTypes.UPDATE_LOCATION_ANSWER,
    payload: {
      index,
      value,
      activePage,
      locationType,
    },
  });
};

export const changePage = direction => dispatch => {
  dispatch({
    type: clientActionTypes.CHANGE_PAGE,
    payload: direction,
  });
};

export const addFailedValidation = elements => dispatch => {
  dispatch({
    type: clientActionTypes.ADD_FAILED_VALIDATIONS,
    payload: elements,
  });
};

export const clearFailedValidation = elements => dispatch => {
  dispatch({
    type: clientActionTypes.CLEAR_FAILED_VALIDATIONS,
    payload: elements,
  });
};
