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

// Individual exports for testing
export function* defaultSaga() {
  // See example in v2Containers/HomePage/sagas.js
}
export function* getTemplateDetails({id, channel, callback}) {
  try {
    const params = {
      id, channel,
    };
    
    const result = yield call(Api.getTemplateDetails, params);
    if (callback) {
      yield call(callback, result.response);
    }
    yield put({ type: types.GET_TEMPLATE_DETAILS_SUCCESS, data: result.response });
  } catch (error) {
    yield put({ type: types.GET_TEMPLATE_DETAILS_FAILURE, error });
  }
}
function* watchGetTemplateDetails() {
  yield takeLatest(types.GET_TEMPLATE_DETAILS_REQUEST, getTemplateDetails);
}
// All sagas to be loaded
export default [
  defaultSaga,
  watchGetTemplateDetails,
];

export function* v2WechatRichmediaTemplatesEditSagas() {
  yield all([
    defaultSaga(),
    watchGetTemplateDetails(),
  ]);
}