import { all, call, put, takeLatest } from "redux-saga/effects";
import { getAllTechnologies } from "../../request/technologiesRequest";
import { FETCH_ADD_AD_TECHNOLOGIES_REQ } from "../actions/addAdTechnologies/addAdTechnologiesActionConstants";
import { setTechnologiesAddAd } from "../actions/addAdTechnologies/addAdTechnologiesActions";
import { FETCH_TECHNOLOGIES_REQ } from "../actions/technologies/technologiesActionConstants";
import {
  setTechnologies,
  setTechnologiesError,
} from "../actions/technologies/technologiesActions";

export function* getTechnologies() {
  try {
    const result = yield call(getAllTechnologies);
    const resultData = result.data.map((res) => ({ ...res, isChecked: false }));
    yield put(setTechnologies(resultData));
  } catch (error) {
    yield put(setTechnologiesError(error));
  }
}

export function* getTechnologiesAddAd() {
  try {
    const result = yield call(getAllTechnologies);
    const resultData = result.data.map((res) => ({ ...res, isChecked: false }));
    yield put(setTechnologiesAddAd(resultData));
  } catch (error) {
    yield put(setTechnologiesError(error));
  }
}

export default function* technologiesSaga() {
  yield all([
    takeLatest(FETCH_TECHNOLOGIES_REQ, getTechnologies),
    takeLatest(FETCH_ADD_AD_TECHNOLOGIES_REQ, getTechnologiesAddAd),
  ]);
}
