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* editTemplate(template) {
  let errorMsg;
  try {
    const result = yield call(Api.createMobilePushTemplate, template);

    if (result.message || result.status.code === 500) {
      errorMsg = result.message;
    }
    yield put( {
      type: types.EDIT_TEMPLATE_SUCCESS, data: result.response, statusCode: result.status ? result.status.code : '', errorMsg,
    });
    yield call(template.callback, { templateId: result?.response?.templateId._id });
  } catch (error) {
    yield put({ type: types.EDIT_TEMPLATE_FAILURE, error, errorMsg });
  }
}

export function* getTemplateDetails(id) {
  try {
    const result = yield call(Api.getTemplateDetails, id);
    // const sidebar = result.response.sidebar;
    yield put({ type: types.GET_TEMPLATE_DETAILS_SUCCESS, data: result.response });
  } catch (error) {
    yield put({ type: types.GET_TEMPLATE_DETAILS_FAILURE, error });
  }
}
export function* uploadAsset(file, assetType, fileParams, mode) {
  try {
    const result = yield call(Api.uploadFile, file, assetType, fileParams, mode);
    yield put({ type: types.UPLOAD_ASSET_SUCCESS, data: result.response.asset, statusCode: result.status ? result.status.code : '' });
  } catch (error) {
    yield put({ type: types.UPLOAD_ASSET_FAILURE, error });
  }
}
export function* getIosCtas(licenseCode) {
  try {
    const result = yield call(Api.getIosCtas, licenseCode);
    yield put({ type: types.GET_IOS_CTAS_SUCCESS, data: result.response.metaEntities.data, statusCode: result.status ? result.status.code : '' });
  } catch (error) {
    yield put({ type: types.GET_IOS_CTAS_FAILURE, error });
  }
}

export function* getMobilepushTemplatesList(params) {
  try {
    const result = yield call(Api.getAllTemplates, {channel: params.channel, queryParams: params.queryParams});
    yield put({ type: types.GET_MOBILEPUSH_TEMPLATES_LIST_SUCCESS, data: result.response.templates, templateData: result.response.mapped});
  } catch (error) {
    yield put({ type: types.GET_MOBILEPUSH_TEMPLATES_LIST_FAILURE, error });
  }
}

export function* fetchWeCrmAccounts(action) {
  try {
    const result = yield call(Api.fetchWeCrmAccounts, action.source);
    yield put({
      type: types.GET_WECRM_ACCOUNTS_SUCCESS,
      data: result.response,
    });
  } catch (error) {
    yield put({
      type: types.GET_WECRM_ACCOUNTS_FAILURE,
      data: error,
    });
  }
}

function* watchFetchWeCrmAccounts() {
  yield takeLatest(types.GET_WECRM_ACCOUNTS_REQUEST, fetchWeCrmAccounts);
}

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

function* watchGetTemplateDetails() {
  yield takeLatest(types.GET_TEMPLATE_DETAILS_REQUEST, getTemplateDetails);
}
function* watchUploadAsset() {
  yield takeLatest(types.UPLOAD_ASSET_REQUEST, uploadAsset);
}
function* watchGetIosCtas() {
  yield takeLatest(types.GET_IOS_CTAS, getIosCtas);
}
function* watchMobilepushTemplatesList() {
  yield takeLatest(types.GET_MOBILEPUSH_TEMPLATES_LIST_REQUEST, getMobilepushTemplatesList);
}

// All sagas to be loaded
export default [
  watchEditTemplate, watchGetTemplateDetails, watchFetchWeCrmAccounts, watchMobilepushTemplatesList,
  watchUploadAsset,
  watchGetIosCtas,
];

export function* v2MobilePushEditSagas() {
  yield all([
    watchEditTemplate(),
    watchGetTemplateDetails(),
    watchFetchWeCrmAccounts(),
    watchMobilepushTemplatesList(),
    watchUploadAsset(),
    watchGetIosCtas(),
  ]);
}
