import _extends from '@babel/runtime/helpers/extends';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import 'core-js/modules/es.array.find-index.js';
import 'core-js/modules/es.array.map.js';
import React__default, { useState } from 'react';
import PropTypes from 'prop-types';
import PageWizard, { childrenPropType } from './PageWizard.js';

var _excluded = ["currentStepId", "children", "onNext", "onBack", "setStep"];
var StatefulPageWizardPropTypes = {
  children: childrenPropType,
  /** Id of current step */
  currentStepId: PropTypes.string,
  /** action when click next button called with no param */
  onNext: PropTypes.func,
  /** action when click back button called with no param */
  onBack: PropTypes.func,
  /** action if the inline wizard is closed or canceled */
  onClose: PropTypes.func.isRequired,
  /** action triggered if the inline wizard has submitted final step */
  onSubmit: PropTypes.func.isRequired,
  i18n: PropTypes.shape({
    /** label to show on the cancel button */
    cancel: PropTypes.string,
    /** label to show on the back button */
    back: PropTypes.string,
    /** label to show on the next button */
    next: PropTypes.string,
    /** label to show on the submit button */
    submit: PropTypes.string,
    /** label to show on the close notification button */
    close: PropTypes.string
  }),
  /** function to go to step when click ProgressIndicator step. */
  setStep: PropTypes.func,
  /** next button disabled */
  nextDisabled: PropTypes.bool,
  /** show progress indicator on finish button */
  sendingData: PropTypes.bool,
  /** Form Error Details */
  error: PropTypes.string,
  /** required callback to clear the error */
  onClearError: PropTypes.func.isRequired,
  /** use sticky footer to show buttons at the bottom */
  hasStickyFooter: PropTypes.bool,
  /** Displays the progress indicator vertically */
  isProgressIndicatorVertical: PropTypes.bool,
  /** Content to render before footer buttons (on left side, in LTR) */
  beforeFooterContent: PropTypes.node,
  /** Content to render after footer buttons (on right side, in LTR) */
  afterFooterContent: PropTypes.node,
  /** Specify whether the progress steps should be split equally in size in the div */
  spaceEqually: PropTypes.bool
};
var defaultProps = {
  children: [],
  nextDisabled: false,
  currentStepId: null,
  i18n: {
    back: 'Back',
    next: 'Next',
    cancel: 'Cancel',
    submit: 'Submit',
    close: 'Close'
  },
  sendingData: false,
  error: null,
  hasStickyFooter: false,
  isProgressIndicatorVertical: true,
  onNext: null,
  onBack: null,
  beforeFooterContent: null,
  afterFooterContent: null,
  setStep: null,
  spaceEqually: false
};
var StatefulPageWizard = function StatefulPageWizard(_ref) {
  var currentStepIdProp = _ref.currentStepId,
    children = _ref.children,
    onNext = _ref.onNext,
    onBack = _ref.onBack,
    _setStep = _ref.setStep,
    other = _objectWithoutProperties(_ref, _excluded);
  var steps = React__default.Children.map(children, function (step) {
    return step.props;
  });
  var _useState = useState(currentStepIdProp || steps && steps[0].id),
    _useState2 = _slicedToArray(_useState, 2),
    currentStepId = _useState2[0],
    setCurrentStepId = _useState2[1];
  var currentStepIndex = steps.findIndex(function (i) {
    return i.id === currentStepId;
  });
  var getNextStep = function getNextStep() {
    var nextStep = steps[currentStepIndex];
    var idx = currentStepIndex + 1;
    /* istanbul ignore else, if current is last, the next button isn't in the DOM, so this cannot be triggered */
    if (idx < steps.length) {
      for (idx; idx < steps.length; idx += 1) {
        if (steps[idx].disabled !== true) {
          nextStep = steps[idx];
          break;
        }
      }
    }
    return nextStep;
  };
  var getPreviousStep = function getPreviousStep() {
    var prevStep = steps[currentStepIndex];
    var idx = currentStepIndex - 1;
    /* istanbul ignore else, if current is first, the previous button isn't in the DOM, so this cannot be triggered */
    if (idx >= 0) {
      for (idx; idx >= 0; idx -= 1) {
        if (steps[idx].disabled !== true) {
          prevStep = steps[idx];
          break;
        }
      }
    }
    return prevStep;
  };
  var handleNext = function handleNext(id) {
    setCurrentStepId(id);
    if (onNext) onNext(id);
  };
  var handleBack = function handleBack(id) {
    setCurrentStepId(id);
    if (onBack) onBack(id);
  };
  var validateSteps = function validateSteps(start, end) {
    var valid = true;
    for (var i = start; i < end; i += 1) {
      valid = steps[i].onValidate() && valid;
    }
    return valid;
  };
  return /*#__PURE__*/React__default.createElement(PageWizard, _extends({}, other, {
    onBack: function onBack() {
      return handleBack(getPreviousStep().id);
    },
    onNext: function onNext() {
      return handleNext(getNextStep().id);
    },
    currentStepId: currentStepId,
    setStep: function setStep(id) {
      var idx = steps.findIndex(function (i) {
        return i.id === id;
      });
      if (idx <= currentStepIndex || validateSteps(currentStepIndex, idx)) {
        setCurrentStepId(id);
        _setStep(id);
      }
    }
  }), children);
};
StatefulPageWizard.propTypes = StatefulPageWizardPropTypes;
StatefulPageWizard.defaultProps = defaultProps;
StatefulPageWizard.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "StatefulPageWizard",
  "props": {
    "children": {
      "defaultValue": {
        "value": "[]",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "custom",
        "raw": "childrenPropType"
      },
      "required": false
    },
    "nextDisabled": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "next button disabled",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "currentStepId": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Id of current step",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "i18n": {
      "defaultValue": {
        "value": "{\n  back: 'Back',\n  next: 'Next',\n  cancel: 'Cancel',\n  submit: 'Submit',\n  close: 'Close',\n}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "cancel": {
            "name": "string",
            "description": "label to show on the cancel button",
            "required": false
          },
          "back": {
            "name": "string",
            "description": "label to show on the back button",
            "required": false
          },
          "next": {
            "name": "string",
            "description": "label to show on the next button",
            "required": false
          },
          "submit": {
            "name": "string",
            "description": "label to show on the submit button",
            "required": false
          },
          "close": {
            "name": "string",
            "description": "label to show on the close notification button",
            "required": false
          }
        }
      },
      "required": false
    },
    "sendingData": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "show progress indicator on finish button",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "error": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Form Error Details",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "hasStickyFooter": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "use sticky footer to show buttons at the bottom",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "isProgressIndicatorVertical": {
      "defaultValue": {
        "value": "true",
        "computed": false
      },
      "description": "Displays the progress indicator vertically",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "onNext": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "action when click next button called with no param",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "onBack": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "action when click back button called with no param",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "beforeFooterContent": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Content to render before footer buttons (on left side, in LTR)",
      "type": {
        "name": "node"
      },
      "required": false
    },
    "afterFooterContent": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Content to render after footer buttons (on right side, in LTR)",
      "type": {
        "name": "node"
      },
      "required": false
    },
    "setStep": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "function to go to step when click ProgressIndicator step.",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "spaceEqually": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "Specify whether the progress steps should be split equally in size in the div",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "onClose": {
      "description": "action if the inline wizard is closed or canceled",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "onSubmit": {
      "description": "action triggered if the inline wizard has submitted final step",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "onClearError": {
      "description": "required callback to clear the error",
      "type": {
        "name": "func"
      },
      "required": true
    }
  }
};
var StatefulPageWizard$1 = StatefulPageWizard;

export { StatefulPageWizard$1 as default };
