import _regeneratorRuntime from "@babel/runtime/regenerator";

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); }); }; }

import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import axios from 'axios';
export var getWeeklyAssessmentsOptions = createAsyncThunk('getWeeklyAssessmentsOptions', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
  var _yield$axios$get, data;

  return _regeneratorRuntime.wrap(function _callee$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          _context.prev = 0;
          _context.next = 3;
          return axios.get('/coach/weekly-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]]);
})));
export var saveWeeklyAssessments = createAsyncThunk('saveWeeklyAssessments', /*#__PURE__*/function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(body) {
    var _yield$axios$post, data;

    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _context2.prev = 0;
            _context2.next = 3;
            return axios.post('/coach/weekly-assessments', body);

          case 3:
            _yield$axios$post = _context2.sent;
            data = _yield$axios$post.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);
  };
}());
var initialState = {
  exercisesFrequency: [],
  exercisesEffort: [],
  painScale: {
    painLevel: [],
    symptoms: '0',
    exerciseFrequency: '',
    exerciseEffort: '',
    healthConcerns: false
  }
};
export var weeklyAssessment = createSlice({
  name: 'weeklyAssessment',
  initialState: initialState,
  reducers: {
    updatePainScale: function updatePainScale(state, action) {
      var _extends2;

      state.painScale = _extends({}, state.painScale, (_extends2 = {}, _extends2[action.payload.name] = action.payload.value, _extends2));
    },
    updateBodyPointsPainLevel: function updateBodyPointsPainLevel(state, action) {
      var _action$payload = action.payload,
          bodyPoint = _action$payload.bodyPoint,
          value = _action$payload.value;
      var bodyPointIndex = state.painScale.painLevel.findIndex(function (obj) {
        return obj.bodyPoint === bodyPoint;
      });
      if (bodyPointIndex > -1) state.painScale.painLevel[bodyPointIndex].value = value;else state.painScale.painLevel.push(_extends({}, action.payload));
    }
  },
  extraReducers: function extraReducers(builder) {
    builder.addCase(getWeeklyAssessmentsOptions.fulfilled, function (state, action) {
      state.exercisesFrequency = [].concat(action.payload.exercisesFrequency);
      state.exercisesEffort = [].concat(action.payload.exercisesEffort);
    });
    builder.addCase(saveWeeklyAssessments.fulfilled, function (state, action) {
      state.painScale = _extends({}, initialState.painScale);
    });
  }
});
var _weeklyAssessment$act = weeklyAssessment.actions,
    updatePainScale = _weeklyAssessment$act.updatePainScale,
    updateBodyPointsPainLevel = _weeklyAssessment$act.updateBodyPointsPainLevel;
export { updatePainScale, updateBodyPointsPainLevel };
export default weeklyAssessment.reducer;