import React, { useState, useCallback } from "react";
import classnames from "classnames";
import { StyledAccordion } from "./Styles";
export const AccordionContext = /*#__PURE__*/React.createContext(null);
export const Accordion = _ref => {
  let {
    children,
    className,
    allowMultipleExpanded = false,
    allowZeroExpanded = false,
    onChange,
    preExpanded = [],
    disableToggleIcon = false,
    customToggleIcon
  } = _ref;
  const [expandedItems, setExpandedItems] = useState(preExpanded);
  const toggleExpandedCallback = useCallback(id => {
    setExpandedItems(prevExpanded => {
      if (prevExpanded.indexOf(id) > -1) {
        if (!allowZeroExpanded && prevExpanded.length === 1) {
          return prevExpanded;
        }
        return prevExpanded.filter(item => {
          return item !== id;
        });
      } else {
        if (!allowMultipleExpanded) {
          return [id];
        }
        return [...prevExpanded, id];
      }
    });
    if (onChange) {
      onChange(id);
    }
  }, [allowMultipleExpanded, allowZeroExpanded, onChange]);
  return /*#__PURE__*/React.createElement(AccordionContext.Provider, {
    value: {
      expandedItems,
      toggleExpanded: toggleExpandedCallback,
      disableToggleIcon,
      customToggleIcon
    }
  }, /*#__PURE__*/React.createElement(StyledAccordion, {
    className: classnames("accordion", className)
  }, children));
};
Accordion.displayName = "Accordion";
//# sourceMappingURL=Accordion.js.map