"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AccordionTab = void 0;
var _react = _interopRequireDefault(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _tooltip = require("../tooltip");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const areThereEnabledSubOptions = subOptions => subOptions.some(subOption => subOption.isShown !== false && !subOption.isDisabled);
const areThereShownSubOptions = subOptions => subOptions.some(subOption => subOption.isShown !== false);
const getAccordionItem = (tab, i, selectedTabKey, onClick, NavLink, className = 'accordion-tab small-header') => {
  if (tab.route && NavLink) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(NavLink, {
      route: tab.route,
      altRoute: tab.altRoute,
      routeParams: tab.routeParams,
      testId: tab.testId,
      tourId: tab.tourId,
      onClick: () => onClick(tab, i),
      isDisabled: tab.isDisabled,
      className: className,
      ...(selectedTabKey && {
        isActive: tab.key === selectedTabKey
      }),
      onRedirect: tab.onRedirect,
      children: tab.value
    }, tab.key);
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
    "test-id": tab.testId,
    "tour-id": tab.tourId,
    onClick: () => onClick(tab, i),
    className: (0, _classnames.default)(className, {
      active: tab.key === selectedTabKey,
      disabled: tab.isDisabled
    }),
    children: tab.value
  }, tab.key);
};
const getAccordionTabSubOptionsTooltipContent = (
// eslint-disable-next-line @typescript-eslint/default-param-last
subOptions = [], selectedTabKey, onClick, NavLink) => {
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: "accordion-tab-suboptions",
    "test-id": "accordion-tab-suboptions",
    children: subOptions.map((option, i) => {
      if (option.isShown === false) {
        return null;
      }
      return getAccordionItem(option, i, selectedTabKey, onClick, NavLink, 'accordion-tab-suboption');
    })
  });
};
const onClickHandler = (selectedTab, i, selectedTabKey, callback) => {
  if (selectedTab.isDisabled || selectedTab.key === selectedTabKey) {
    return;
  }
  return callback(selectedTab, i);
};
const AccordionTab = ({
  tab,
  index,
  selectedTabKey = '',
  onSelect = () => {},
  onSelectSubOptions = () => {},
  NavLink
}) => {
  let tabContent;
  let tooltipContent;
  let isShown;
  const hasInteractiveTooltip = Boolean(tab.subOptions);
  if (hasInteractiveTooltip) {
    isShown = tab.isShown !== undefined ? tab.isShown : areThereShownSubOptions(tab.subOptions);
    const isDisabled = !!tab.isDisabled || !areThereEnabledSubOptions(tab.subOptions);
    tabContent = getAccordionItem({
      ...tab,
      isDisabled,
      isShown
    }, index, selectedTabKey, (selectedTab, i) => onClickHandler(selectedTab, i, selectedTabKey, onSelect), NavLink);
    tooltipContent = !isDisabled ? getAccordionTabSubOptionsTooltipContent(tab.subOptions, selectedTabKey, (selectedTab, i) => onClickHandler(selectedTab, i, selectedTabKey, onSelectSubOptions), NavLink) : tab.tooltip;
  } else {
    isShown = tab.isShown;
    tooltipContent = tab.tooltip;
    tabContent = getAccordionItem(tab, index, selectedTabKey, (selectedTab, i) => onClickHandler(selectedTab, i, selectedTabKey, onSelect), NavLink);
  }
  if (isShown === false) {
    return null;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.Tooltip, {
    placement: _tooltip.TOOLTIP_PLACEMENT_TYPES.BOTTOM,
    docsLink: tab.docsLink,
    content: tooltipContent,
    interactiveCloseOnClick: hasInteractiveTooltip,
    children: tabContent
  }, tab.key);
};
exports.AccordionTab = AccordionTab;