import { clsx } from 'clsx';
import React__default, { useState, useMemo, useEffect } from 'react';
import { PromoCardContext } from './PromoCardContext.mjs';
import { jsx } from 'react/jsx-runtime';

const PromoCardGroup = ({
  children,
  className,
  defaultChecked = '',
  id,
  isDisabled = false,
  onChange = () => {},
  testId
}) => {
  const [state, setState] = useState(defaultChecked);
  const [containerRole, setContainerRole] = 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 = useMemo(() => {
    const handleOnChange = value => {
      setState(value);
      onChange(value);
    };
    return {
      state,
      isDisabled,
      onChange: handleOnChange
    };
  }, [state, isDisabled, onChange]);
  const commonClasses = clsx({
    'np-CardGroup': true,
    'is-disabled': isDisabled
  }, className);
  const commonProps = {
    className: commonClasses,
    id,
    'data-testid': testId,
    role: containerRole // Add the role attribute here
  };
  useEffect(() => {
    setState(defaultChecked);
    // Collect an array of types from the children PromoCard components
    const types = React__default.Children.map(children, child => {
      if (/*#__PURE__*/React__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__*/jsx(PromoCardContext.Provider, {
    value: contextValue,
    children: /*#__PURE__*/jsx("div", {
      ...commonProps,
      children: children
    })
  });
};
var PromoCardGroup_default = /*#__PURE__*/React__default.memo(PromoCardGroup);

export { PromoCardGroup_default as default };
//# sourceMappingURL=PromoCardGroup.mjs.map
