"use strict";

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

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

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

var _VideoRecord = _interopRequireDefault(require("../components/VideoRecord"));

var _ProgressBarUpload = _interopRequireDefault(require("../components/ProgressBarUpload"));

var _RecordConsultForm = _interopRequireDefault(require("./RecordConsultForm"));

var _EvaluationAssessment = _interopRequireDefault(require("./EvaluationAssessment"));

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

var _ResultRecordScreen = _interopRequireDefault(require("./ResultRecordScreen"));

var _EmptyData = _interopRequireDefault(require("../components/EmptyData"));

var _Library = _interopRequireDefault(require("./Library"));

var _main = require("../store/rehab/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; }

var Main = function Main(props) {
  var _useState = (0, _react.useState)(false),
      isUploading = _useState[0],
      setUploading = _useState[1];

  var dispatch = (0, _reactRedux.useDispatch)();

  var _useSelector = (0, _reactRedux.useSelector)(function (state) {
    return state.rehab.main;
  }),
      sequence = _useSelector.sequence,
      recordConsult = _useSelector.recordConsult,
      completed = _useSelector.completed,
      user = _useSelector.user,
      physioterapist = _useSelector.physioterapist,
      isPhysioterapist = _useSelector.isPhysioterapist,
      exercises = _useSelector.exercises,
      currentExercise = _useSelector.currentExercise,
      identifier = _useSelector.identifier,
      isInvertCam = _useSelector.isInvertCam,
      aspectArea = _useSelector.aspectArea,
      users = _useSelector.users,
      recordVideoToLibrary = _useSelector.recordVideoToLibrary;

  (0, _react.useEffect)(function () {
    if (props.rehabAction !== 'record' && (sequence === null || sequence === void 0 ? void 0 : sequence.value) === 'recordConsult') {
      dispatch((0, _main.nextSequence)(sequence.next));
    }
  }, [sequence]);
  (0, _react.useEffect)(function () {
    if (physioterapist) dispatch((0, _main.getVideoFromLibrary)(physioterapist + "?page=1"));
  }, [physioterapist]);

  var onFilterLibrary = function onFilterLibrary(filter, object, page) {
    if (page === void 0) {
      page = 1;
    }

    if (filter === 'default') return dispatch((0, _main.getVideoFromLibrary)(physioterapist + "?page=" + page + "&default=true"));
    if (filter === 'personal') return dispatch((0, _main.getVideoFromLibrary)(physioterapist + "?page=" + page + "&default=false"));
    return dispatch((0, _main.getVideoFromLibrary)(physioterapist + "?page=" + page));
  };

  var onSearchExerciseFromLibrary = function onSearchExerciseFromLibrary(title, page) {
    if (page === void 0) {
      page = 1;
    }

    dispatch((0, _main.getVideoFromLibrary)(physioterapist + "?page=" + page + "&title=" + title));
  };

  var submitPhysioterapistVideo = function submitPhysioterapistVideo(videoBlob) {
    setUploading(true);
    var form = new FormData();
    form.append('video', videoBlob);
    form.append('users', users);
    form.append('physioterapist', physioterapist);
    form.append('title', recordConsult.title);
    form.append('description', recordConsult.description);
    form.append('repetitions', recordConsult.repetitions);
    form.append('setsPerSessions', recordConsult.setsPerSessions);
    form.append('setsPerDay', recordConsult.setsPerDay);
    form.append('frequencyPerWeek', recordConsult.frequencyPerWeek);
    form.append('startDate', recordConsult.startDate);
    form.append('finalDate', recordConsult.finalDate);
    dispatch((0, _main.savePhysioterapistVideo)(form));
  };

  var submitLibraryVideo = function submitLibraryVideo(videoBlob) {
    setUploading(true);
    var form = new FormData();
    form.append('video', videoBlob);
    form.append('physioterapist', physioterapist);
    form.append('title', recordVideoToLibrary.title);
    form.append('description', recordVideoToLibrary.description);
    form.append('repetitions', recordVideoToLibrary.repetitions);
    form.append('setsPerSessions', recordVideoToLibrary.setsPerSessions);
    form.append('setsPerDay', recordVideoToLibrary.setsPerDay);
    form.append('frequencyPerWeek', recordVideoToLibrary.frequencyPerWeek);
    form.append('visible', recordVideoToLibrary.visible);
    dispatch((0, _main.addVideoToLibrary)(form));
  };

  var submitVideoPatient = function submitVideoPatient(videoBlob) {
    setUploading(true);
    var form = new FormData();
    form.append('video', videoBlob);
    form.append('exercise', currentExercise._id);
    form.append('user', user);
    form.append('identifier', identifier);
    dispatch((0, _main.savePatientVideo)(form));
  };

  var toggleHideMenu = function toggleHideMenu(value) {
    dispatch((0, _main.shouldHideMenu)(value));
  };

  (0, _react.useEffect)(function () {
    if (completed && (sequence === null || sequence === void 0 ? void 0 : sequence.next)) setTimeout(function () {
      dispatch((0, _main.nextSequence)(sequence.next));
      setUploading(false);
    }, 1000);
  }, [completed]);
  (0, _react.useEffect)(function () {
    if (!isPhysioterapist && !currentExercise) {
      dispatch((0, _main.nextExercise)());
      setUploading(false);
    }
  }, [currentExercise]);
  var physioterapistRender = (0, _react.useCallback)(function () {
    return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, (sequence === null || sequence === void 0 ? void 0 : sequence.value) === 'library' && /*#__PURE__*/_react["default"].createElement(_Library["default"], {
      onFilterLibrary: onFilterLibrary,
      onSearchExerciseFromLibrary: onSearchExerciseFromLibrary
    }), isUploading && /*#__PURE__*/_react["default"].createElement(_ProgressBarUpload["default"], null), (sequence === null || sequence === void 0 ? void 0 : sequence.value) === 'recordConsult' && props.rehabAction === 'record' && /*#__PURE__*/_react["default"].createElement(_RecordConsultForm["default"], null), (sequence === null || sequence === void 0 ? void 0 : sequence.value) === 'recordVideo' && props.rehabAction === 'record' && /*#__PURE__*/_react["default"].createElement(_VideoRecord["default"], {
      isInvertCam: isInvertCam,
      onFinish: submitPhysioterapistVideo,
      aspectArea: aspectArea
    }), (sequence === null || sequence === void 0 ? void 0 : sequence.value) === 'recordVideo' && props.rehabAction === 'library' && /*#__PURE__*/_react["default"].createElement(_VideoRecord["default"], {
      isInvertCam: isInvertCam,
      onFinish: submitLibraryVideo,
      aspectArea: aspectArea
    }), (sequence === null || sequence === void 0 ? void 0 : sequence.value) === 'resultRecordScreen' && /*#__PURE__*/_react["default"].createElement(_ResultRecordScreen["default"], null));
  });
  var patientRender = (0, _react.useCallback)(function () {
    return exercises.length === 0 ? /*#__PURE__*/_react["default"].createElement(_EmptyData["default"], null) : /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, isUploading && /*#__PURE__*/_react["default"].createElement(_ProgressBarUpload["default"], null), (sequence === null || sequence === void 0 ? void 0 : sequence.value) === 'recordVideo' && exercises.length > 0 && /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_VideoRecord["default"], {
      onFinish: submitVideoPatient,
      onRecording: toggleHideMenu,
      isInvertCam: isInvertCam,
      aspectArea: aspectArea
    }), /*#__PURE__*/_react["default"].createElement(_InstructionalVideo["default"], null)), (sequence === null || sequence === void 0 ? void 0 : sequence.value) === 'evaluationAssessment' && /*#__PURE__*/_react["default"].createElement(_EvaluationAssessment["default"], null));
  });
  return isPhysioterapist ? physioterapistRender() : patientRender();
};

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

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