'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
require('core-js/modules/es.symbol.js');
require('core-js/modules/es.symbol.description.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.array.find.js');
require('core-js/modules/es.array.find-index.js');
require('core-js/modules/es.array.map.js');
require('core-js/modules/es.object.get-own-property-descriptor.js');
require('core-js/modules/es.object.get-own-property-descriptors.js');
require('core-js/modules/es.object.keys.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/web.dom-collections.for-each.js');
var React = require('react');
var PropTypes = require('prop-types');
var classnames = require('classnames');
var ProgressIndicator = require('../ProgressIndicator/ProgressIndicator.js');
var Settings = require('../../constants/Settings.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var React__default = /*#__PURE__*/_interopDefault(React);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);
var classnames__default = /*#__PURE__*/_interopDefault(classnames);

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__default.default(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.settings.iotPrefix;
var childrenPropType = PropTypes__default.default.oneOfType([PropTypes__default.default.arrayOf(PropTypes__default.default.node), PropTypes__default.default.node]);
var PageWizardPropTypes = {
  children: childrenPropType,
  /** Id of current step */
  currentStepId: PropTypes__default.default.string,
  /** action when click next button called with no param */
  onNext: PropTypes__default.default.func,
  /** action when click back button called with no param */
  onBack: PropTypes__default.default.func,
  /** action if the inline wizard is closed or canceled */
  onClose: PropTypes__default.default.func.isRequired,
  /** action triggered if the inline wizard has submitted final step */
  onSubmit: PropTypes__default.default.func.isRequired,
  i18n: PropTypes__default.default.shape({
    /** label to show on the cancel button */
    cancel: PropTypes__default.default.string,
    /** label to show on the back button */
    back: PropTypes__default.default.string,
    /** label to show on the next button */
    next: PropTypes__default.default.string,
    /** label to show on the submit button */
    submit: PropTypes__default.default.string,
    /** label to show on the close notification button */
    close: PropTypes__default.default.string
  }),
  /** function to go to step when click ProgressIndicator step. */
  setStep: PropTypes__default.default.func,
  /** next button disabled */
  nextDisabled: PropTypes__default.default.bool,
  /** show progress indicator on finish button */
  sendingData: PropTypes__default.default.bool,
  /** Form Error Details */
  error: PropTypes__default.default.string,
  /** required callback to clear the error */
  onClearError: PropTypes__default.default.func.isRequired,
  /** use sticky footer to show buttons at the bottom */
  hasStickyFooter: PropTypes__default.default.bool,
  /** Displays the progress indicator vertically */
  isProgressIndicatorVertical: PropTypes__default.default.bool,
  /** Content to render before footer buttons (on left side, in LTR) */
  beforeFooterContent: PropTypes__default.default.node,
  /** Content to render after footer buttons (on right side, in LTR) */
  afterFooterContent: PropTypes__default.default.node,
  /** Make the ProgressIndicator clickable */
  isClickable: PropTypes__default.default.bool,
  testId: PropTypes__default.default.string,
  /** Specify whether the progress steps should be split equally in size in the div */
  spaceEqually: PropTypes__default.default.bool,
  stepWidth: PropTypes__default.default.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.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.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.default.createElement("div", {
    "data-testid": testId,
    className: classnames__default.default(isProgressIndicatorVertical ? "".concat(iotPrefix, "--page-wizard") : null, className, hasStickyFooter ? "".concat(iotPrefix, "--page-wizard__sticky") : null)
  }, steps.length > 1 ? /*#__PURE__*/React__default.default.createElement("div", {
    className: isProgressIndicatorVertical ? "".concat(iotPrefix, "--page-wizard--progress--vertical") : "".concat(iotPrefix, "--page-wizard--progress--horizontal")
  }, /*#__PURE__*/React__default.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.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;

exports.PageWizardPropTypes = PageWizardPropTypes;
exports.childrenPropType = childrenPropType;
exports.default = PageWizard$1;
exports.defaultProps = defaultProps;
