import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "className", "value", "onChange"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

import React from 'react';
import omit from 'lodash.omit';
import Box, { omitBoxProps } from '../box';
import Button from './Button';
import cx from 'classnames';
import isComponentOfType from '../utils/is-component-of-type';
import theme from './theme.css';

var MarketingButtonGroup = function MarketingButtonGroup(_ref) {
  var children = _ref.children,
      className = _ref.className,
      value = _ref.value,
      onChange = _ref.onChange,
      others = _objectWithoutProperties(_ref, _excluded);

  var handleChange = function handleChange(value, event) {
    if (onChange) {
      onChange(value, event);
    }
  };

  var classNames = cx(theme['group'], theme['segmented'], className);
  return /*#__PURE__*/React.createElement(Box, _extends({
    "data-teamleader-ui": "button-group",
    className: classNames
  }, others), React.Children.map(children, function (child) {
    if (! /*#__PURE__*/React.isValidElement(child)) {
      return;
    }

    if (!isComponentOfType(Button, child)) {
      return child;
    }

    var optionalChildProps = {};

    if (value) {
      optionalChildProps = {
        active: child.props.value === value,
        onClick: function onClick(event) {
          return handleChange(child.props.value, event);
        }
      };
    }

    var childProps = omit(child.props, ['value']);
    var groupProps = omit(others, ['onChange']);
    return /*#__PURE__*/React.createElement(child.type, _objectSpread(_objectSpread(_objectSpread({}, childProps), optionalChildProps), omitBoxProps(groupProps)));
  }));
};

MarketingButtonGroup.Button = Button;
export default MarketingButtonGroup;