import _extends from '@babel/runtime/helpers/extends';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
import _createClass from '@babel/runtime/helpers/createClass';
import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstructorReturn';
import _getPrototypeOf from '@babel/runtime/helpers/getPrototypeOf';
import _inherits from '@babel/runtime/helpers/inherits';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import isNil from '../../node_modules/lodash-es/isNil.js';
import 'core-js/modules/es.array.concat.js';
import 'core-js/modules/es.array.map.js';
import 'core-js/modules/es.reflect.construct.js';
import React__default, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import ComposedModal from '../ComposedModal/ComposedModal.js';
import ProgressIndicator from '../ProgressIndicator/ProgressIndicator.js';
import WizardFooter from '../WizardInline/WizardFooter/WizardFooter.js';
import { settings } from '../../constants/Settings.js';

var _excluded = ["steps", "className", "currentStepIndex", "isClickable", "onBack", "testId"];
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var iotPrefix = settings.iotPrefix;

/**
 * Extends ComposedModal to add wizard, refer to that component for common props
 */
var WizardModal = /*#__PURE__*/function (_Component) {
  function WizardModal() {
    var _this;
    _classCallCheck(this, WizardModal);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, WizardModal, [].concat(args));
    _defineProperty(_this, "state", {
      step: _this.props.currentStepIndex
    });
    _defineProperty(_this, "handleNext", function () {
      if (_this.validateCurrentStep()) {
        _this.setState(function (state) {
          return {
            step: state.step + 1
          };
        });
      }
    });
    _defineProperty(_this, "handlePrevious", function () {
      var onBack = _this.props.onBack;
      _this.setState(function (state) {
        return {
          step: state.step - 1
        };
      }, function () {
        if (onBack) {
          onBack(_this.state.step);
        }
      });
    });
    _defineProperty(_this, "handleClick", function (key) {
      // If you're trying to go higher then validate
      var step = _this.state.step;
      var onBack = _this.props.onBack;
      if (key < step || _this.validateCurrentStep()) {
        _this.setState({
          step: key
        }, function () {
          if (onBack && key < step) {
            onBack(key);
          }
        });
      }
    });
    _defineProperty(_this, "validateCurrentStep", function () {
      var steps = _this.props.steps;
      var step = _this.state.step;
      var onValidate = steps[step].onValidate;
      if (onValidate) {
        var isValid = onValidate();
        return isNil(isValid) || isValid;
      }
      return true;
    });
    _defineProperty(_this, "handleSubmit", function () {
      var onSubmit = _this.props.onSubmit; // eslint-disable-line react/prop-types
      if (_this.validateCurrentStep()) {
        onSubmit();
      }
    });
    _defineProperty(_this, "renderFooter", function () {
      var _this$props = _this.props,
        onClose = _this$props.onClose,
        sendingData = _this$props.sendingData,
        _this$props$footer = _this$props.footer,
        leftContent = _this$props$footer.leftContent,
        submitButtonLabel = _this$props$footer.submitButtonLabel,
        cancelButtonLabel = _this$props$footer.cancelButtonLabel,
        nextButtonLabel = _this$props$footer.nextButtonLabel,
        previousButtonLabel = _this$props$footer.previousButtonLabel,
        steps = _this$props.steps,
        testId = _this$props.testId;
      var step = _this.state.step;
      var defaultFooterProps = WizardModal.defaultProps.footer;
      var hasNext = step !== steps.length - 1;
      var hasPrev = step !== 0;
      return /*#__PURE__*/React__default.createElement(WizardFooter, {
        hasPrev: hasPrev,
        hasNext: hasNext,
        onCancel: onClose,
        onBack: _this.handlePrevious,
        onNext: _this.handleNext,
        onSubmit: _this.handleSubmit,
        footerLeftContent: leftContent,
        cancelLabel: cancelButtonLabel || defaultFooterProps.cancelButtonLabel,
        nextLabel: nextButtonLabel || defaultFooterProps.nextButtonLabel,
        backLabel: previousButtonLabel || defaultFooterProps.previousButtonLabel,
        submitLabel: submitButtonLabel || defaultFooterProps.submitButtonLabel,
        sendingData: typeof sendingData === 'boolean' && sendingData || typeof sendingData === 'string',
        className: "".concat(iotPrefix, "--wizard-modal__footer"),
        testId: "".concat(testId, "-footer")
      });
    });
    return _this;
  }
  _inherits(WizardModal, _Component);
  return _createClass(WizardModal, [{
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
        steps = _this$props2.steps,
        className = _this$props2.className;
        _this$props2.currentStepIndex;
        var isClickable = _this$props2.isClickable;
        _this$props2.onBack;
        var testId = _this$props2.testId,
        other = _objectWithoutProperties(_this$props2, _excluded);
      // Transform object to be what Progress Indicator expects
      var items = steps.map(function (step, index) {
        return {
          id: index,
          label: step.label
        };
      });
      var stepIndex = this.state.step;
      return /*#__PURE__*/React__default.createElement(ComposedModal, _extends({}, other, {
        className: classnames("".concat(iotPrefix, "--wizard-modal"), className),
        footer: this.renderFooter()
        // TODO: pass testId in v3 to override defaults.
        // testId={testId}
      }), /*#__PURE__*/React__default.createElement(ProgressIndicator, {
        items: items,
        currentItemId: !isNil(stepIndex) ? items[stepIndex] && items[stepIndex].id : null,
        onClickItem: this.handleClick,
        isClickable: isClickable
        // TODO: pass testId in v3 to override defaults.
        // testId={`${testId}-progress-indicator`}
      }), /*#__PURE__*/React__default.createElement("div", {
        "data-testid": "".concat(testId, "-content"),
        className: "".concat(iotPrefix, "--wizard-modal__content")
      }, steps[stepIndex].content));
    }
  }]);
}(Component);
_defineProperty(WizardModal, "propTypes", {
  /**
   * Header to pass through to Modal
   */
  header: PropTypes.shape({
    label: PropTypes.string,
    title: PropTypes.string.isRequired,
    helpText: PropTypes.string
  }).isRequired,
  /**
   * steps for the wizard to take you through
   *  label: the label of each step up in the progress bar,
   *  content: the wizard page content for each step
   *  onValidate: is the callback called to validate that we can leave this step
   */
  steps: PropTypes.arrayOf(PropTypes.shape({
    label: PropTypes.node.isRequired,
    content: PropTypes.node.isRequired,
    onValidate: PropTypes.func
  })).isRequired,
  /** optionally pass in the current Step */
  currentStepIndex: PropTypes.number,
  /** Make the progress indicator clickable */
  isClickable: PropTypes.bool,
  /** callback when dialog is submitted */
  onSubmit: PropTypes.func.isRequired,
  /** Callback when going to previous step  */
  onBack: PropTypes.func,
  /**
   * leftContent: Anything that will placed to the left of the buttons inside the footer
   * labels: Internationalized string labels for the buttons in the footer
   */
  footer: PropTypes.shape({
    leftContent: PropTypes.node,
    nextButtonLabel: PropTypes.node,
    previousButtonLabel: PropTypes.node,
    submitButtonLabel: PropTypes.node,
    cancelButtonLabel: PropTypes.node
  }),
  testId: PropTypes.string
});
_defineProperty(WizardModal, "defaultProps", {
  currentStepIndex: 0,
  isClickable: false,
  onBack: null,
  footer: {
    leftContent: null,
    nextButtonLabel: 'Next',
    previousButtonLabel: 'Previous',
    submitButtonLabel: 'Submit',
    cancelButtonLabel: 'Cancel'
  },
  testId: 'wizard-modal'
});
WizardModal.__docgenInfo = {
  "description": "Extends ComposedModal to add wizard, refer to that component for common props",
  "methods": [{
    "name": "handleNext",
    "docblock": null,
    "modifiers": [],
    "params": [],
    "returns": null
  }, {
    "name": "handlePrevious",
    "docblock": null,
    "modifiers": [],
    "params": [],
    "returns": null
  }, {
    "name": "handleClick",
    "docblock": null,
    "modifiers": [],
    "params": [{
      "name": "key",
      "type": null
    }],
    "returns": null
  }, {
    "name": "validateCurrentStep",
    "docblock": null,
    "modifiers": [],
    "params": [],
    "returns": null
  }, {
    "name": "handleSubmit",
    "docblock": null,
    "modifiers": [],
    "params": [],
    "returns": null
  }, {
    "name": "renderFooter",
    "docblock": null,
    "modifiers": [],
    "params": [],
    "returns": null
  }],
  "displayName": "WizardModal",
  "props": {
    "currentStepIndex": {
      "defaultValue": {
        "value": "0",
        "computed": false
      },
      "description": "optionally pass in the current Step",
      "type": {
        "name": "number"
      },
      "required": false
    },
    "isClickable": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "Make the progress indicator clickable",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "onBack": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Callback when going to previous step",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "footer": {
      "defaultValue": {
        "value": "{\n  leftContent: null,\n  nextButtonLabel: 'Next',\n  previousButtonLabel: 'Previous',\n  submitButtonLabel: 'Submit',\n  cancelButtonLabel: 'Cancel',\n}",
        "computed": false
      },
      "description": "leftContent: Anything that will placed to the left of the buttons inside the footer\nlabels: Internationalized string labels for the buttons in the footer",
      "type": {
        "name": "shape",
        "value": {
          "leftContent": {
            "name": "node",
            "required": false
          },
          "nextButtonLabel": {
            "name": "node",
            "required": false
          },
          "previousButtonLabel": {
            "name": "node",
            "required": false
          },
          "submitButtonLabel": {
            "name": "node",
            "required": false
          },
          "cancelButtonLabel": {
            "name": "node",
            "required": false
          }
        }
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'wizard-modal'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "header": {
      "description": "Header to pass through to Modal",
      "type": {
        "name": "shape",
        "value": {
          "label": {
            "name": "string",
            "required": false
          },
          "title": {
            "name": "string",
            "required": true
          },
          "helpText": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": true
    },
    "steps": {
      "description": "steps for the wizard to take you through\n label: the label of each step up in the progress bar,\n content: the wizard page content for each step\n onValidate: is the callback called to validate that we can leave this step",
      "type": {
        "name": "arrayOf",
        "value": {
          "name": "shape",
          "value": {
            "label": {
              "name": "node",
              "required": true
            },
            "content": {
              "name": "node",
              "required": true
            },
            "onValidate": {
              "name": "func",
              "required": false
            }
          }
        }
      },
      "required": true
    },
    "onSubmit": {
      "description": "callback when dialog is submitted",
      "type": {
        "name": "func"
      },
      "required": true
    }
  }
};
var WizardModal$1 = WizardModal;

export { WizardModal$1 as default };
