"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useButtonGroup = exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _stack = _interopRequireDefault(require("../../atoms/stack/stack"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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); }
/**
 * ButtonGroupContext interface
 * This interface defines the properties for the ButtonGroupContext
 */

const buttonGroupContext = /*#__PURE__*/(0, _react.createContext)({});

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

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

  /**
   * Renders the children of the ButtonGroup.
   *
   * @returns The rendered children.
   */
  const renderChildren = _react.default.useCallback(() => {
    return arrayChildren.map((child, index) => {
      const isFirst = index === 0;
      const isLast = index === arrayChildren.length - 1;
      return /*#__PURE__*/_react.default.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.default.createElement(buttonGroupContext.Provider, {
    value: {
      size: size,
      variant: variant,
      isDisabled: isDisabled,
      colorScheme: colorScheme
    }
  }, /*#__PURE__*/_react.default.createElement(_stack.default, _extends({
    direction: "horizontal",
    spacing: isAttached ? 0 : spacing,
    ref: ref
  }, rest), renderChildren()));
}));
ButtonGroup.displayName = "ButtonGroup";
var _default = exports.default = ButtonGroup;
//# sourceMappingURL=button-group.js.map