"use strict";

require("core-js/modules/web.dom.iterable");

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

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

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

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

var uiActions = _interopRequireWildcard(require("../../actions/ui"));

var _formNavigationButtons = _interopRequireDefault(require("./form-navigation-buttons"));

var _styled = require("./styled");

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
 * This component handles the flow between screens for new OTP user accounts.
 */
class SequentialPaneDisplay extends _react.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "_routeTo", nextId => {
      const {
        parentPath,
        routeTo
      } = this.props;
      routeTo(`${parentPath}/${nextId}`);
    });

    _defineProperty(this, "_handleToNextPane", async e => {
      const {
        activePane
      } = this.props;
      const nextId = activePane.nextId;

      if (nextId) {
        // Don't submit the form if there are more steps to complete.
        e.preventDefault(); // Execute pane-specific action, if any (e.g. save a user account)
        // when clicking next.

        if (typeof activePane.onNext === 'function') {
          await activePane.onNext();
        }

        this._routeTo(nextId);
      }
    });

    _defineProperty(this, "_handleToPrevPane", () => {
      const {
        activePane
      } = this.props;

      this._routeTo(activePane.prevId);
    });
  }

  render() {
    const {
      activePane = {}
    } = this.props;
    const {
      disableNext,
      hideNavigation,
      nextId,
      pane: Pane,
      prevId,
      props,
      title
    } = activePane;
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("h1", null, title), /*#__PURE__*/_react.default.createElement(_styled.SequentialPaneContainer, null, Pane && /*#__PURE__*/_react.default.createElement(Pane, props)), !hideNavigation && /*#__PURE__*/_react.default.createElement(_formNavigationButtons.default, {
      backButton: prevId && {
        onClick: this._handleToPrevPane,
        text: 'Back'
      },
      okayButton: {
        disabled: disableNext,
        onClick: this._handleToNextPane,
        text: nextId ? 'Next' : 'Finish',
        type: 'submit'
      }
    }));
  }

} // connect to the redux store


_defineProperty(SequentialPaneDisplay, "propTypes", {
  activePaneId: _propTypes.default.string.isRequired,
  paneSequence: _propTypes.default.object.isRequired
});

const mapStateToProps = (state, ownProps) => {
  const {
    activePaneId,
    paneSequence
  } = ownProps;
  const {
    pathname
  } = state.router.location;
  return {
    activePane: paneSequence[activePaneId],
    parentPath: pathname.substr(0, pathname.lastIndexOf('/'))
  };
};

const mapDispatchToProps = {
  routeTo: uiActions.routeTo
};

var _default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(SequentialPaneDisplay);

exports.default = _default;
module.exports = exports.default;

//# sourceMappingURL=sequential-pane-display.js