import { call, put, takeLatest, all } from 'redux-saga/effects';
import * as Api from '../../services/api';
import * as types from './constants';
import { watchForMediaGeneration } from "@capillarytech/cap-ui-library/CapAskAira/CapAiMediaGeneration/saga"
export function* createCustomRow({data, callback}) {
  let errorMsg;
  try {
    const result = yield call(Api.createCustomRow, data);
    const code = result.status ? result.status.code : '';
    if (!result.success || code === 500) {
      errorMsg = result.message;
      yield put({ type: types.CREATE_CUSTOM_ROW_FAILURE, errorMsg });
      callback('failed', code !== 500 ? errorMsg : '');
    } else {
      yield put({ type: types.CREATE_CUSTOM_ROW_SUCCESS, data: result.response, statusCode: code, errorMsg });
      callback('success');
    }
  } catch (error) {
    yield put({ type: types.CREATE_CUSTOM_ROW_FAILURE, error, errorMsg });
  }
}

function* watchCreateCustomRow() {
  yield takeLatest(types.CREATE_CUSTOM_ROW_REQUEST, createCustomRow);
}

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

export function* v2BeeEditionSagas() {
  yield all([
    watchCreateCustomRow(),
  ]);
}
