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

export function* getFacebookPreview(previewData) {
  try {
    const response = yield call(Api.getFacebookPreview, previewData );
    const { status, response: responsePreview } = response;
    if (status && status.isError) {
      yield put({
        type: type.GENERATE_PREVIEW_FAILURE,
        payload: status,
      });
    } else {
      yield put({
        type: type.GENERATE_PREVIEW_SUCCESS,
        payload: responsePreview,
      });
    }
  } catch (error) {
    yield put({
      type: type.GENERATE_PREVIEW_FAILURE,
      payload: error,
    });
  }
}


function* watchImageFacebookPreview() {
  yield takeLatest(type.GENERATE_PREVIEW_REQUEST, getFacebookPreview);
}


export default [
  watchImageFacebookPreview,
];

export function* v2CapFacebookPreviewSagas() {
  yield all([
    watchImageFacebookPreview(),
  ]);
}
