import { _ as __rest } from './tslib.es6-es.js';
import React__default, { useState, useMemo, useRef } from 'react';
import { s as styles } from './DataList.module-es.js';
import { a as DataListTotalCount } from './DataListTotalCount-es.js';
import { a as DataListLoadingState } from './DataListLoadingState-es.js';
import { D as DataListLayout } from './DataListLayout-es.js';
import { D as DataListHeader } from './DataListHeader-es.js';
import { I as InternalDataListFilters, D as DataListFilters } from './DataListFilters-es.js';
import { D as DataListStickyHeader } from './DataListStickyHeader-es.js';
import { I as InternalDataListSearch, a as DataListSearch } from './DataListSearch-es.js';
import { I as InternalDataListEmptyState, D as DataListEmptyState } from './DataListEmptyState-es.js';
import { D as DataListLoadMore } from './DataListLoadMore-es.js';
import { D as DataListItemActions } from './DataListItemActions-es.js';
import { D as DataListAction } from './DataListAction-es.js';
import { D as DataListLayoutActions } from './DataListLayoutActions-es.js';
import { D as DataListContext, u as useDataListContext } from './DataListContext-es.js';
import { s as sortBreakpoints, g as getCompoundComponent, a as getCompoundComponents } from './DataList.utils-es.js';
import { D as DATA_LIST_TITLE_CONTAINER_TEST_ID, a as DATA_LIST_FILTERING_SPINNER_TEST_ID } from './DataList.const-es.js';
import { D as DataListBulkActions } from './DataListBulkActions-es.js';
import { I as InternalDataListStatusBar, D as DataListStatusBar } from './DataListStatusBar-es.js';
import { H as Heading } from './Heading-es.js';
import { S as Spinner } from './Spinner-es.js';

/**
 * Legacy component. Existing usages are supported but DataList should not
 * be used for new implementations. Use DataTable composable atoms instead.
 */
function DataList(_a) {
    var { selected = [], sorting } = _a, props = __rest(_a, ["selected", "sorting"]);
    const [layouts, setLayouts] = useState({});
    const layoutBreakpoints = useMemo(() => sortBreakpoints(Object.keys(layouts)), [layouts]);
    const searchComponent = getCompoundComponent(props.children, DataListSearch);
    const filterComponent = getCompoundComponent(props.children, DataListFilters);
    const layoutComponents = getCompoundComponents(props.children, DataListLayout);
    const emptyStateComponents = getCompoundComponents(props.children, DataListEmptyState);
    const itemActionComponent = getCompoundComponent(props.children, DataListItemActions);
    const bulkActionsComponent = getCompoundComponent(props.children, DataListBulkActions);
    const headerCount = Object.keys(props.headers).length;
    const shouldRenderStickyHeader = !!filterComponent || !!searchComponent || headerCount > 0;
    return (React__default.createElement(DataListContext.Provider, { value: Object.assign(Object.assign({ searchComponent,
            filterComponent,
            layoutComponents,
            emptyStateComponents,
            itemActionComponent,
            bulkActionsComponent,
            layoutBreakpoints,
            layouts,
            registerLayout }, props), { selected, 
            // T !== DataListObject
            sorting: sorting }) },
        React__default.createElement(InternalDataList, { shouldRenderStickyHeader: shouldRenderStickyHeader })));
    function registerLayout(size, children) {
        setLayouts(prev => (Object.assign(Object.assign({}, prev), { [size]: children })));
    }
}
function InternalDataList({ shouldRenderStickyHeader, }) {
    const { data, title, totalCount, loadingState = "none", layoutComponents, } = useDataListContext();
    const backToTopRef = useRef(null);
    const initialLoading = loadingState === "initial";
    const showEmptyState = !initialLoading && data.length === 0;
    const shouldRenderLoadMoreTrigger = !initialLoading && !showEmptyState;
    const shouldRenderTitleContainer = initialLoading || title !== undefined || totalCount !== undefined;
    return (React__default.createElement("div", { className: styles.wrapper },
        shouldRenderTitleContainer && (React__default.createElement("div", { className: styles.titleContainer, "data-testid": DATA_LIST_TITLE_CONTAINER_TEST_ID },
            title && React__default.createElement(Heading, { level: 3 }, title),
            totalCount !== undefined && (React__default.createElement(DataListTotalCount, { totalCount: totalCount, loading: initialLoading })))),
        React__default.createElement("div", { ref: backToTopRef }),
        shouldRenderStickyHeader && (React__default.createElement(DataListStickyHeader, null,
            React__default.createElement("div", { className: styles.headerFilters },
                React__default.createElement(InternalDataListFilters, null),
                React__default.createElement(InternalDataListSearch, null)),
            React__default.createElement(InternalDataListStatusBar, null),
            React__default.createElement(DataListHeader, null))),
        initialLoading && React__default.createElement(DataListLoadingState, null),
        showEmptyState && React__default.createElement(InternalDataListEmptyState, null),
        layoutComponents,
        loadingState === "filtering" && (React__default.createElement("div", { "data-testid": DATA_LIST_FILTERING_SPINNER_TEST_ID, className: styles.filtering },
            React__default.createElement("div", { className: styles.filteringSpinner },
                React__default.createElement(Spinner, { size: "small" })))),
        shouldRenderLoadMoreTrigger && (React__default.createElement(DataListLoadMore, { onBackToTop: handleBackToTop }))));
    function handleBackToTop() {
        var _a;
        // For testing purposes since jest doesn't know scrollIntoView.
        // This prevents consumer's tests from needing to mock scrollIntoView.
        if (!window.HTMLElement.prototype.scrollIntoView)
            return;
        (_a = backToTopRef.current) === null || _a === void 0 ? void 0 : _a.scrollIntoView({
            behavior: "smooth",
            block: "nearest",
            inline: "start",
        });
    }
}
/**
 * Sets the layout that the DataList item and header will use.
 */
DataList.Layout = DataListLayout;
/**
 * By using this component, we can render the actions anywhere in the layout
 * instead of being added automatically on hover.
 */
DataList.LayoutActions = DataListLayoutActions;
/**
 * When the DataList is either empty and/or filtered, this component will be
 * rendered.
 */
DataList.EmptyState = DataListEmptyState;
/**
 * Adds the filter components of your choosing to the DataList.
 */
DataList.Filters = DataListFilters;
/**
 * Enables the search functionality of the DataList.
 */
DataList.Search = DataListSearch;
/**
 * Defines the group actions you could do on a single DataList item.
 */
DataList.ItemActions = DataListItemActions;
/**
 * Defines the action in a DataList. This should be used inside the
 * DataListItemActions component.
 */
DataList.ItemAction = DataListAction;
/**
 * Defines the group actions you could do on multiple DataList items.
 */
DataList.BatchActions = DataListBulkActions;
/**
 * Defines the batch action in a DataList. This should be used inside the
 * DataListBatchActions component.
 */
DataList.BatchAction = function DataListBatchAction(props) {
    return React__default.createElement(DataListAction, Object.assign({}, props));
};
/**
 * Defines a status bar that is rendered between the filters and the header.
 */
DataList.StatusBar = DataListStatusBar;

export { DataList as D };
