"use strict";

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

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

var _index = require("../button/index.js");

var _overrides = require("../helpers/overrides.js");

var _index2 = require("../locale/index.js");

var _styledComponents = require("./styled-components.js");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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; }

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
  };
}

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

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

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

    return _react.default.cloneElement(child, {
      disabled: props.disabled ? true : child.props.disabled,
      isSelected: isSelected(props.selected, index),
      kind: _index.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);
        }
      },
      overrides: _objectSpread({
        BaseButton: {
          style: getBorderRadii(index, props.children.length)
        }
      }, child.props.overrides),
      shape: props.shape,
      size: props.size
    });
  }));
} // 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


function ButtonGroup(props) {
  return _react.default.createElement(_index2.LocaleContext.Consumer, null, function (locale) {
    return _react.default.createElement(ButtonGroupRoot, _extends({}, props, {
      locale: locale.buttongroup
    }));
  });
}

ButtonGroup.defaultProps = {
  disabled: false,
  onClick: function onClick() {},
  shape: _index.SHAPE.default,
  size: _index.SIZE.default
};