import _defineProperty from '@babel/runtime/helpers/defineProperty';
import 'core-js/modules/es.symbol.js';
import 'core-js/modules/es.symbol.description.js';
import 'core-js/modules/es.array.filter.js';
import 'core-js/modules/es.array.find.js';
import 'core-js/modules/es.array.find-index.js';
import 'core-js/modules/es.array.map.js';
import 'core-js/modules/es.object.get-own-property-descriptor.js';
import 'core-js/modules/es.object.get-own-property-descriptors.js';
import 'core-js/modules/es.object.keys.js';
import 'core-js/modules/es.object.to-string.js';
import 'core-js/modules/web.dom-collections.for-each.js';
import React__default from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import ProgressIndicator from '../ProgressIndicator/ProgressIndicator.js';
import { settings } from '../../constants/Settings.js';

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var iotPrefix = settings.iotPrefix;
var childrenPropType = PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node]);
var PageWizardPropTypes = {
  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,
  /** Make the ProgressIndicator clickable */
  isClickable: PropTypes.bool,
  testId: PropTypes.string,
  /** Specify whether the progress steps should be split equally in size in the div */
  spaceEqually: PropTypes.bool,
  stepWidth: PropTypes.number
};
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,
  setStep: null,
  beforeFooterContent: null,
  afterFooterContent: null,
  isClickable: false,
  testId: 'page-wizard',
  spaceEqually: false,
  stepWidth: null
};
var PageWizard = function PageWizard(_ref) {
  var ch = _ref.children,
    currentStepId = _ref.currentStepId,
    onNext = _ref.onNext,
    onBack = _ref.onBack,
    setStep = _ref.setStep,
    onSubmit = _ref.onSubmit,
    onClose = _ref.onClose,
    i18n = _ref.i18n,
    nextDisabled = _ref.nextDisabled,
    sendingData = _ref.sendingData,
    className = _ref.className,
    error = _ref.error,
    hasStickyFooter = _ref.hasStickyFooter,
    onClearError = _ref.onClearError,
    isProgressIndicatorVertical = _ref.isProgressIndicatorVertical,
    beforeFooterContent = _ref.beforeFooterContent,
    afterFooterContent = _ref.afterFooterContent,
    isClickable = _ref.isClickable,
    testId = _ref.testId,
    spaceEqually = _ref.spaceEqually,
    stepWidth = _ref.stepWidth;
  var children = ch.length ? ch : [ch];
  var steps = React__default.Children.map(children, function (step) {
    return step.props;
  });
  var currentStepIdx = steps.findIndex(function (i) {
    return i.id === currentStepId;
  });
  var hasPrev = currentStepIdx !== 0;
  var hasNext = currentStepIdx !== steps.length - 1;
  var currentStep = children.find(function (i, idx) {
    return idx === currentStepIdx;
  });
  var currentStepToRender = /*#__PURE__*/React__default.cloneElement(currentStep, {
    hasPrev: hasPrev,
    hasNext: hasNext,
    onNext: onNext,
    onBack: onBack,
    onSubmit: onSubmit,
    onClose: onClose,
    i18n: i18n,
    nextDisabled: nextDisabled,
    sendingData: sendingData,
    error: error,
    hasStickyFooter: hasStickyFooter,
    onClearError: onClearError,
    beforeFooterContent: beforeFooterContent,
    afterFooterContent: afterFooterContent
  });

  /**
   * The progress indicator requires an array of items(steps) with substeps passed as children.
   * This function takes all the steps from PageWizardSteps and puts subSteps as children into
   * the corresponding parent step
   * @type {array} steps - The steps array extracted from PageWizardSteps
   * @return {array} newItemsArray - An array with steps items and nested children steps
   */
  var newItemsArray = function newItemsArray() {
    var array = [];
    steps.forEach(function (_ref2) {
      var id = _ref2.id,
        label = _ref2.label,
        secondaryLabel = _ref2.secondaryLabel,
        description = _ref2.description,
        subStep = _ref2.subStep,
        disabled = _ref2.disabled,
        invalid = _ref2.invalid,
        pictogramName = _ref2.pictogramName,
        isComplete = _ref2.isComplete;
      if (!subStep) {
        array.push(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, id && {
          id: id
        }), label && {
          label: label
        }), secondaryLabel && {
          secondaryLabel: secondaryLabel
        }), description && {
          description: description
        }), disabled && {
          disabled: disabled
        }), invalid && {
          invalid: invalid
        }), pictogramName && {
          pictogramName: pictogramName
        }), {}, {
          isComplete: isComplete
        }));
      } else {
        var lastMainStep = array[array.length - 1];
        if (!('children' in lastMainStep)) {
          lastMainStep.children = [];
        }
        lastMainStep.children.push(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, id && {
          id: id
        }), label && {
          label: label
        }), secondaryLabel && {
          secondaryLabel: secondaryLabel
        }), description && {
          description: description
        }), disabled && {
          disabled: disabled
        }), invalid && {
          invalid: invalid
        }), pictogramName && {
          pictogramName: pictogramName
        }), {}, {
          isComplete: isComplete
        }));
      }
    });
    return array;
  };
  return /*#__PURE__*/React__default.createElement("div", {
    "data-testid": testId,
    className: classnames(isProgressIndicatorVertical ? "".concat(iotPrefix, "--page-wizard") : null, className, hasStickyFooter ? "".concat(iotPrefix, "--page-wizard__sticky") : null)
  }, steps.length > 1 ? /*#__PURE__*/React__default.createElement("div", {
    className: isProgressIndicatorVertical ? "".concat(iotPrefix, "--page-wizard--progress--vertical") : "".concat(iotPrefix, "--page-wizard--progress--horizontal")
  }, /*#__PURE__*/React__default.createElement(ProgressIndicator, {
    items: newItemsArray(),
    currentItemId: currentStepId,
    onClickItem: setStep,
    isVerticalMode: isProgressIndicatorVertical,
    isClickable: isClickable,
    spaceEqually: spaceEqually,
    stepWidth: stepWidth
    // TODO: pass down the testId in v3 instead of falling back to the
    // default.
    // testId={`${testId}-progress-indicator`}
  })) : null, /*#__PURE__*/React__default.createElement("div", {
    "data-testid": "".concat(testId, "-content"),
    className: "".concat(iotPrefix, "--page-wizard--content")
  }, currentStepToRender));
};
PageWizard.propTypes = PageWizardPropTypes;
PageWizard.defaultProps = defaultProps;
PageWizard.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "PageWizard",
  "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
    },
    "setStep": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "function to go to step when click ProgressIndicator step.",
      "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
    },
    "isClickable": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "Make the ProgressIndicator clickable",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'page-wizard'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "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
    },
    "stepWidth": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "number"
      },
      "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 PageWizard$1 = PageWizard;

export { PageWizardPropTypes, childrenPropType, PageWizard$1 as default, defaultProps };
