"use strict";

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

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

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

var _TransitionResults = _interopRequireDefault(require("../components/TransitionResults"));

var _TransitionSingleResult = _interopRequireDefault(require("../components/TransitionSingleResult"));

var _TransitionReadySetGo = _interopRequireDefault(require("../components/TransitionReadySetGo"));

var _TransitionOpenning = _interopRequireDefault(require("../components/TransitionOpenning"));

var _TransitionClosing = _interopRequireDefault(require("../components/TransitionClosing"));

var _InstructionalVideo = _interopRequireDefault(require("./InstructionalVideo"));

var _Tutorial = _interopRequireDefault(require("./Tutorial"));

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

var _TransitionCalibration = _interopRequireDefault(require("../components/TransitionCalibration"));

var _main = require("../store/rom/main");

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

// let count = 0;
var Main = function Main(props) {
  var dispatch = (0, _reactRedux.useDispatch)();

  var _useSelector = (0, _reactRedux.useSelector)(function (state) {
    return state.rom.main;
  }),
      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,
      isRepetingExercise = _useSelector.isRepetingExercise,
      bodyPartsNotVisible = _useSelector.bodyPartsNotVisible,
      resultExerciseValidation = _useSelector.resultExerciseValidation,
      metricsData = _useSelector.metricsData,
      incorrectSide = _useSelector.incorrectSide;

  var _useState = (0, _react.useState)(false),
      previousValue = _useState[0],
      setPreviousValue = _useState[1];

  var onNextTransition = function onNextTransition(value) {
    if (value === void 0) {
      value = null;
    }

    dispatch((0, _main.nextTransition)());
  };

  var onRepeatExercise = function onRepeatExercise(value) {
    dispatch((0, _main.repetingExercise)(value));
  };

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

  var onSetIsRepetingExercise = function onSetIsRepetingExercise(value) {
    dispatch((0, _main.setIsRepetingExercise)(value));
  }; // const onToogleSelfieMode = () => {
  //   dispatch(toggleSelfieMode());
  // };


  var onSavePhysicalAssessmentsMetricsToState = function onSavePhysicalAssessmentsMetricsToState() {
    var data = {
      session: session.id,
      identifier: identifier,
      user: user,
      anonymous: true,
      metrics: {
        exercise: currentExercise._id,
        value: pose.angleResult,
        printscreen: (0, _main.getPrintScreen)()
      }
    };
    dispatch((0, _main.setMetricsDataValue)(data));
    dispatch((0, _main.setResultExerciseValidateValue)(data));
  };

  var onSavePhysicalAssessmentsMetrics = function onSavePhysicalAssessmentsMetrics() {
    dispatch((0, _main.savePhysicalAssessmentsMetrics)(metricsData));
  };

  var onWeeklyAssessment = function onWeeklyAssessment() {
    dispatch((0, _main.goToWeeklyAssessment)());
  };

  var callback = function callback() {
    onSavePhysicalAssessmentsMetricsToState();
    onNextTransition();
  };

  (0, _react.useEffect)(function () {
    if (currentTransition === 0) {
      setPreviousValue(false);
    }

    return function () {
      setPreviousValue(false);
    };
  }, [currentTransition]);
  (0, _react.useEffect)(function () {
    if (bodyPartsNotVisible.part1 && bodyPartsNotVisible.part2 && bodyPartsNotVisible.part3) {
      if (!previousValue) {
        setPreviousValue(true); // dispatch(setPlay());

        if (currentTransition === 0) onNextTransition();
      }
    } else {
      if (previousValue) {
        setPreviousValue(false);
        if (currentTransition != 3 && currentTransition != 4 && currentTransition != 5) onRepeatExercise(true); // dispatch(setPause());
      }
    }
  }, [bodyPartsNotVisible]);
  (0, _react.useEffect)(function () {
    if (currentTransition === 4 && (resultExerciseValidation === null || resultExerciseValidation === void 0 ? void 0 : resultExerciseValidation.result) >= (resultExerciseValidation === null || resultExerciseValidation === void 0 ? void 0 : resultExerciseValidation.minAcceptableAngle) && (resultExerciseValidation === null || resultExerciseValidation === void 0 ? void 0 : resultExerciseValidation.result) <= (resultExerciseValidation === null || resultExerciseValidation === void 0 ? void 0 : resultExerciseValidation.acceptableAngle)) {
      onSetIsRepetingExercise(false);
      onSavePhysicalAssessmentsMetrics();
    }

    if (!splash.isOpened && !tutorial.isOpened && !paused && !gallery.isOpened && !assessment.isOpened && exercises.length > 0 && currentTransition === -1) {
      if (!isRepetingExercise) dispatch((0, _main.nextExercise)());
      onNextTransition();
    }
  }, [tutorial, currentTransition, finishedExercises.finished, paused, isRepetingExercise]);
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, tutorial.isOpened && /*#__PURE__*/_react["default"].createElement(_Tutorial["default"], null), splash.isOpened && /*#__PURE__*/_react["default"].createElement(_SplashPage["default"], null), currentTransition === 0 && /*#__PURE__*/_react["default"].createElement(_TransitionCalibration["default"], {
    currentExercise: currentExercise,
    incorrectSide: incorrectSide
  }), (currentTransition === 1 || currentTransition === 2) && /*#__PURE__*/_react["default"].createElement(_InstructionalVideo["default"], {
    onNextTransition: onNextTransition
  }), currentTransition === 2 && /*#__PURE__*/_react["default"].createElement(_TransitionReadySetGo["default"], {
    isPaused: paused,
    callback: callback
  }), currentTransition === 3 && /*#__PURE__*/_react["default"].createElement(_TransitionClosing["default"], {
    isPaused: paused,
    onNextTransition: onNextTransition
  }), currentTransition === 4 && /*#__PURE__*/_react["default"].createElement(_TransitionSingleResult["default"], {
    isPaused: paused,
    onNextTransition: onNextTransition,
    exercise: currentExercise,
    resultsExercises: resultsExercises,
    onRepeatExercise: onRepeatExercise,
    onSetIsRepetingExercise: onSetIsRepetingExercise,
    resultExerciseValidation: resultExerciseValidation,
    onSavePhysicalAssessmentsMetrics: onSavePhysicalAssessmentsMetrics
  }), currentTransition === 5 && /*#__PURE__*/_react["default"].createElement(_TransitionOpenning["default"], {
    isPaused: paused,
    onNextTransition: onNextTransition
  }), finishedExercises.isOpened && finishedExercises.finished && /*#__PURE__*/_react["default"].createElement(_TransitionResults["default"], {
    resultsExercises: resultsExercises,
    onResetResultExercises: onResetResultExercises // onWeeklyAssessment={onWeeklyAssessment}

  }), props.children);
};

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

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