import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(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; } }

import React from 'react';
import PropTypes from 'prop-types';
import { Form, FormSpy } from 'react-final-form';
import FormWizard from '../../components/FormWizard';
import FormControls from '../../components/FormControls';
import StepNavigation from '../../components/StepNavigation';
import StepNavigationItem from '../../components/StepNavigationItem';

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

var Wizardr = /*#__PURE__*/function (_React$Component) {
  _inherits(Wizardr, _React$Component);

  var _super = _createSuper(Wizardr);

  function Wizardr(props) {
    var _this;

    _classCallCheck(this, Wizardr);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "handleTabClick", function (values) {
      return _this.setState(function (state) {
        return {
          page: values,
          values: values
        };
      });
    });

    _defineProperty(_assertThisInitialized(_this), "next", function (values) {
      return _this.setState(function (state) {
        return {
          page: Math.min(state.page + 1, _this.props.children.length - 1),
          values: values
        };
      });
    });

    _defineProperty(_assertThisInitialized(_this), "previous", function () {
      return _this.setState(function (state) {
        return {
          page: Math.max(state.page - 1, 0)
        };
      });
    });

    _defineProperty(_assertThisInitialized(_this), "validate", function (values) {
      var activePage = React.Children.toArray(_this.props.children)[_this.state.page];

      return activePage.props.validate ? activePage.props.validate(values) : {};
    });

    _defineProperty(_assertThisInitialized(_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.Children.count(children) - 1;

      if (isLastPage) {
        return onSubmit(values);
      } else {
        _this.next(values);
      }
    });

    _this.state = {
      page: 0,
      values: props.initialValues || {}
    };
    return _this;
  }

  _createClass(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.Children.toArray(children)[page];
      var isLastPage = page === React.Children.count(children) - 1;
      return /*#__PURE__*/React.createElement(Form, {
        initialValues: values,
        validate: this.validate,
        onSubmit: this.handleSubmit
      }, function (_ref) {
        var handleSubmit = _ref.handleSubmit,
            submitting = _ref.submitting,
            values = _ref.values;
        return /*#__PURE__*/React.createElement("form", {
          onSubmit: handleSubmit
        }, /*#__PURE__*/React.createElement(FormWizard, {
          stickySidebar: true,
          formHeader: "Step: ".concat(page, "/4 ").concat(activePage.props.label),
          formControls: /*#__PURE__*/React.createElement(FormControls, {
            onPreviousClick: _this2.previous,
            previousHidden: page > 0 ? false : true,
            nextHidden: isLastPage,
            submitHidden: !isLastPage,
            onSubmitClick: handleSubmit
          }),
          sidebar: /*#__PURE__*/React.createElement(StepNavigation, {
            selectedPage: page,
            handleTabClick: _this2.handleTabClick
          }, React.Children.map(children, function (child, i) {
            // Ignore the first child
            return /*#__PURE__*/React.createElement(StepNavigationItem, _extends({
              page: i,
              status: page >= i ? 'complete' : 'not-started'
            }, child.props));
          }))
        }, /*#__PURE__*/React.createElement("div", {
          className: "wfp--form-long"
        }, activePage)), /*#__PURE__*/React.createElement(FormSpy, {
          subscription: {
            active: true,
            values: true
          },
          component: handleValidation
        }));
      });
    }
  }]);

  return Wizardr;
}(React.Component);

_defineProperty(Wizardr, "propTypes", {
  onSubmit: PropTypes.func.isRequired
});

_defineProperty(Wizardr, "Page", function (_ref2) {
  var children = _ref2.children;
  return children;
});

export { Wizardr as default };