"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Stepper = Stepper;
exports.getActiveStepIndex = getActiveStepIndex;
exports.isCompletedStep = isCompletedStep;
exports.isLastInList = isLastInList;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _stepperItem = require("./stepper-item");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function isLastInList(index, listLength) {
  return index === listLength - 1;
}
function getActiveStepIndex(steps, key) {
  return steps.findIndex(step => step.key === key);
}
function isCompletedStep(stepIndex, activeStepIndex) {
  return stepIndex < activeStepIndex;
}

/** Stepper conveys progress through consecutive steps.
 * @uxr-only-supported
 * @midnight-gray-supported
 * @alpine-light-supported
 */
function Stepper({
  steps,
  onClick,
  className,
  isDisabled,
  activeKey,
  testId = 'stepper'
}) {
  const [isActiveStepVisited, setIsActiveStepVisited] = (0, _react.useState)(false);
  const activeStepIndex = getActiveStepIndex(steps, activeKey);
  function handleClick(key) {
    if (key === activeKey) {
      return;
    }
    const nextActiveStepIndex = getActiveStepIndex(steps, key);
    if (activeStepIndex > nextActiveStepIndex) {
      setIsActiveStepVisited(true);
    }
    if (isActiveStepVisited && activeStepIndex < nextActiveStepIndex) {
      setIsActiveStepVisited(false);
    }
    onClick(key);
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("ul", {
    className: (0, _classnames.default)('stepper', className),
    "test-id": testId,
    children: steps.map(({
      key,
      icon,
      label,
      helperText,
      hasErrored,
      ...rest
    }, index) => {
      const isActive = activeKey === key;
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_stepperItem.StepperItem, {
        stepKey: key,
        hasErrored: hasErrored,
        icon: icon,
        label: label,
        helperText: helperText,
        isDisabled: rest.isDisabled || isDisabled,
        isCompleted: index < activeStepIndex,
        onClick: handleClick,
        isRectHidden: isLastInList(index, steps.length),
        isActive: isActive,
        isVisited: isActive && isActiveStepVisited,
        testId: testId
      }, key);
    })
  });
}