import _findIndex from "lodash/fp/findIndex";
import _noop from "lodash/fp/noop";
import _map from "lodash/fp/map";
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); }
import React, { useMemo, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import Part from './part';
import style from './style.css';
const themeStyle = {
  setup: style.setupWrapper,
  default: style.wrapper
};
const Tab = ({
  children,
  index,
  title,
  selected,
  iconType,
  onClick,
  href,
  type,
  theme,
  isOpen,
  onUpdateOpenedTab
}) => {
  const handleOnClick = useMemo(() => evt => onClick(index, evt), [index, onClick]);
  return children ? /*#__PURE__*/React.createElement("div", {
    "data-name": "accordion",
    className: themeStyle[theme]
  }, /*#__PURE__*/React.createElement(Part, {
    iconType: iconType,
    title: title,
    content: children,
    selected: selected,
    theme: theme,
    href: href,
    type: type,
    onClick: handleOnClick,
    onUpdateOpenedTab: onUpdateOpenedTab,
    isOpen: isOpen
  })) : /*#__PURE__*/React.createElement("div", {
    "data-name": "accordion",
    className: style.wrapperHidden
  });
};
Tab.propTypes = process.env.NODE_ENV !== "production" ? {
  children: Part.propTypes.content,
  index: PropTypes.number,
  title: Part.propTypes.title,
  iconType: Part.propTypes.iconType,
  theme: Part.propTypes.theme,
  onClick: Part.propTypes.onClick,
  selected: Part.propTypes.selected,
  type: Part.propTypes.type,
  href: Part.propTypes.href,
  isOpen: Part.propTypes.isOpen,
  onUpdateOpenedTab: Part.propTypes.onUpdateOpenedTab
} : {};
const Accordion = props => {
  const {
    tabProps,
    children,
    theme = 'default',
    onClick = _noop
  } = props;
  const selectedIndex = _findIndex(item => item.selected, tabProps);
  const [openedTab, updateOpenedTab] = useState(selectedIndex);
  useEffect(() => {
    updateOpenedTab(selectedIndex);
  }, [selectedIndex]);
  const tabs = _map.convert({
    cap: false
  })((tab, index) => ({
    ...tab,
    child: children[index],
    children: undefined
  }))(tabProps);
  const accordion = _map.convert({
    cap: false
  })((tab, index) => {
    const isOpen = openedTab === index;
    function onUpdateOpenedTab() {
      updateOpenedTab(isOpen ? -1 : index);
    }
    return /*#__PURE__*/React.createElement(Tab, _extends({}, tab, {
      key: index,
      index: index,
      theme: theme,
      onClick: onClick,
      onUpdateOpenedTab: onUpdateOpenedTab,
      isOpen: isOpen
    }), tab.child);
  }, tabs);
  return /*#__PURE__*/React.createElement("div", null, accordion);
};
Accordion.defaultProps = {
  children: []
};
Accordion.propTypes = process.env.NODE_ENV !== "production" ? {
  children: PropTypes.arrayOf(Tab.propTypes.children),
  tabProps: PropTypes.arrayOf(PropTypes.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.oneOf(['setup', 'default'])
} : {};
export default Accordion;
//# sourceMappingURL=index.js.map