"use strict";

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

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

var _antd = require("antd");

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

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

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 TextArea = _antd.Input.TextArea;
var errorMessage = 'Fill the required fields';
var titleDefault = 'Record new exercise';
var formValuesDefault = {
  title: '',
  description: '',
  repetitions: null,
  setsPerSessions: null,
  setsPerDay: null,
  frequencyPerWeek: null
};

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

  var _Form$useForm = _antd.Form.useForm(),
      form = _Form$useForm[0];

  var RangePicker = _antd.DatePicker.RangePicker;
  var contentType = props.contentType;

  var _useSelector = (0, _reactRedux.useSelector)(function (state) {
    return state.rehab.main;
  }),
      objectToSave = _useSelector.objectToSave,
      physioterapist = _useSelector.physioterapist,
      sequence = _useSelector.sequence,
      selectedVideoId = _useSelector.selectedVideoId;

  var _useState = (0, _react.useState)(false),
      error = _useState[0],
      setError = _useState[1];

  var _useState2 = (0, _react.useState)(titleDefault),
      title = _useState2[0],
      setTitle = _useState2[1];

  (0, _react.useEffect)(function () {
    if (contentType === 'general') {
      setTitle('Edit All Videos');
      form.setFieldsValue(objectToSave.settings);
    }

    if (contentType === 'single') {
      setTitle('Edit Video');
      var videoData = objectToSave.videos.find(function (video) {
        return video.rehabLibraryId === selectedVideoId;
      });
      form.setFieldsValue(videoData);
    }

    if (contentType === 'new') {
      setTitle('Record new exercise to library');
      form.setFieldsValue(formValuesDefault);
    }

    return function () {
      setTitle(titleDefault);
      form.setFieldsValue(formValuesDefault);
    };
  }, [form, contentType, props.isModalVisible]);

  var onFinish = function onFinish(data) {
    props.handleOk();
    setError(false);

    if (contentType === 'general') {
      var objectData = {
        description: data === null || data === void 0 ? void 0 : data.description,
        repetitions: data === null || data === void 0 ? void 0 : data.repetitions,
        setsPerSessions: data === null || data === void 0 ? void 0 : data.setsPerSessions,
        setsPerDay: data === null || data === void 0 ? void 0 : data.setsPerDay,
        frequencyPerWeek: data === null || data === void 0 ? void 0 : data.frequencyPerWeek,
        startDate: data === null || data === void 0 ? void 0 : data.exerciseDate[0].format('YYYY-MM-DD HH:mm:ss'),
        finalDate: data === null || data === void 0 ? void 0 : data.exerciseDate[1].format('YYYY-MM-DD HH:mm:ss')
      };
      dispatch((0, _main.updateGeneralSettings)(objectData));
    }

    if (contentType === 'single') {
      var _objectData = {
        rehabLibraryId: selectedVideoId,
        description: data === null || data === void 0 ? void 0 : data.description,
        repetitions: data === null || data === void 0 ? void 0 : data.repetitions,
        setsPerSessions: data === null || data === void 0 ? void 0 : data.setsPerSessions,
        setsPerDay: data === null || data === void 0 ? void 0 : data.setsPerDay,
        frequencyPerWeek: data === null || data === void 0 ? void 0 : data.frequencyPerWeek
      };
      dispatch((0, _main.updateSingleSettings)(_objectData));
    }

    if (contentType === 'new') {
      var _objectData2 = {
        physioterapist: physioterapist,
        title: data.title,
        description: data === null || data === void 0 ? void 0 : data.description,
        repetitions: data === null || data === void 0 ? void 0 : data.repetitions,
        setsPerSessions: data === null || data === void 0 ? void 0 : data.setsPerSessions,
        setsPerDay: data === null || data === void 0 ? void 0 : data.setsPerDay,
        frequencyPerWeek: data === null || data === void 0 ? void 0 : data.frequencyPerWeek,
        visible: true
      };
      dispatch((0, _main.updateNewExercise)(_objectData2));
      dispatch((0, _main.nextSequence)(sequence.next));
    }
  };

  var onFinishFailed = function onFinishFailed(errorInfo) {
    console.error('Failed:', errorInfo);
    setError(true);
  };

  var onValuesChange = function onValuesChange() {
    setError(false);
  };

  return /*#__PURE__*/_react["default"].createElement(_antd.Modal, {
    title: title,
    destroyOnClose: true,
    visible: props.isModalVisible,
    closable: false,
    onCancel: props.handleCancel,
    maskClosable: true,
    footer: null
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "record-form-content"
  }, /*#__PURE__*/_react["default"].createElement(_antd.Form, {
    form: form,
    labelWrap: true,
    layout: "vertical",
    onFinish: onFinish,
    size: "large",
    onFinishFailed: onFinishFailed,
    onValuesChange: onValuesChange // initialValues={formValues}

  }, contentType === 'new' && /*#__PURE__*/_react["default"].createElement(_antd.Form.Item, {
    name: "title",
    rules: [{
      required: contentType === 'new' ? true : false,
      message: ''
    }]
  }, /*#__PURE__*/_react["default"].createElement(_antd.Input, {
    placeholder: "Add rehab exercise title",
    name: 'title'
  })), /*#__PURE__*/_react["default"].createElement(_antd.Form.Item, {
    name: "description"
  }, /*#__PURE__*/_react["default"].createElement(TextArea, {
    name: "description",
    rows: 4,
    placeholder: "Description"
  })), /*#__PURE__*/_react["default"].createElement(_antd.Row, {
    gutter: [16]
  }, /*#__PURE__*/_react["default"].createElement(_antd.Col, {
    span: 12
  }, /*#__PURE__*/_react["default"].createElement(_antd.Form.Item, {
    name: "repetitions",
    rules: [{
      required: true,
      message: ''
    }]
  }, /*#__PURE__*/_react["default"].createElement(_antd.InputNumber, {
    min: 0,
    style: {
      width: '100%'
    },
    placeholder: "Repetitions",
    name: "repetitions"
  }))), /*#__PURE__*/_react["default"].createElement(_antd.Col, {
    span: 12
  }, /*#__PURE__*/_react["default"].createElement(_antd.Form.Item, {
    name: "setsPerSessions",
    rules: [{
      required: true,
      message: ''
    }]
  }, /*#__PURE__*/_react["default"].createElement(_antd.InputNumber, {
    min: 0,
    style: {
      width: '100%'
    },
    placeholder: "Sets per session",
    name: "setsPerSessions"
  }))), /*#__PURE__*/_react["default"].createElement(_antd.Col, {
    span: 12
  }, /*#__PURE__*/_react["default"].createElement(_antd.Form.Item, {
    name: "setsPerDay",
    rules: [{
      required: true,
      message: ''
    }]
  }, /*#__PURE__*/_react["default"].createElement(_antd.InputNumber, {
    min: 0,
    style: {
      width: '100%'
    },
    placeholder: "Sets per day",
    name: "setsPerDay"
  }))), /*#__PURE__*/_react["default"].createElement(_antd.Col, {
    span: 12
  }, /*#__PURE__*/_react["default"].createElement(_antd.Form.Item, {
    name: "frequencyPerWeek",
    rules: [{
      required: true,
      message: ''
    }]
  }, /*#__PURE__*/_react["default"].createElement(_antd.InputNumber, {
    min: 0,
    placeholder: "Sessions per week",
    style: {
      width: '100%'
    },
    name: "frequencyPerWeek"
  })))), contentType === 'general' && /*#__PURE__*/_react["default"].createElement(_antd.Row, null, /*#__PURE__*/_react["default"].createElement(_antd.Col, {
    span: 24
  }, /*#__PURE__*/_react["default"].createElement(_antd.Form.Item, {
    name: "exerciseDate",
    rules: [{
      required: true,
      message: ''
    }]
  }, /*#__PURE__*/_react["default"].createElement(RangePicker, {
    style: {
      width: '100%'
    },
    format: "YYYY-MM-DD"
  })))), /*#__PURE__*/_react["default"].createElement(_antd.Form.Item, {
    className: "save-form-item"
  }, /*#__PURE__*/_react["default"].createElement(_antd.Button, {
    size: "large",
    htmlType: "submit",
    block: true
  }, "SAVE")), error && /*#__PURE__*/_react["default"].createElement("div", {
    className: "error-message"
  }, errorMessage))));
};

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