"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.AccordionItemContext = void 0;

var _react = _interopRequireDefault(require("react"));

var _primitives = require("../../primitives");

var _index = require("./index");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const AccordionItemContext = /*#__PURE__*/_react.default.createContext({});

exports.AccordionItemContext = AccordionItemContext;

const AccordionItem = ({
  children,
  index: pIndex = 0,
  isDisabled,
  ...props
}) => {
  const {
    index: cIndex,
    changeHandler
  } = _react.default.useContext(_index.AccordionContext);

  const isOpen = cIndex === null || cIndex === void 0 ? void 0 : cIndex.includes(pIndex);

  const onClose = cb => {
    changeHandler && changeHandler(false, pIndex);
    cb && cb();
  };

  const onOpen = cb => {
    changeHandler && changeHandler(true, pIndex);
    cb && cb();
  };

  const childSetter = () => {
    if (typeof children === 'function') return children({
      isExpanded: isOpen,
      isDisabled
    });
    return children;
  };

  return /*#__PURE__*/_react.default.createElement(AccordionItemContext.Provider, {
    value: {
      isOpen,
      isDisabled,
      onClose,
      onOpen
    }
  }, /*#__PURE__*/_react.default.createElement(_primitives.Box, props, childSetter()));
};

var _default = AccordionItem;
exports.default = _default;
//# sourceMappingURL=AccordionItem.js.map