"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.AccordionItem = void 0;
var _compat = require("preact/compat");
var _Icons = require("../Icons");
var _classnames = _interopRequireDefault(require("classnames"));
var _useId = _interopRequireDefault(require("../utilities/useId"));
var _jsxRuntime = require("preact/compat/jsx-runtime");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const AccordionItem = _ref => {
  let {
    buttonClassName,
    children,
    contentClassName,
    defaultOpen,
    heading,
    headingLevel,
    id,
    // TODO: Explore deprecating `isControlledOpen` in favor of `isOpen`
    isControlledOpen,
    onChange,
    closeIconComponent,
    openIconComponent
  } = _ref;
  const contentClasses = (0, _classnames.default)('ds-c-accordion__content', contentClassName);
  const buttonClasses = (0, _classnames.default)('ds-c-accordion__button', buttonClassName);
  const HeadingTag = `h${headingLevel}`;
  const isControlled = !!onChange;
  const contentId = (0, _useId.default)('accordion-item--', id);
  const buttonId = `${contentId}__button`;
  const [isOpen, setIsOpen] = (0, _compat.useState)(isControlled ? isControlledOpen : defaultOpen);

  // Set the state for opening and closing an accordion item
  const handleClick = () => {
    if (isControlled) {
      onChange();
    } else {
      setIsOpen(!isOpen);
    }
  };
  const isItemOpen = isControlled ? isControlledOpen : isOpen;
  const CloseIconComponent = closeIconComponent;
  const OpenIconComponent = openIconComponent;
  const closeIcon = /*#__PURE__*/(0, _jsxRuntime.jsx)(CloseIconComponent, {
    className: "ds-c-accordion__button-icon",
    id: `${contentId}__icon`
  });
  const openIcon = /*#__PURE__*/(0, _jsxRuntime.jsx)(OpenIconComponent, {
    className: "ds-c-accordion__button-icon",
    id: `${contentId}__icon`
  });
  if (heading) {
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(HeadingTag, {
        className: "ds-c-accordion__heading",
        children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("button", {
          className: buttonClasses,
          "aria-expanded": isItemOpen,
          "aria-controls": contentId,
          id: buttonId,
          onClick: handleClick,
          type: "button",
          children: [heading, isItemOpen ? closeIcon : openIcon]
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: contentClasses,
        id: contentId,
        hidden: isControlled ? !isControlledOpen : !isOpen,
        children: children
      })]
    });
  }
};
exports.AccordionItem = AccordionItem;
AccordionItem.defaultProps = {
  defaultOpen: false,
  headingLevel: '2',
  closeIconComponent: _Icons.RemoveIcon,
  openIconComponent: _Icons.AddIcon
};
var _default = AccordionItem;
exports.default = _default;