"use strict";

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

exports.__esModule = true;
exports.WizardStep = void 0;

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

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

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

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

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

var _Popup = require("./popups/Popup");

var _Button = require("./Button");

var _Block = require("./Block");

var _WizardNavbar = require("./WizardNavbar");

var _utils = require("../../utils");

var _classnames = _interopRequireDefault(require("classnames"));

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

  function WizardStep(props, context) {
    var _this;

    _this = _React$Component.call(this, props, context) || this;

    _this.context.wizard.addStepOnLeaveHandler(_this.props.id, _this.onStepLeave.bind((0, _assertThisInitialized2.default)(_this)));

    return _this;
  }

  var _proto = WizardStep.prototype;

  _proto.getDefaultButtons = function getDefaultButtons(footerConfig) {
    if (footerConfig === void 0) {
      footerConfig = {};
    }

    var left;
    var right;
    footerConfig = Object.assign({}, this.props.footerConfig, footerConfig);

    if (!this.context.wizard.isFirstStep(this.props.id)) {
      left = _react.default.createElement(_Button.Button, {
        onClick: this.context.wizard.goBack,
        testAutomationId: "backButton",
        key: "backButton"
      }, (0, _utils.translate)(this.props.backText));
    } else {
      left = _react.default.createElement(_Button.Button, {
        onClick: _Popup.Popup.close,
        testAutomationId: "cancelButton",
        key: "cancelButton"
      }, (0, _utils.translate)(this.props.cancelText));
    }

    right = _react.default.createElement(_Button.Button, {
      onClick: this.context.wizard.goNext,
      disabled: footerConfig.rightBtnDisabled,
      bsStyle: "primary",
      testAutomationId: "nextButton",
      key: "nextButton"
    }, (0, _utils.translate)(this.props.nextText));
    return [left, right];
  };

  _proto.getDefaultNavbar = function getDefaultNavbar() {
    return _react.default.createElement(_WizardNavbar.WizardNavbar, null);
  };

  _proto.getFooter = function getFooter() {
    return this.props.footer || this.getDefaultButtons();
  };

  _proto.getHeader = function getHeader() {
    return this.props.header || this.getDefaultNavbar();
  }
  /**
   * @abstract
   * @returns {Promise.<T>}
   */
  ;

  _proto.onStepLeave = function onStepLeave() {
    return this.props.onStepLeave && this.props.onStepLeave() || Promise.resolve();
  };

  _proto.isAnimated = function isAnimated() {
    return this.context.wizard.getMaxVisitedStepIndex() === 0;
  };

  _proto.render = function render() {
    var _this$props = this.props,
        noTitlePadding = _this$props.noTitlePadding,
        title = _this$props.title;
    var popupTitle = noTitlePadding ? title : _react.default.createElement(_Block.Block, null, title);
    return _react.default.createElement(_Popup.Popup, {
      className: (0, _classnames.default)(this.props.className),
      animation: this.isAnimated(),
      noPadding: this.props.noPadding,
      noBottomPadding: this.props.noBottomPadding,
      onClose: this.props.onClose,
      closeButton: this.props.closeButton,
      bsSize: this.props.bsSize,
      title: popupTitle,
      header: this.getHeader(),
      testAutomationId: this.props.testAutomationId,
      footer: this.getFooter()
    }, this.props.children);
  };

  return WizardStep;
}(_react.default.Component);

exports.WizardStep = WizardStep;
(0, _defineProperty2.default)(WizardStep, "contextTypes", {
  wizard: _propTypes.default.object.isRequired
});
(0, _defineProperty2.default)(WizardStep, "propTypes", {
  children: _propTypes.default.node,
  id: _propTypes.default.string.isRequired,
  title: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.element]).isRequired,
  bsSize: _propTypes.default.string,
  header: _propTypes.default.node,
  footer: _propTypes.default.node,
  backText: _propTypes.default.string,
  cancelText: _propTypes.default.string,
  nextText: _propTypes.default.string,
  tabText: _propTypes.default.string,
  footerConfig: _propTypes.default.shape({
    rightBtnDisabled: _propTypes.default.bool
  }),
  validation: _propTypes.default.func,
  noPadding: _propTypes.default.bool,
  noBottomPadding: _propTypes.default.bool,
  testAutomationId: _propTypes.default.string,
  className: _propTypes.default.string,
  noTitlePadding: _propTypes.default.bool,
  skipNavbarItem: _propTypes.default.bool,
  onStepLeave: _propTypes.default.func,
  onClose: _propTypes.default.func,
  closeButton: _propTypes.default.bool
});
(0, _defineProperty2.default)(WizardStep, "defaultProps", {
  children: null,
  bsSize: 'large',
  testAutomationId: 'wizardstep',
  header: null,
  footer: null,
  footerConfig: null,
  tabText: '',
  backText: 'TRANSLATE:BACK',
  cancelText: 'TRANSLATE:CANCEL',
  nextText: 'TRANSLATE:NEXT',
  className: null,
  noTitlePadding: false,
  skipNavbarItem: false,
  noPadding: false,
  noBottomPadding: false,
  validation: null,
  onStepLeave: null,
  onClose: null,
  closeButton: true
});