import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "inline", "small", "vertical", "className", "role", "selectedPage", "onSelectionChange"];
import PropTypes from 'prop-types';
import React, { useState, createRef } from 'react';
import classNames from 'classnames';
import { settings } from '../../globals/js';
var prefix = settings.prefix;
/** Step Navigation provide indications to help users reach their destination from their current position */

export default function StepNavigation(_ref) {
  var _classNames, _classNames2;

  var children = _ref.children,
      inline = _ref.inline,
      small = _ref.small,
      vertical = _ref.vertical,
      className = _ref.className,
      role = _ref.role,
      selectedPage = _ref.selectedPage,
      onSelectionChange = _ref.onSelectionChange,
      other = _objectWithoutProperties(_ref, _excluded);

  var _useState = useState(true),
      _useState2 = _slicedToArray(_useState, 2),
      dropdownHidden = _useState2[0],
      setDropdownHidden = _useState2[1];

  var _useState3 = useState({}),
      _useState4 = _slicedToArray(_useState3, 2),
      elRefs = _useState4[0],
      setElRefs = _useState4[1];

  var getTabAt = function getTabAt(index) {
    return elRefs["tab".concat(index)] || React.Children.toArray(children)[index];
  };

  React.useEffect(function () {
    // add or remove refs
    setElRefs(function (elRefs) {
      return Array(arrLength).fill().map(function (_, i) {
        return elRefs[i] || /*#__PURE__*/createRef();
      });
    });
  }, [arrLength]);

  var getTabs = function getTabs() {
    return React.Children.map(children, function (tab) {
      return tab;
    });
  };

  var arrLength = getTabs().length; // const setTabAt = (index, tabRef) => {
  //   //setElRefs({ ...elRefs, [`tab${index}`]: tabRef });
  // };

  var handleTabClick = function handleTabClick(onSelectionChange) {
    return function (index, label, evt) {
      evt.preventDefault();
      selectTabAt(index, onSelectionChange);
    };
  };

  var handleTabAnchorFocus = function handleTabAnchorFocus(onSelectionChange) {
    return function (index) {
      var tabCount = React.Children.count(children) - 1;
      var tabIndex = index;

      if (index < 0) {
        tabIndex = tabCount;
      } else if (index > tabCount) {
        tabIndex = 0;
      }

      var tab = getTabAt(tabIndex);

      if (tab) {
        selectTabAt(tabIndex, onSelectionChange);

        if (tab.tabAnchor) {
          tab.tabAnchor.focus();
        }
      }
    };
  }; // const handleDropdownClick = () => {
  //   setDropdownHidden(!dropdownHidden);
  // };


  var selectTabAt = function selectTabAt(index, onSelectionChange) {
    handleTabClick(index);

    if (typeof onSelectionChange === 'function') {
      onSelectionChange(index);
    }
  };

  var tabsWithProps = getTabs().map(function (tab, index) {
    var newTab = /*#__PURE__*/React.cloneElement(tab, {
      index: index,
      selectedPage: selectedPage,
      handleTabClick: handleTabClick(onSelectionChange),
      handleTabAnchorFocus: handleTabAnchorFocus(onSelectionChange),
      ref: elRefs[index]
    });
    return newTab;
  });
  var classes = {
    tabs: classNames(className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "--step-navigation"), true), _defineProperty(_classNames, "".concat(prefix, "--step-navigation--vertical"), vertical), _defineProperty(_classNames, "".concat(prefix, "--step-navigation--small"), small), _defineProperty(_classNames, "".concat(prefix, "--step-navigation--regular"), !small), _classNames)),
    tablist: classNames("".concat(prefix, "--step-navigation__nav"), (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefix, "--step-navigation__nav--hidden"), dropdownHidden), _defineProperty(_classNames2, "".concat(prefix, "--step-navigation__nav--inline"), inline), _classNames2))
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("nav", {
    className: classes.tabs,
    role: role
  }, /*#__PURE__*/React.createElement("ul", {
    role: "tablist",
    className: classes.tablist
  }, tabsWithProps)));
}
StepNavigation.defaultProps = {
  role: 'navigation',
  selectedPage: 0
};
StepNavigation.propTypes = {
  /**
   * Pass in a collection of <StepNavigationItem> children to be rendered depending on the
   * currently selected tab
   */
  children: PropTypes.node,

  /**
   * Provide a className that is applied to the root <nav> component for the
   * <Tabs>
   */
  className: PropTypes.string,

  /**
   * Specify whether the StepNavigation will be displayed small
   */
  small: PropTypes.bool,

  /**
   * Specify whether the StepNavigation will be displayed vertically
   */
  vertical: PropTypes.bool,

  /**
   * By default, this value is "navigation". You can also provide an alternate
   * role if it makes sense from the accessibility-side
   */
  role: PropTypes.string.isRequired,

  /**
   * Provide an optional handler that is called whenever the selection
   * changes. This method is called with the index of the tab that was
   * selected
   */
  onSelectionChange: PropTypes.func,

  /**
   * Optionally provide an index for the currently selected <Tab>
   */
  selectedPage: PropTypes.number
};