"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AuthSaga = AuthSaga;
exports.default = void 0;
var _effects = require("redux-saga/effects");
var actions = _interopRequireWildcard(require("../slices"));
var _api = _interopRequireDefault(require("../../api"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
// --------------- LIBRARIES ---------------

function* AuthenticateUserRequest(action) {
  try {
    const response = yield (0, _effects.call)(_api.default.auth.authenticateUser, {
      payload: action.payload.payload,
      environment: action.payload.environment
    });
    if (Number(response.code) === -1) {
      throw new Error("Session Expire");
    } else if (Number(response.code) === 0) {
      throw new Error(response.message);
    }
    yield (0, _effects.put)(actions.authenticateUserSuccess(response));
    action.payload.resolve(response);
  } catch (error) {
    yield (0, _effects.put)(actions.authenticateUserError(error instanceof Error ? error === null || error === void 0 ? void 0 : error.message : "Something went wrong"));
    action.payload.reject(error);
  }
}
function* AuthSaga() {
  yield (0, _effects.all)([(0, _effects.takeEvery)(actions.authenticateUserRequest.type, AuthenticateUserRequest)]);
}
var _default = exports.default = AuthSaga;
//# sourceMappingURL=AuthSaga.js.map