import React, { cloneElement, Fragment, useEffect } from 'react';
import { Box } from '@wix/design-system';
import { observer } from 'mobx-react-lite';
import { ToolbarItem } from '../ToolbarItem/ToolbarItem';
import { ToolbarGroupScrollWidthBox } from './ToolbarGroupScrollWidthBox';
import { SearchOrCustomFilter } from './SearchOrCustomFilter';
import { useToolbarCollectionContext } from '../ToolbarCollectionContext';
import { CollectionToolbarActionsGroupProps } from './CollectionToolbarActionsGroupProps';
import { InitialLoadingConditional } from '../InitialLoadingConditional';
import { CollectionToolbarSkeletonItem } from './CollectionToolbarSkeletonItem';
import { ToolbarOverflowMenu } from './ToolbarOverflowMenu';
import { useToolbarOverflowItems } from './useToolbarOverflowItems';
import { LAYOUT_SWITCH_BUTTON_WIDTH } from '../../state/Toolbar/toolbarResponsiveConstants';
function _CollectionToolbarActionsGroupResponsiveLayout(
props: CollectionToolbarActionsGroupProps,
) {
const {
state,
left,
viewsToolbarItem,
filtersElement,
MoreFiltersButton,
multiLevelSorting,
customColumns,
exportButton,
importButton,
search,
showCustomColumnsOnTheLeft,
tableGridSwitchButton,
viewTypeState,
} = props;
const toolbarState = useToolbarCollectionContext();
const {
toolbar: { responsive },
} = toolbarState;
const overflowItems = useToolbarOverflowItems(toolbarState, {
customColumns,
multiLevelSorting,
exportButton,
importButton,
MoreFiltersButton,
viewTypeState,
});
const overflowItemCount =
overflowItems.primaryItems.length +
overflowItems.secondaryItems.length +
(overflowItems.layoutSwitchConfig ? 1 : 0);
// Don't show a "More" menu for a single item — keep it as an inline icon.
const shouldOverflow = responsive._shouldOverflow && overflowItemCount > 1;
// Track width of non-group content hidden during overflow so the algorithm
// doesn't overestimate available space when deciding whether to exit overflow.
// Tied to effective shouldOverflow (not just _shouldOverflow) so the single-item
// bypass doesn't cause a mismatch between DOM and algorithm.
useEffect(() => {
responsive._overflowHiddenContentWidth =
tableGridSwitchButton && shouldOverflow ? LAYOUT_SWITCH_BUTTON_WIDTH : 0;
}, [responsive, !!tableGridSwitchButton, shouldOverflow]);
let child = (
{left ? (
<>
{viewsToolbarItem && (
}
>
{viewsToolbarItem}
)}
{filtersElement}
>
) : viewsToolbarItem ? (
filtersElement
) : undefined}
{!shouldOverflow && tableGridSwitchButton}
{!shouldOverflow && MoreFiltersButton && (
)}
{!shouldOverflow && (
{customColumns && showCustomColumnsOnTheLeft && customColumns}
{multiLevelSorting}
{exportButton &&
cloneElement(exportButton, {
toolbarType: 'default',
})}
{importButton &&
cloneElement(importButton, {
toolbarType: 'default',
})}
{customColumns && !showCustomColumnsOnTheLeft && customColumns}
)}
{shouldOverflow && (exportButton || importButton) && (
{exportButton &&
cloneElement(exportButton, { toolbarType: 'default' })}
{importButton &&
cloneElement(importButton, { toolbarType: 'default' })}
)}
{shouldOverflow && (
)}
{search && }
);
child = (
{
responsive._contentElement = element;
}}
dataHook="horizontal-overflow-box"
minWidth={0}
direction="horizontal"
gap="SP2"
>
{child}
);
child = {child};
child = (
{
responsive._measurementElement = element;
}}
minWidth={0}
flex={1}
align="right"
direction="horizontal"
>
{child}
);
return child;
}
export const CollectionToolbarActionsGroupResponsiveLayout = observer(
_CollectionToolbarActionsGroupResponsiveLayout,
);