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

export function* createTemplate({ template, callback, meta }) {
  let errorMsg;
  try {
    const result = yield call(Api.createWebPushTemplate, template);
    if (result.status && result.status.code >= 400) {
      errorMsg = result.message;
      throw errorMsg;
    }
    if (result.message) {
      errorMsg = result.message;
    }
    // Merge meta into response if provided
    const responseWithMeta = meta ? { ...result.response, meta } : result.response;
    if (callback) {
      yield call(callback, responseWithMeta);
    }
    yield put({
      type: types.CREATE_TEMPLATE_SUCCESS,
      data: responseWithMeta,
      statusCode: result.status ? result.status.code : '',
      errorMsg,
    });
  } catch (error) {
    yield put({ type: types.CREATE_TEMPLATE_FAILURE, error, errorMsg });
  }
}

export function* clearCreateResponse() {
  yield put({ type: types.CLEAR_CREATE_RESPONSE_SUCCESS });
}

export function* editTemplate({ template, callback }) {
  let errorMsg;
  try {
    const result = yield call(Api.createWebPushTemplate, template);
    if (result.status && result.status.code >= 400) {
      errorMsg = result.message;
      throw errorMsg;
    }
    if (result.message) {
      errorMsg = result.message;
    }
    if (callback) {
      callback(result.response, errorMsg);
    }
    yield put({
      type: types.EDIT_TEMPLATE_SUCCESS,
      data: result.response,
      statusCode: result.status ? result.status.code : '',
      errorMsg,
    });
  } catch (error) {
    yield put({ type: types.EDIT_TEMPLATE_FAILURE, error, errorMsg });
    if (callback) {
      callback(null, errorMsg);
    }
  }
}

export function* clearEditResponse() {
  yield put({ type: types.CLEAR_EDIT_RESPONSE_SUCCESS });
}

export function* uploadWebPushAsset({ file, assetType, fileParams, templateType }) {
  try {
    const result = yield call(Api.uploadFile, {
      file,
      assetType,
      fileParams,
    });
    yield put({
      type: types.UPLOAD_WEBPUSH_ASSET_SUCCESS,
      data: result?.response?.asset,
      statusCode: result?.status?.code || '',
      templateType,
    });
  } catch (error) {
    yield put({ type: types.UPLOAD_WEBPUSH_ASSET_FAILURE, error });
  }
}

export function* watchCreateTemplate() {
  yield takeLatest(types.CREATE_TEMPLATE_REQUEST, createTemplate);
}

export function* watchClearCreateResponse() {
  yield takeLatest(types.CLEAR_CREATE_RESPONSE_REQUEST, clearCreateResponse);
}

export function* watchEditTemplate() {
  yield takeLatest(types.EDIT_TEMPLATE_REQUEST, editTemplate);
}

export function* watchClearEditResponse() {
  yield takeLatest(types.CLEAR_EDIT_RESPONSE_REQUEST, clearEditResponse);
}

export function* watchUploadWebPushAsset() {
  yield takeLatest(
    types.UPLOAD_WEBPUSH_ASSET_REQUEST,
    uploadWebPushAsset,
  );
}

export default function* webPushSagas() {
  yield all([
    watchCreateTemplate(),
    watchClearCreateResponse(),
    watchEditTemplate(),
    watchClearEditResponse(),
    watchUploadWebPushAsset(),
  ]);
}
