import {
  takeLatest, all, call, put,
} from 'redux-saga/effects';
import * as Api from '../../services/api';
import * as types from './constants';

export function* getLocizMessage(action) {
  try {
    const result = yield call(Api.getLocizMessage, action.locale);
    yield put({
      type: types.GET_LOCIZ_MESSAGE_SUCCESS,
      data: result,
      locale: action.locale,
    });

  } catch (error) {
    yield put({
      type: types.GET_LOCIZ_MESSAGE_FAILURE,
      data: error,
    });
  }
}
export function* watchGetLocizMessage() {
  yield takeLatest(
    types.GET_LOCIZ_MESSAGE_REQUEST,
    getLocizMessage,
  );
}

export default [watchGetLocizMessage];

export function* LanguageProviderRootSaga() {
  yield all([
    watchGetLocizMessage(),
  ]);
}
