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

// Individual exports for testing
export function* createTemplate(template) {
  let errorMsg;
  try {
    const result = yield call(Api.createTemplate, template);
    // const sidebar = result.response.sidebar;
    if (result.message || result.status.code === 500) {
      errorMsg = result.message;
    }

    yield put({ type: types.CREATE_TEMPLATE_SUCCESS, data: result.response, statusCode: result.status ? result.status.code : '', errorMsg });
    yield template.onCreateTemplateComplete(result.response);
  } catch (error) {
    yield put({ type: types.CREATE_TEMPLATE_FAILURE, error, errorMsg });
  }
}


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

// All sagas to be loaded
export default [
  watchCreateTemplate,
];

export function* v2SmsCreateSagas() {
  yield all([
    watchCreateTemplate(),
  ]);
}