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-2019 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 * as React from 'react';
import { KIND, SIZE, SHAPE } from '../button/index.js';
import { getOverrides } from '../helpers/overrides.js';
import { LocaleContext } from '../locale/index.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;
}

export function ButtonGroupRoot(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];

  return React.createElement(Root, _extends({
    "aria-label": props.ariaLabel || (props.locale ? props.locale.ariaLabel : ''),
    "data-baseweb": "button-group"
  }, rootProps), React.Children.map(props.children, function (child, index) {
    if (!React.isValidElement(child)) {
      return null;
    }

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

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

        if (props.onClick) {
          props.onClick(event, index);
        }
      },
      shape: props.shape,
      size: props.size,
      overrides: {
        BaseButton: _objectSpread({
          style: function style() {
            // left most button
            if (index === 0) {
              return {
                borderTopRightRadius: 0,
                borderBottomRightRadius: 0
              };
            } // right most button


            if (index === props.children.length - 1) {
              return {
                borderTopLeftRadius: 0,
                borderBottomLeftRadius: 0
              };
            } // inner button


            return {
              borderTopRightRadius: 0,
              borderBottomRightRadius: 0,
              borderTopLeftRadius: 0,
              borderBottomLeftRadius: 0
            };
          }
        }, child.props.overrides)
      }
    });
  }));
} // The wrapper component below was created to continue to support enzyme tests for the ButtonGroup
// component. Enzyme at the moment does not support React context @ 16.3. To get around the limitation
// in enzyme, we create a wrapper around the core ButtonGroup and pass context as a prop. In our tests,
// only ButtonGroupRoot will be tested.
// https://github.com/airbnb/enzyme/issues/1908#issuecomment-439747826

export default function ButtonGroup(props) {
  return React.createElement(LocaleContext.Consumer, null, function (locale) {
    return React.createElement(ButtonGroupRoot, _extends({}, props, {
      locale: locale.buttongroup
    }));
  });
}
ButtonGroup.defaultProps = {
  disabled: false,
  onClick: function onClick() {},
  shape: SHAPE.default,
  size: SIZE.default
};