"use strict";

exports.__esModule = true;
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactRedux = require("react-redux");

var _index = require("./transitions/index");

var _coach = require("../store/coach");

var _SplashPage = _interopRequireDefault(require("./transitions/SplashPage"));

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var Transitions = function Transitions(props) {
  var dispatch = (0, _reactRedux.useDispatch)();

  var _useSelector = (0, _reactRedux.useSelector)(function (state) {
    return {
      user: state.coach.user,
      tutorial: state.coach.tutorial,
      splash: state.coach.splash,
      currentExercise: state.coach.currentExercise,
      currentTransition: state.coach.currentTransition,
      finishedExercises: state.coach.finishedExercises,
      resultsExercises: state.coach.resultsExercises,
      exercises: state.coach.exercises,
      paused: state.coach.paused,
      pose: state.coach.pose,
      identifier: state.coach.identifier,
      session: state.coach.session,
      gallery: state.coach.gallery,
      assessment: state.coach.assessment
    };
  }),
      user = _useSelector.user,
      tutorial = _useSelector.tutorial,
      splash = _useSelector.splash,
      currentExercise = _useSelector.currentExercise,
      currentTransition = _useSelector.currentTransition,
      finishedExercises = _useSelector.finishedExercises,
      resultsExercises = _useSelector.resultsExercises,
      exercises = _useSelector.exercises,
      paused = _useSelector.paused,
      pose = _useSelector.pose,
      identifier = _useSelector.identifier,
      gallery = _useSelector.gallery,
      assessment = _useSelector.assessment,
      session = _useSelector.session;

  var onNextTransition = function onNextTransition() {
    dispatch((0, _coach.nextTransition)());
  };

  var onResetResultExercises = function onResetResultExercises() {
    dispatch((0, _coach.resetResultsExercises)());
  };

  var onSavePhysicalAssessmentsMetrics = function onSavePhysicalAssessmentsMetrics() {
    dispatch((0, _coach.savePhysicalAssessmentsMetrics)({
      session: session.id,
      identifier: identifier,
      user: user,
      anonymous: true,
      metrics: {
        exercise: currentExercise._id,
        value: pose.angleResult,
        printscreen: (0, _coach.getPrintScreen)()
      }
    }));
  }; // useEffect(() => {
  //   if (!tutorial.watched) {
  //     dispatch(watchTutorial(true));
  //   }
  // }, [dispatch, tutorial.watched]);


  (0, _react.useEffect)(function () {
    if (!splash.isOpened && !tutorial.isOpened && !paused && !gallery.isOpened && !assessment.isOpened && currentTransition === -1 && exercises.length > 0) {
      dispatch((0, _coach.nextExercise)());
      onNextTransition();
    }
  }, [onNextTransition, tutorial, currentTransition, finishedExercises.finished, paused]);
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, tutorial.isOpened && /*#__PURE__*/_react["default"].createElement(_index.Tutorial, null), splash.isOpened && /*#__PURE__*/_react["default"].createElement(_SplashPage["default"], {
    onNextTransition: onNextTransition
  }), (currentTransition === 0 || currentTransition === 1) && /*#__PURE__*/_react["default"].createElement(_index.Intro, {
    onNextTransition: onNextTransition
  }), currentTransition === 1 && /*#__PURE__*/_react["default"].createElement(_index.ReadySetGo, {
    isPaused: paused,
    onNextTransition: onNextTransition,
    onSavePhysicalAssessmentsMetrics: onSavePhysicalAssessmentsMetrics
  }), currentTransition === 2 && /*#__PURE__*/_react["default"].createElement(_index.Closing, {
    isPaused: paused,
    onNextTransition: onNextTransition
  }), currentTransition === 3 && /*#__PURE__*/_react["default"].createElement(_index.SingleResult, {
    isPaused: paused,
    onNextTransition: onNextTransition,
    exercise: currentExercise,
    resultsExercises: resultsExercises
  }), currentTransition === 4 && /*#__PURE__*/_react["default"].createElement(_index.Openning, {
    isPaused: paused,
    onNextTransition: onNextTransition
  }), finishedExercises.isOpened && finishedExercises.finished && /*#__PURE__*/_react["default"].createElement(_index.Results, {
    resultsExercises: resultsExercises,
    onResetResultExercises: onResetResultExercises
  }), props.children);
};

var _default = (0, _react.memo)(Transitions);

exports["default"] = _default;
module.exports = exports.default;