'use strict';

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

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var index = require('../../utils/index.js');
var Accordion_styles = require('./Accordion.styles.js');
var Accordion_tokens = require('./Accordion.tokens.js');
var base = require('./variations/_view/base.js');
var base$1 = require('./variations/_size/base.js');
var index$1 = require('./utils/index.js');

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

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

var _excluded = ["size", "view", "stretching", "defaultActiveEventKey", "children", "disabled", "className", "singleActive", "onChange"];
var accordionRoot = function accordionRoot(Root) {
  return /*#__PURE__*/React.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 = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
    var stretchingClass = Accordion_tokens.classes["".concat(stretching, "Stretching")];
    var _useState = React.useState(defaultActiveEventKey),
      _useState2 = _rollupPluginBabelHelpers.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(_rollupPluginBabelHelpers.toConsumableArray(activeIndex)));
      } else {
        var updatedActiveIndex = activeIndex.filter(function (i) {
          return i !== index;
        });
        setActiveIndex(updatedActiveIndex);
      }
    };
    var childrenArray = React.Children.toArray(children);
    return /*#__PURE__*/React__default.default.createElement(Root, _rollupPluginBabelHelpers.extends({
      ref: outerRootRef,
      size: size,
      view: view,
      className: index.cx(stretchingClass, Accordion_tokens.classes.accordionRoot, className)
    }, rest), index$1.getChildren(childrenArray, activeIndex, view, disabled, updateValue));
  });
};
var accordionConfig = {
  name: 'Accordion',
  tag: 'div',
  layout: accordionRoot,
  base: Accordion_styles.base,
  variations: {
    view: {
      css: base.base
    },
    size: {
      css: base$1.base
    }
  },
  defaults: {
    view: 'default',
    size: 'm'
  }
};

exports.accordionConfig = accordionConfig;
exports.accordionRoot = accordionRoot;
