'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var clsx = require('clsx');
var React = require('react');
var PromoCardContext = require('./PromoCardContext.js');
var jsxRuntime = require('react/jsx-runtime');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

const PromoCardGroup = ({
  children,
  className,
  defaultChecked = '',
  id,
  isDisabled = false,
  onChange = () => {},
  testId
}) => {
  const [state, setState] = React.useState(defaultChecked);
  const [containerRole, setContainerRole] = React.useState(null);
  /**
   * The context value for the PromoCardGroup.
   *
   * The context value is an object that contains the current state of the
   * group, whether the group is disabled or not, and a function to call when
   * the group value changes. This value is used to provide context to child
   * PromoCard components, allowing them to interact with the group and update
   * its state.
   */
  const contextValue = React.useMemo(() => {
    const handleOnChange = value => {
      setState(value);
      onChange(value);
    };
    return {
      state,
      isDisabled,
      onChange: handleOnChange
    };
  }, [state, isDisabled, onChange]);
  const commonClasses = clsx.clsx({
    'np-CardGroup': true,
    'is-disabled': isDisabled
  }, className);
  const commonProps = {
    className: commonClasses,
    id,
    'data-testid': testId,
    role: containerRole // Add the role attribute here
  };
  React.useEffect(() => {
    setState(defaultChecked);
    // Collect an array of types from the children PromoCard components
    const types = React__default.default.Children.map(children, child => {
      if (/*#__PURE__*/React__default.default.isValidElement(child) && child.props.type) {
        return child.props.type;
      }
      return null;
    })?.filter(type => type !== null && type !== undefined) ?? [];
    // Check if all types are the same
    const allTypesAreTheSame = types.every(type => type === types[0]);
    // If all types are the same and the type is 'radio', set the container role
    setContainerRole(allTypesAreTheSame && types[0] === 'radio' ? 'radiogroup' : null);
  }, [defaultChecked, children]);
  return /*#__PURE__*/jsxRuntime.jsx(PromoCardContext.PromoCardContext.Provider, {
    value: contextValue,
    children: /*#__PURE__*/jsxRuntime.jsx("div", {
      ...commonProps,
      children: children
    })
  });
};
var PromoCardGroup_default = /*#__PURE__*/React__default.default.memo(PromoCardGroup);

exports.default = PromoCardGroup_default;
//# sourceMappingURL=PromoCardGroup.js.map
