import { objectWithoutProperties as _objectWithoutProperties, slicedToArray as _slicedToArray, extends as _extends, toConsumableArray as _toConsumableArray } from '../../_virtual/_rollupPluginBabelHelpers.js';
import React__default, { forwardRef, useState, Children } from 'react';
import { cx } from '../../utils/index.js';
import { base } from './Accordion.styles.js';
import { classes } from './Accordion.tokens.js';
import { base as base$1 } from './variations/_view/base.js';
import { base as base$2 } from './variations/_size/base.js';
import { getChildren } from './utils/index.js';

var _excluded = ["size", "view", "stretching", "defaultActiveEventKey", "children", "disabled", "className", "singleActive", "onChange"];
var accordionRoot = function accordionRoot(Root) {
  return /*#__PURE__*/forwardRef(function (_ref, outerRootRef) {
    var size = _ref.size,
      view = _ref.view,
      _ref$stretching = _ref.stretching,
      stretching = _ref$stretching === void 0 ? 'filled' : _ref$stretching,
      _ref$defaultActiveEve = _ref.defaultActiveEventKey,
      defaultActiveEventKey = _ref$defaultActiveEve === void 0 ? [] : _ref$defaultActiveEve,
      children = _ref.children,
      disabled = _ref.disabled,
      className = _ref.className,
      singleActive = _ref.singleActive,
      onChange = _ref.onChange,
      rest = _objectWithoutProperties(_ref, _excluded);
    var stretchingClass = classes["".concat(stretching, "Stretching")];
    var _useState = useState(defaultActiveEventKey),
      _useState2 = _slicedToArray(_useState, 2),
      activeIndex = _useState2[0],
      setActiveIndex = _useState2[1];
    var updateValue = function updateValue(index, value) {
      if (onChange) {
        onChange(index, value);
      }
      if (singleActive) {
        if (value) {
          setActiveIndex([index]);
        } else {
          setActiveIndex([]);
        }
      } else if (value) {
        setActiveIndex([index].concat(_toConsumableArray(activeIndex)));
      } else {
        var updatedActiveIndex = activeIndex.filter(function (i) {
          return i !== index;
        });
        setActiveIndex(updatedActiveIndex);
      }
    };
    var childrenArray = Children.toArray(children);
    return /*#__PURE__*/React__default.createElement(Root, _extends({
      ref: outerRootRef,
      size: size,
      view: view,
      className: cx(stretchingClass, classes.accordionRoot, className)
    }, rest), getChildren(childrenArray, activeIndex, view, disabled, updateValue));
  });
};
var accordionConfig = {
  name: 'Accordion',
  tag: 'div',
  layout: accordionRoot,
  base: base,
  variations: {
    view: {
      css: base$1
    },
    size: {
      css: base$2
    }
  },
  defaults: {
    view: 'default',
    size: 'm'
  }
};

export { accordionConfig, accordionRoot };
