function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

/*
Copyright (c) 2018 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import React from 'react';
import { KIND, SIZE, SHAPE } from '../button/index.js';
import { getOverrides } from '../helpers/overrides.js';
import { StyledRoot } from './styled-components.js';

function isSelected(selected, index) {
  if (!Array.isArray(selected) && typeof selected !== 'number') {
    return false;
  }

  if (Array.isArray(selected)) {
    return selected.includes(index);
  }

  return selected === index;
}

function getBorderRadii(index, length) {
  if (index === 0) {
    return {
      borderTopRightRadius: 0,
      borderBottomRightRadius: 0
    };
  }

  if (index === length - 1) {
    return {
      borderTopLeftRadius: 0,
      borderBottomLeftRadius: 0
    };
  }

  return {
    borderTopRightRadius: 0,
    borderBottomRightRadius: 0,
    borderTopLeftRadius: 0,
    borderBottomLeftRadius: 0
  };
}

export default function ButtonGroup(props) {
  var _props$overrides = props.overrides,
      overrides = _props$overrides === void 0 ? {} : _props$overrides;

  var _getOverrides = getOverrides(overrides.Root, StyledRoot),
      _getOverrides2 = _slicedToArray(_getOverrides, 2),
      Root = _getOverrides2[0],
      rootProps = _getOverrides2[1];

  if (process.env.NODE_ENV !== "production") {
    if (props.kind !== KIND.tertiary) {
      // eslint-disable-next-line no-console
      console.warn("ButtonGroup kind prop will no longer be supported in future major versions of base ui\n         because only a single color variant is supported in designs. Please update your code\n         to remove the kind prop.");
    }
  }

  return React.createElement(Root, _extends({
    "aria-label": props.ariaLabel
  }, rootProps), React.Children.map(props.children, function (child, index) {
    if (!React.isValidElement(child)) {
      return null;
    }

    return React.cloneElement(child, {
      disabled: props.disabled ? true : child.props.disabled,
      isSelected: isSelected(props.selected, index),
      kind: props.kind,
      onClick: function onClick(event) {
        if (props.disabled) {
          return;
        }

        if (child.props.onClick) {
          child.props.onClick(event);
        }

        if (props.onClick) {
          props.onClick(event, index);
        }
      },
      overrides: _objectSpread({
        BaseButton: {
          style: getBorderRadii(index, props.children.length)
        }
      }, child.props.overrides),
      shape: props.shape,
      size: props.size
    });
  }));
}
ButtonGroup.defaultProps = {
  ariaLabel: 'button group',
  disabled: false,
  kind: KIND.tertiary,
  onClick: function onClick() {},
  shape: SHAPE.default,
  size: SIZE.default
};