"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactFinalForm = require("react-final-form");

var _FormWizard = _interopRequireDefault(require("../../components/FormWizard"));

var _FormControls = _interopRequireDefault(require("../../components/FormControls"));

var _StepNavigation = _interopRequireDefault(require("../../components/StepNavigation"));

var _StepNavigationItem = _interopRequireDefault(require("../../components/StepNavigationItem"));

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

var handleValidation = function handleValidation(props) {
  return null;
};

var Wizardr = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2.default)(Wizardr, _React$Component);

  var _super = _createSuper(Wizardr);

  function Wizardr(props) {
    var _this;

    (0, _classCallCheck2.default)(this, Wizardr);
    _this = _super.call(this, props);
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleTabClick", function (values) {
      return _this.setState(function (state) {
        return {
          page: values,
          values: values
        };
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "next", function (values) {
      return _this.setState(function (state) {
        return {
          page: Math.min(state.page + 1, _this.props.children.length - 1),
          values: values
        };
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "previous", function () {
      return _this.setState(function (state) {
        return {
          page: Math.max(state.page - 1, 0)
        };
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "validate", function (values) {
      var activePage = _react.default.Children.toArray(_this.props.children)[_this.state.page];

      return activePage.props.validate ? activePage.props.validate(values) : {};
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleSubmit", function (values) {
      var _this$props = _this.props,
          children = _this$props.children,
          onSubmit = _this$props.onSubmit;
      var page = _this.state.page;
      var isLastPage = page === _react.default.Children.count(children) - 1;

      if (isLastPage) {
        return onSubmit(values);
      } else {
        _this.next(values);
      }
    });
    _this.state = {
      page: 0,
      values: props.initialValues || {}
    };
    return _this;
  }

  (0, _createClass2.default)(Wizardr, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var children = this.props.children;
      var _this$state = this.state,
          page = _this$state.page,
          values = _this$state.values;

      var activePage = _react.default.Children.toArray(children)[page];

      var isLastPage = page === _react.default.Children.count(children) - 1;
      return /*#__PURE__*/_react.default.createElement(_reactFinalForm.Form, {
        initialValues: values,
        validate: this.validate,
        onSubmit: this.handleSubmit
      }, function (_ref) {
        var handleSubmit = _ref.handleSubmit,
            submitting = _ref.submitting,
            values = _ref.values;
        return /*#__PURE__*/_react.default.createElement("form", {
          onSubmit: handleSubmit
        }, /*#__PURE__*/_react.default.createElement(_FormWizard.default, {
          stickySidebar: true,
          formHeader: "Step: ".concat(page, "/4 ").concat(activePage.props.label),
          formControls: /*#__PURE__*/_react.default.createElement(_FormControls.default, {
            onPreviousClick: _this2.previous,
            previousHidden: page > 0 ? false : true,
            nextHidden: isLastPage,
            submitHidden: !isLastPage,
            onSubmitClick: handleSubmit
          }),
          sidebar: /*#__PURE__*/_react.default.createElement(_StepNavigation.default, {
            selectedPage: page,
            handleTabClick: _this2.handleTabClick
          }, _react.default.Children.map(children, function (child, i) {
            // Ignore the first child
            return /*#__PURE__*/_react.default.createElement(_StepNavigationItem.default, (0, _extends2.default)({
              page: i,
              status: page >= i ? 'complete' : 'not-started'
            }, child.props));
          }))
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "wfp--form-long"
        }, activePage)), /*#__PURE__*/_react.default.createElement(_reactFinalForm.FormSpy, {
          subscription: {
            active: true,
            values: true
          },
          component: handleValidation
        }));
      });
    }
  }]);
  return Wizardr;
}(_react.default.Component);

exports.default = Wizardr;
(0, _defineProperty2.default)(Wizardr, "propTypes", {
  onSubmit: _propTypes.default.func.isRequired
});
(0, _defineProperty2.default)(Wizardr, "Page", function (_ref2) {
  var children = _ref2.children;
  return children;
});