'use strict';

var tslib_es6 = require('./tslib.es6-cjs.js');
var React = require('react');
var DataList_module = require('./DataList.module-cjs.js');
var DataListTotalCount = require('./DataListTotalCount-cjs.js');
var DataListLoadingState = require('./DataListLoadingState-cjs.js');
var DataListLayout = require('./DataListLayout-cjs.js');
var DataListHeader = require('./DataListHeader-cjs.js');
var DataListFilters = require('./DataListFilters-cjs.js');
var DataListStickyHeader = require('./DataListStickyHeader-cjs.js');
var DataListSearch = require('./DataListSearch-cjs.js');
var DataListEmptyState = require('./DataListEmptyState-cjs.js');
var DataListLoadMore = require('./DataListLoadMore-cjs.js');
var DataListItemActions = require('./DataListItemActions-cjs.js');
var DataListAction = require('./DataListAction-cjs.js');
var DataListLayoutActions = require('./DataListLayoutActions-cjs.js');
var DataListContext = require('./DataListContext-cjs.js');
var DataList_utils = require('./DataList.utils-cjs.js');
var DataList_const = require('./DataList.const-cjs.js');
var DataListBulkActions = require('./DataListBulkActions-cjs.js');
var DataListStatusBar = require('./DataListStatusBar-cjs.js');
var Heading = require('./Heading-cjs.js');
var Spinner = require('./Spinner-cjs.js');

function DataList(_a) {
    var { selected = [], sorting } = _a, props = tslib_es6.__rest(_a, ["selected", "sorting"]);
    const [layouts, setLayouts] = React.useState({});
    const layoutBreakpoints = React.useMemo(() => DataList_utils.sortBreakpoints(Object.keys(layouts)), [layouts]);
    const searchComponent = DataList_utils.getCompoundComponent(props.children, DataListSearch.DataListSearch);
    const filterComponent = DataList_utils.getCompoundComponent(props.children, DataListFilters.DataListFilters);
    const layoutComponents = DataList_utils.getCompoundComponents(props.children, DataListLayout.DataListLayout);
    const emptyStateComponents = DataList_utils.getCompoundComponents(props.children, DataListEmptyState.DataListEmptyState);
    const itemActionComponent = DataList_utils.getCompoundComponent(props.children, DataListItemActions.DataListItemActions);
    const bulkActionsComponent = DataList_utils.getCompoundComponent(props.children, DataListBulkActions.DataListBulkActions);
    const headerCount = Object.keys(props.headers).length;
    const shouldRenderStickyHeader = !!filterComponent || !!searchComponent || headerCount > 0;
    return (React.createElement(DataListContext.DataListContext.Provider, { value: Object.assign(Object.assign({ searchComponent,
            filterComponent,
            layoutComponents,
            emptyStateComponents,
            itemActionComponent,
            bulkActionsComponent,
            layoutBreakpoints,
            layouts,
            registerLayout }, props), { selected, 
            // T !== DataListObject
            sorting: sorting }) },
        React.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, } = DataListContext.useDataListContext();
    const backToTopRef = React.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.createElement("div", { className: DataList_module.styles.wrapper },
        shouldRenderTitleContainer && (React.createElement("div", { className: DataList_module.styles.titleContainer, "data-testid": DataList_const.DATA_LIST_TITLE_CONTAINER_TEST_ID },
            title && React.createElement(Heading.Heading, { level: 3 }, title),
            totalCount !== undefined && (React.createElement(DataListTotalCount.DataListTotalCount, { totalCount: totalCount, loading: initialLoading })))),
        React.createElement("div", { ref: backToTopRef }),
        shouldRenderStickyHeader && (React.createElement(DataListStickyHeader.DataListStickyHeader, null,
            React.createElement("div", { className: DataList_module.styles.headerFilters },
                React.createElement(DataListFilters.InternalDataListFilters, null),
                React.createElement(DataListSearch.InternalDataListSearch, null)),
            React.createElement(DataListStatusBar.InternalDataListStatusBar, null),
            React.createElement(DataListHeader.DataListHeader, null))),
        initialLoading && React.createElement(DataListLoadingState.DataListLoadingState, null),
        showEmptyState && React.createElement(DataListEmptyState.InternalDataListEmptyState, null),
        layoutComponents,
        loadingState === "filtering" && (React.createElement("div", { "data-testid": DataList_const.DATA_LIST_FILTERING_SPINNER_TEST_ID, className: DataList_module.styles.filtering },
            React.createElement("div", { className: DataList_module.styles.filteringSpinner },
                React.createElement(Spinner.Spinner, { size: "small" })))),
        shouldRenderLoadMoreTrigger && (React.createElement(DataListLoadMore.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.DataListLayout;
/**
 * By using this component, we can render the actions anywhere in the layout
 * instead of being added automatically on hover.
 */
DataList.LayoutActions = DataListLayoutActions.DataListLayoutActions;
/**
 * When the DataList is either empty and/or filtered, this component will be
 * rendered.
 */
DataList.EmptyState = DataListEmptyState.DataListEmptyState;
/**
 * Adds the filter components of your choosing to the DataList.
 */
DataList.Filters = DataListFilters.DataListFilters;
/**
 * Enables the search functionality of the DataList.
 */
DataList.Search = DataListSearch.DataListSearch;
/**
 * Defines the group actions you could do on a single DataList item.
 */
DataList.ItemActions = DataListItemActions.DataListItemActions;
/**
 * Defines the action in a DataList. This should be used inside the
 * DataListItemActions component.
 */
DataList.ItemAction = DataListAction.DataListAction;
/**
 * Defines the group actions you could do on multiple DataList items.
 */
DataList.BatchActions = DataListBulkActions.DataListBulkActions;
/**
 * Defines the batch action in a DataList. This should be used inside the
 * DataListBatchActions component.
 */
DataList.BatchAction = function DataListBatchAction(props) {
    return React.createElement(DataListAction.DataListAction, Object.assign({}, props));
};
/**
 * Defines a status bar that is rendered between the filters and the header.
 */
DataList.StatusBar = DataListStatusBar.DataListStatusBar;

exports.DataList = DataList;
