function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }
import React, { createContext, useContext } from "react";
import Stack from "../../atoms/stack/stack";

/**
 * ButtonGroupContext interface
 * This interface defines the properties for the ButtonGroupContext
 */

const buttonGroupContext = /*#__PURE__*/createContext({});

/**
 * Hook to get access to the state of a Button group
 */
export const useButtonGroup = () => useContext(buttonGroupContext);

/**
 * ButtonGroupProps interface
 * This interface defines the properties for the ButtonGroup component
 */

/**
 * ButtonGroup is a layout component that makes it easy to stack buttons together and apply a space between them.
 */
const ButtonGroup = /*#__PURE__*/React.memo( /*#__PURE__*/React.forwardRef(({
  children,
  isDisabled = false,
  isAttached = false,
  spacing = "2",
  size = "m",
  variant = "filled",
  colorScheme = "primary",
  ...rest
}, ref) => {
  const arrayChildren = React.Children.toArray(children);

  /**
   * Renders the children of the ButtonGroup.
   *
   * @returns The rendered children.
   */
  const renderChildren = React.useCallback(() => {
    return arrayChildren.map((child, index) => {
      const isFirst = index === 0;
      const isLast = index === arrayChildren.length - 1;
      return /*#__PURE__*/React.cloneElement(child, {
        borderRadius: isAttached && !isFirst && !isLast ? 0 : undefined,
        borderTopRightRadius: isAttached && isFirst ? 0 : undefined,
        borderBottomRightRadius: isAttached && isFirst ? 0 : undefined,
        borderTopLeftRadius: isAttached && isLast ? 0 : undefined,
        borderBottomLeftRadius: isAttached && isLast ? 0 : undefined,
        borderRightWidth: isAttached && !isLast ? 0 : undefined
      });
    });
  }, [arrayChildren, isAttached]);
  return /*#__PURE__*/React.createElement(buttonGroupContext.Provider, {
    value: {
      size: size,
      variant: variant,
      isDisabled: isDisabled,
      colorScheme: colorScheme
    }
  }, /*#__PURE__*/React.createElement(Stack, _extends({
    direction: "horizontal",
    spacing: isAttached ? 0 : spacing,
    ref: ref
  }, rest), renderChildren()));
}));
ButtonGroup.displayName = "ButtonGroup";
export default ButtonGroup;
//# sourceMappingURL=button-group.js.map