import {
  call,
  put,
  takeLatest
} from "redux-saga/effects";
import {
  LOGIN,
  LOGOUT,
  LOGIN_SUCCESS
} from "../../data/auth/actions.js";
import { authorize, endSession } from "../../utils/auth/index.js";
import { getLogger } from "../../utils/logger.js";
import { logRecords } from "../../utils/log-records.js";
function* login({
  payload
}) {
  try {
    yield call(authorize, payload);
    yield put({ type: LOGIN_SUCCESS });
  } catch (err) {
    getLogger()?.error({
      ...logRecords.LOGIN_FAILED,
      exception: err
    });
  }
}
function* logout({
  payload
}) {
  try {
    yield call(endSession, payload);
  } catch (err) {
    getLogger()?.error({
      ...logRecords.LOGOUT_FAILED,
      exception: err
    });
  }
}
function* authentication() {
  yield takeLatest(LOGIN, login);
  yield takeLatest(LOGOUT, logout);
}
export {
  authentication,
  login,
  logout
};
