"use strict";

exports.__esModule = true;
exports.updatePainScale = exports.selectBodyPoint = exports.savePreAssessments = exports.resetPreAssessment = exports.preAssessment = exports.getPreAssessmentsOptions = exports.getPreAssessments = exports.getLastPreAssessments = exports.deletePreAssessments = exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _toolkit = require("@reduxjs/toolkit");

var _axios = _interopRequireDefault(require("axios"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

var getPreAssessmentsOptions = (0, _toolkit.createAsyncThunk)("getPreAssessmentsOptions", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
  var _yield$axios$get, data;

  return _regenerator["default"].wrap(function _callee$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          _context.prev = 0;
          _context.next = 3;
          return _axios["default"].get("/coach/pre-assessments/options");

        case 3:
          _yield$axios$get = _context.sent;
          data = _yield$axios$get.data;
          return _context.abrupt("return", data);

        case 8:
          _context.prev = 8;
          _context.t0 = _context["catch"](0);
          return _context.abrupt("return", _context.t0.response);

        case 11:
        case "end":
          return _context.stop();
      }
    }
  }, _callee, null, [[0, 8]]);
})));
exports.getPreAssessmentsOptions = getPreAssessmentsOptions;
var getLastPreAssessments = (0, _toolkit.createAsyncThunk)("getLastPreAssessments", /*#__PURE__*/function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regenerator["default"].mark(function _callee2(body) {
    var _yield$axios$get2, data;

    return _regenerator["default"].wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _context2.prev = 0;
            _context2.next = 3;
            return _axios["default"].get("/coach/pre-assessments?identifier=" + body.identifier + "&session=" + body.session);

          case 3:
            _yield$axios$get2 = _context2.sent;
            data = _yield$axios$get2.data;
            return _context2.abrupt("return", data);

          case 8:
            _context2.prev = 8;
            _context2.t0 = _context2["catch"](0);
            return _context2.abrupt("return", _context2.t0.response);

          case 11:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2, null, [[0, 8]]);
  }));

  return function (_x) {
    return _ref2.apply(this, arguments);
  };
}());
exports.getLastPreAssessments = getLastPreAssessments;
var getPreAssessments = (0, _toolkit.createAsyncThunk)("getPreAssessments", /*#__PURE__*/function () {
  var _ref3 = _asyncToGenerator( /*#__PURE__*/_regenerator["default"].mark(function _callee3(body) {
    var identifier, _JSON$parse, id, _yield$axios$get3, data;

    return _regenerator["default"].wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            _context3.prev = 0;
            identifier = localStorage.getItem("identifier");
            _JSON$parse = JSON.parse(localStorage.getItem("session")), id = _JSON$parse.id;
            _context3.next = 5;
            return _axios["default"].get("/coach/pre-assessments?identifier=" + identifier + "&session=" + id);

          case 5:
            _yield$axios$get3 = _context3.sent;
            data = _yield$axios$get3.data;
            return _context3.abrupt("return", data);

          case 10:
            _context3.prev = 10;
            _context3.t0 = _context3["catch"](0);
            return _context3.abrupt("return", _context3.t0.response);

          case 13:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3, null, [[0, 10]]);
  }));

  return function (_x2) {
    return _ref3.apply(this, arguments);
  };
}());
exports.getPreAssessments = getPreAssessments;
var savePreAssessments = (0, _toolkit.createAsyncThunk)("savePreAssessments", /*#__PURE__*/function () {
  var _ref4 = _asyncToGenerator( /*#__PURE__*/_regenerator["default"].mark(function _callee4(body) {
    var _yield$axios$put, _data, _yield$axios$post, data;

    return _regenerator["default"].wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            _context4.prev = 0;

            if (!(body === null || body === void 0 ? void 0 : body._id)) {
              _context4.next = 7;
              break;
            }

            _context4.next = 4;
            return _axios["default"].put("/coach/pre-assessments/" + body._id, body);

          case 4:
            _yield$axios$put = _context4.sent;
            _data = _yield$axios$put.data;
            return _context4.abrupt("return", _data);

          case 7:
            _context4.next = 9;
            return _axios["default"].post("/coach/pre-assessments", body);

          case 9:
            _yield$axios$post = _context4.sent;
            data = _yield$axios$post.data;
            return _context4.abrupt("return", data);

          case 14:
            _context4.prev = 14;
            _context4.t0 = _context4["catch"](0);
            return _context4.abrupt("return", _context4.t0.response);

          case 17:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4, null, [[0, 14]]);
  }));

  return function (_x3) {
    return _ref4.apply(this, arguments);
  };
}());
exports.savePreAssessments = savePreAssessments;
var deletePreAssessments = (0, _toolkit.createAsyncThunk)("deletePreAssessments", /*#__PURE__*/function () {
  var _ref5 = _asyncToGenerator( /*#__PURE__*/_regenerator["default"].mark(function _callee5(_id) {
    var _yield$axios$delete, data;

    return _regenerator["default"].wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            _context5.prev = 0;
            _context5.next = 3;
            return _axios["default"]["delete"]("/coach/pre-assessments/" + _id);

          case 3:
            _yield$axios$delete = _context5.sent;
            data = _yield$axios$delete.data;
            return _context5.abrupt("return", data);

          case 8:
            _context5.prev = 8;
            _context5.t0 = _context5["catch"](0);
            return _context5.abrupt("return", _context5.t0.response);

          case 11:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5, null, [[0, 8]]);
  }));

  return function (_x4) {
    return _ref5.apply(this, arguments);
  };
}());
exports.deletePreAssessments = deletePreAssessments;
var initialState = {
  bodyPoints: [],
  painStart: [],
  painFrequency: [],
  selectedBodyPoint: null,
  bodyPointsSaved: [],
  bodyPointsFromLastSession: [],
  preAssesmentsSaved: 0,
  painScale: {
    whenPainStart: "",
    painCause: "",
    painFrequency: "",
    painLevel: "0",
    relievePainLevel: ""
  }
};
var preAssessment = (0, _toolkit.createSlice)({
  name: "preAssessment",
  initialState: initialState,
  reducers: {
    updatePainScale: function updatePainScale(state, action) {
      var _extends2;

      state.painScale = _extends({}, state.painScale, (_extends2 = {}, _extends2[action.payload.name] = action.payload.value, _extends2));
    },
    selectBodyPoint: function selectBodyPoint(state, action) {
      state.selectedBodyPoint = action.payload || null;
      var bodyPointSaved = state.bodyPointsSaved.find(function (bd) {
        var _action$payload;

        return (bd === null || bd === void 0 ? void 0 : bd.bodyPoint._id) === ((_action$payload = action.payload) === null || _action$payload === void 0 ? void 0 : _action$payload._id);
      });

      if (bodyPointSaved) {
        state.painScale = bodyPointSaved;
      } else {
        state.painScale = _extends({}, initialState.painScale);
      }
    },
    resetPreAssessment: function resetPreAssessment(state, action) {
      state.selectedBodyPoint = initialState.selectedBodyPoint;
      state.bodyPointsSaved = initialState.bodyPointsSaved;
      state.painScale = _extends({}, initialState.painScale);
    }
  },
  extraReducers: function extraReducers(builder) {
    builder.addCase(getPreAssessmentsOptions.fulfilled, function (state, action) {
      state.bodyPoints = [].concat(action.payload.bodyPoints);
      state.painStart = [].concat(action.payload.painStart);
      state.painFrequency = [].concat(action.payload.painFrequency);
    });
    builder.addCase(getLastPreAssessments.fulfilled, function (state, action) {
      state.bodyPointsFromLastSession = action.payload;
    });
    builder.addCase(getPreAssessments.fulfilled, function (state, action) {
      state.bodyPoints.map(function (bd, index) {
        action.payload.forEach(function (element) {
          var _element$bodyPoint;

          if (bd._id === (element === null || element === void 0 ? void 0 : (_element$bodyPoint = element.bodyPoint) === null || _element$bodyPoint === void 0 ? void 0 : _element$bodyPoint._id)) {
            state.bodyPointsSaved[index] = element;
          }
        });
      });
    });
    builder.addCase(savePreAssessments.fulfilled, function (state, action) {
      var bodyPointIndex = state.bodyPoints.findIndex(function (bd) {
        return bd._id === action.payload.bodyPoint;
      });
      if (bodyPointIndex !== -1) state.bodyPointsSaved[bodyPointIndex] = action.payload;
      state.painScale = _extends({}, action.payload);
      state.preAssesmentsSaved = state.preAssesmentsSaved + 1;
    });
    builder.addCase(deletePreAssessments.fulfilled, function (state, action) {
      state.painScale = _extends({}, initialState.painScale);
      var bodyPointIndex = state.bodyPointsSaved.findIndex(function (bd) {
        return bd.bodyPoint === state.selectedBodyPoint._id;
      });

      if (bodyPointIndex !== -1) {
        var list = [].concat(state.bodyPointsSaved);
        list.splice(bodyPointIndex, 1);
        state.bodyPointsSaved = list;
      }
    });
  }
});
exports.preAssessment = preAssessment;
var _preAssessment$action = preAssessment.actions,
    selectBodyPoint = _preAssessment$action.selectBodyPoint,
    updatePainScale = _preAssessment$action.updatePainScale,
    resetPreAssessment = _preAssessment$action.resetPreAssessment;
exports.resetPreAssessment = resetPreAssessment;
exports.updatePainScale = updatePainScale;
exports.selectBodyPoint = selectBodyPoint;
var _default = preAssessment.reducer;
exports["default"] = _default;