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';
import { v4 } from 'uuid';
export var fetchExercises = createAsyncThunk('performance/fetchExercises', /*#__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/performance/exercises");

        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 saveUserProgress = createAsyncThunk('performance/saveUserProgress', /*#__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/performance/progress', 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 initialPoseConfidence = {
  down: 0,
  up: 0
};
var initialState = {
  user: null,
  identifier: v4(),
  exercises: [],
  exercisesCompleted: [],
  currentExercise: null,
  isMenuOpened: false,
  isHiddenMenu: true,
  progress: {
    total: 0,
    // numero de exercícios
    completed: 0,
    current: 0
  },
  isInvertVideo: false,
  exerciseProgress: {
    total: 0,
    current: 0,
    sets: 0,
    totalSets: 0
  },
  complete: false,
  // poseConfidence: { ...initialPoseConfidence },
  aspectArea: {
    width: 1280,
    height: 720
  },
  shouldRedirect: false
};
export var main = createSlice({
  name: 'main',
  initialState: initialState,
  reducers: {
    resetAll: function resetAll(state, action) {
      state.exerciseProgress = _extends({}, initialState.exerciseProgress);
      state.complete = false;
      state.progress = _extends({}, initialState.progress);
      state.exercisesCompleted = [];
      state.progress.total = state.exercises.length;
      state.identifier = v4();
      state.shouldRedirect = initialState.shouldRedirect;
    },
    updateUser: function updateUser(state, action) {
      state.user = action.payload;
    },
    setNumberOfReps: function setNumberOfReps(state, action) {
      // state.poseConfidence = { ...initialPoseConfidence };
      state.exerciseProgress.current = action.payload;
    },
    setNumberOfSets: function setNumberOfSets(state, action) {
      state.exerciseProgress.current = 0;
      state.exerciseProgress.sets = state.exerciseProgress.sets + 1;
      state.complete = // melhorar aqui
      state.exerciseProgress.sets >= state.currentExercise.setsPerSessions;
    },
    // resetPoseConfidence: (state, action) => {
    //   state.poseConfidence = { ...initialPoseConfidence };
    // },
    // updatePoseConfidence: (state, action) => {
    //   console.log('🚀 ~ file: main.js ~ line 57 ~ action', action.payload);
    //   // aqui pode ser um ponto somar aqui ou no arquivo main.js
    //   state.poseConfidence[action.payload.label] =
    //     state.poseConfidence[action.payload.label] + action.payload.value;
    // },
    toggleMenu: function toggleMenu(state, action) {
      state.isMenuOpened = !state.isMenuOpened;
    },
    goToExercise: function goToExercise(state, action) {
      state.currentExercise = action.payload;
    },
    nextExercise: function nextExercise(state, action) {
      var arr1 = state.exercises.map(function (exer) {
        return exer._id;
      });
      var arr2 = state.exercisesCompleted.map(function (exer) {
        return exer.exercise;
      });
      var diff = arr1.filter(function (x) {
        return !arr2.includes(x);
      });

      if (diff.length > 0) {
        var exerciseIndex = state.exercises.findIndex(function (elm) {
          return state.currentExercise ? elm._id === state.currentExercise : elm._id === diff[0];
        });
        state.currentExercise = _extends({}, state.exercises[exerciseIndex]);
        state.progress.current = arr1.length - diff.length;
        state.exerciseProgress = _extends({}, initialState);
        state.exerciseProgress.total = state.currentExercise.repetitions;
        state.exerciseProgress.totalSets = state.currentExercise.setsPerSessions;
        state.progress.completed = state.progress.current * 100 / state.progress.total;
        return;
      }

      state.currentExercise = null;
    },
    invertVideo: function invertVideo(state, action) {
      state.isInvertVideo = !state.isInvertVideo;
    },
    updateAspectArea: function updateAspectArea(state, action) {
      state.aspectArea = _extends({}, state.aspectArea, action.payload);
    },
    shouldRedirectTo: function shouldRedirectTo(state, action) {
      state.shouldRedirect = true;
    }
  },
  extraReducers: function extraReducers(builder) {
    builder.addCase(fetchExercises.fulfilled, function (state, action) {
      state.exercises = [].concat(action.payload);
      state.currentExercise = state.exercises[0];
      state.exerciseProgress.total = state.currentExercise.repetitions;
      state.exerciseProgress.totalSets = state.currentExercise.setsPerSessions;
      state.exercisesCompleted = [];
      state.isHiddenMenu = state.exercises.length === 0 ? true : false;
      state.progress.total = state.exercises.length;
    });
    builder.addCase(saveUserProgress.fulfilled, function (state, action) {
      console.log('🚀 ~ file: main.js ~ line 140 ~ builder.addCase ~ action', action.payload); // state.complete = true;
    });
  }
});
var _main$actions = main.actions,
    resetAll = _main$actions.resetAll,
    updateUser = _main$actions.updateUser,
    setNumberOfReps = _main$actions.setNumberOfReps,
    setNumberOfSets = _main$actions.setNumberOfSets,
    goToExercise = _main$actions.goToExercise,
    nextExercise = _main$actions.nextExercise,
    toggleMenu = _main$actions.toggleMenu,
    invertVideo = _main$actions.invertVideo,
    updateAspectArea = _main$actions.updateAspectArea,
    shouldRedirectTo = _main$actions.shouldRedirectTo;
export { resetAll, updateUser, setNumberOfReps, setNumberOfSets, goToExercise, nextExercise, toggleMenu, invertVideo, updateAspectArea, shouldRedirectTo };
export default main.reducer;