import React, { useState, useEffect } from 'react';
import TableRowWithExpandableContent from './table-row-with-expandable-content';
import { jsx as _jsx } from "react/jsx-runtime";
export default function withExpandableContent(BaseTable) {
  function WithExpandableContent({
    onTableRowSelected = () => {},
    selectedTableRowId,
    tabs,
    forceTabsControl = false,
    rowTestId = 'expandable-row',
    disableScrollOnOpen = false,
    isActiveRowCollapsible = true,
    activeTabId,
    onTabSelect,
    ...props
  }) {
    const [selectedId, setSelectedId] = useState(selectedTableRowId);
    useEffect(() => {
      setSelectedId(selectedTableRowId);
    }, [selectedTableRowId]);
    const handleRowClick = (id, activeTabIndex) => {
      const isOpened = id !== selectedId;

      // if we should not close already opened section
      if (!isOpened && !isActiveRowCollapsible) {
        return;
      }
      setSelectedId(isOpened ? id : null);
      onTableRowSelected(isOpened ? id : null, activeTabIndex);
    };
    return /*#__PURE__*/_jsx(BaseTable, {
      ...props,
      TrComponent: TableRowWithExpandableContent,
      trProps: {
        handleRowClick,
        selectedId,
        tabs,
        forceTabsControl,
        disableScrollOnOpen,
        testId: rowTestId,
        activeTabId,
        onTabSelect
      }
    });
  }
  return WithExpandableContent;
}