"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = TableRowWithExpandableContent;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _accordionTabs = require("../../../accordion-tabs");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function TableRowWithExpandableContent({
  handleRowClick = () => {},
  selectedId = null,
  tableHeaderSelector = '.r-thead',
  tableSelector = '.r-table',
  item,
  tabs,
  children,
  forceTabsControl,
  testId,
  disableScrollOnOpen,
  activeTabId,
  onTabSelect = () => {}
}) {
  const getActiveTabIndex = () => tabs.findIndex(t => {
    return activeTabId ? t.id === activeTabId : !t.isVisible || t.isVisible(item);
  });
  const [activeTabIndex, setActiveTabIndex] = (0, _react.useState)(getActiveTabIndex());
  const elementRef = (0, _react.useRef)(null);
  const isOpened = selectedId === item?.id;
  (0, _react.useEffect)(() => {
    if (isOpened && elementRef.current && !disableScrollOnOpen) {
      elementRef.current?.scrollIntoView(true);

      // Subtract `position: sticky` header on tableElem to get correct scrollTop.
      const tableHeader = document.querySelector(tableHeaderSelector);
      const tableElem = document.querySelector(tableSelector);
      if (tableElem && tableHeader) {
        tableElem.scrollTop -= tableHeader.getBoundingClientRect().height;
      }
    }
  }, [selectedId]);
  (0, _react.useEffect)(() => {
    const activeTab = getActiveTabIndex();
    setActiveTabIndex(activeTab);
  }, [activeTabId]);
  const SelectedComponent = tabs[activeTabIndex].component;

  // Key of the value passed to the active tab.
  // Defaults to `item` if `[key]` is not provided.
  const key = tabs[activeTabIndex].key || 'item';

  // Assemble item as prop for selectedComponent
  const selectedComponentItem = {
    [key]: {
      ...item
    }
  };
  const shouldShowTabsControl = forceTabsControl || tabs.length > 1;
  const getIsTabShown = function (tab) {
    if (tab.isVisible === undefined) {
      return true;
    }
    if (typeof tab.isVisible === 'function') {
      return tab.isVisible(item);
    }
    return tab.isVisible;
  };
  const preppedTabs = tabs.map(tab => ({
    testId: tab?.id,
    key: tab?.id,
    value: tab?.value,
    isShown: getIsTabShown(tab),
    isDisabled: tab?.isDisabled
  }));
  const handleTabSelected = tabIndex => {
    setActiveTabIndex(tabIndex);
    onTabSelect(tabIndex);
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("article", {
    className: (0, _classnames.default)('r-tr tr-with-expandable-content', {
      open: isOpened
    }, item?.className),
    "test-id": testId,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames.default)('tr-with-expandable-content-data clickable', {
        open: isOpened
      }),
      role: "button",
      ref: elementRef,
      onClick: () => handleRowClick(item?.id, activeTabIndex),
      children: children
    }), isOpened && /*#__PURE__*/(0, _jsxRuntime.jsxs)("section", {
      className: "sub-panel",
      children: [shouldShowTabsControl && /*#__PURE__*/(0, _jsxRuntime.jsx)(_accordionTabs.AccordionTabs, {
        tabs: preppedTabs,
        onSelect: (selectedTab, i) => handleTabSelected(i),
        selectedTabKey: preppedTabs[activeTabIndex].key
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "sub-panel-main",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(SelectedComponent, {
          ...selectedComponentItem,
          // ERROR: "Property 'viewMore' does not exist on type 'IntrinsicAttributes'.ts(2322)"
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          viewMore: index => handleTabSelected(index),
          tabs: tabs
        })
      })]
    })]
  });
}