"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = ContentSwitcher;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _js = require("../../globals/js");

var _excluded = ["children", "className", "selectedIndex", "small", "onChange"];

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var prefix = _js.settings.prefix;
/** ContentSwitcher manipulates the content shown following an exclusive or “either/or” pattern. It is used to toggle between two or more content sections within the same space on screen. Only one section can be shown at a time.
 */

function ContentSwitcher(_ref) {
  var children = _ref.children,
      className = _ref.className,
      selectedIndex = _ref.selectedIndex,
      small = _ref.small,
      onChange = _ref.onChange,
      other = (0, _objectWithoutProperties2.default)(_ref, _excluded);

  var _useState = (0, _react.useState)(selectedIndex),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      selectedIdx = _useState2[0],
      setSelectedIndex = _useState2[1];

  var getChildren = function getChildren(children) {
    return _react.default.Children.map(children, function (child, index) {
      return /*#__PURE__*/_react.default.cloneElement(child, {
        index: index,
        onClick: handleChildChange,
        onKeyDown: handleChildChange,
        selected: index === selectedIdx
      });
    });
  };

  var handleChildChange = function handleChildChange(data) {
    var index = data.index;

    if (selectedIdx !== index) {
      setSelectedIndex(index);
      onChange(data);
    }
  };

  var classes = (0, _classnames.default)("".concat(prefix, "--content-switcher"), className, (0, _defineProperty2.default)({}, "".concat(prefix, "--content-switcher--sm"), small));
  return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({}, other, {
    className: classes
  }), getChildren(children));
}

ContentSwitcher.propTypes = {
  /** Specify the content of your ContentSwitcher */
  children: _propTypes.default.node,

  /** Specify an optional className to be added to your ContentSwitcher */
  className: _propTypes.default.string,

  /** Specify an `onChange` handler that is called whenever the ContentSwitcher changes which item is selected */
  onChange: _propTypes.default.func.isRequired,

  /** Specify a selected index for the initially selected content */
  selectedIndex: _propTypes.default.number
};
ContentSwitcher.defaultProps = {
  selectedIndex: 0
};