"use strict";

exports.__esModule = true;
exports.default = void 0;
var _findIndex2 = _interopRequireDefault(require("lodash/fp/findIndex"));
var _noop2 = _interopRequireDefault(require("lodash/fp/noop"));
var _map2 = _interopRequireDefault(require("lodash/fp/map"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _part = _interopRequireDefault(require("./part"));
var _style = _interopRequireDefault(require("./style.css"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
const themeStyle = {
  setup: _style.default.setupWrapper,
  default: _style.default.wrapper
};
const Tab = ({
  children,
  index,
  title,
  selected,
  iconType,
  onClick,
  href,
  type,
  theme,
  isOpen,
  onUpdateOpenedTab
}) => {
  const handleOnClick = (0, _react.useMemo)(() => evt => onClick(index, evt), [index, onClick]);
  return children ? /*#__PURE__*/_react.default.createElement("div", {
    "data-name": "accordion",
    className: themeStyle[theme]
  }, /*#__PURE__*/_react.default.createElement(_part.default, {
    iconType: iconType,
    title: title,
    content: children,
    selected: selected,
    theme: theme,
    href: href,
    type: type,
    onClick: handleOnClick,
    onUpdateOpenedTab: onUpdateOpenedTab,
    isOpen: isOpen
  })) : /*#__PURE__*/_react.default.createElement("div", {
    "data-name": "accordion",
    className: _style.default.wrapperHidden
  });
};
Tab.propTypes = process.env.NODE_ENV !== "production" ? {
  children: _part.default.propTypes.content,
  index: _propTypes.default.number,
  title: _part.default.propTypes.title,
  iconType: _part.default.propTypes.iconType,
  theme: _part.default.propTypes.theme,
  onClick: _part.default.propTypes.onClick,
  selected: _part.default.propTypes.selected,
  type: _part.default.propTypes.type,
  href: _part.default.propTypes.href,
  isOpen: _part.default.propTypes.isOpen,
  onUpdateOpenedTab: _part.default.propTypes.onUpdateOpenedTab
} : {};
const Accordion = props => {
  const {
    tabProps,
    children,
    theme = 'default',
    onClick = _noop2.default
  } = props;
  const selectedIndex = (0, _findIndex2.default)(item => item.selected, tabProps);
  const [openedTab, updateOpenedTab] = (0, _react.useState)(selectedIndex);
  (0, _react.useEffect)(() => {
    updateOpenedTab(selectedIndex);
  }, [selectedIndex]);
  const tabs = _map2.default.convert({
    cap: false
  })((tab, index) => ({
    ...tab,
    child: children[index],
    children: undefined
  }))(tabProps);
  const accordion = _map2.default.convert({
    cap: false
  })((tab, index) => {
    const isOpen = openedTab === index;
    function onUpdateOpenedTab() {
      updateOpenedTab(isOpen ? -1 : index);
    }
    return /*#__PURE__*/_react.default.createElement(Tab, _extends({}, tab, {
      key: index,
      index: index,
      theme: theme,
      onClick: onClick,
      onUpdateOpenedTab: onUpdateOpenedTab,
      isOpen: isOpen
    }), tab.child);
  }, tabs);
  return /*#__PURE__*/_react.default.createElement("div", null, accordion);
};
Accordion.defaultProps = {
  children: []
};
Accordion.propTypes = process.env.NODE_ENV !== "production" ? {
  children: _propTypes.default.arrayOf(Tab.propTypes.children),
  tabProps: _propTypes.default.arrayOf(_propTypes.default.shape({
    title: Tab.propTypes.title,
    iconType: Tab.propTypes.iconType,
    selected: Tab.propTypes.selected,
    type: Tab.propTypes.type,
    href: Tab.propTypes.href,
    index: Tab.propTypes.index,
    children: Tab.propTypes.children
  })),
  onClick: Tab.propTypes.onClick,
  theme: _propTypes.default.oneOf(['setup', 'default'])
} : {};
var _default = exports.default = Accordion;
//# sourceMappingURL=index.js.map