import React__default from 'react';
import { u as useDataListContext } from './DataListContext-es.js';
import { c as generateHeaderElements, s as sortBreakpoints } from './DataList.utils-es.js';
import { u as useResponsiveSizing } from './useResponsiveSizing-es.js';
import classnames from 'classnames';
import { A as AnimatedSwitcher } from './AnimatedSwitcher-es.js';
import { T as Text } from './Text-es.js';
import { B as Button } from './Button-es.js';
import { C as Checkbox } from './Checkbox-es.js';
import { s as styles } from './DataList.module-es.js';
import { I as InternalDataListBulkActions } from './DataListBulkActions-es.js';
import { u as useBatchSelect } from './useBatchSelect-es.js';
import { u as useActiveLayout } from './useActiveLayout-es.js';

const DATA_LIST_HEADER_CHECKBOX_TEST_ID = "ATL-DataList-header-checkbox";
const DATA_LIST_HEADER_BATCH_SELECT_TEST_ID = "ATL-DataList-header-batch-select";
function DataListHeaderCheckbox({ children }) {
    const { sm } = useResponsiveSizing();
    const { data, totalCount } = useDataListContext();
    const { canSelect, canSelectAll, hasAtLeastOneSelected, isSelectAll, selectedCount, selectedIDs, selected, onSelectAll, onSelect, } = useBatchSelect();
    // If there's no onSelect, we don't need to render the checkbox.
    // We'll still render the (invisible) checkbox even if only onSelect is provided for alignment.
    if (!canSelect)
        return children;
    const deselectText = sm ? "Deselect All" : "Deselect";
    const selectedLabel = selectedCount ? `${selectedCount} selected` : "";
    return (React__default.createElement("div", { className: styles.selectable },
        React__default.createElement("div", { "data-testid": DATA_LIST_HEADER_CHECKBOX_TEST_ID, style: {
                visibility: canSelectAll ? "visible" : "hidden",
            }, className: classnames(styles.selectAllCheckbox) },
            React__default.createElement(Checkbox, { checked: isAllSelected(), indeterminate: isIndeterminate(), onChange: handleSelectAll, label: React__default.createElement("div", { className: styles.srOnly }, selectedLabel) })),
        React__default.createElement(ColumnHeaderContent, { canSelectAll: canSelectAll, hasAtLeastOneSelected: hasAtLeastOneSelected, selectedCount: selectedCount, deselectText: deselectText, onSelect: onSelect }, children)));
    function isIndeterminate() {
        if (isSelectAll)
            return selectedIDs.length > 0;
        return selectedCount > 0 && !isAllSelected();
    }
    function isAllSelected() {
        if (isSelectAll)
            return true;
        // If there's a totalCount, we can use that to accurately determine if the
        // user have "selected all".
        if (totalCount) {
            return totalCount === selectedCount;
        }
        // Otherwise, we'll use the total count of data. This is not as reliable, as
        // it's possible that the would select all loaded data while we're
        // loading more. It's still hard to get to that state as the load more
        // triggers before you see the last item.
        return data.length > 0 && selectedCount >= data.length;
    }
    function handleSelectAll() {
        if (isAllSelected()) {
            return onSelectAll === null || onSelectAll === void 0 ? void 0 : onSelectAll([]);
        }
        onSelectAll === null || onSelectAll === void 0 ? void 0 : onSelectAll({ totalCount: getTotalCount(), unselected: [] });
    }
    function getTotalCount() {
        if (selected && "totalCount" in selected)
            return selected.totalCount;
        if (totalCount)
            return totalCount;
        return 0;
    }
}
function ColumnHeaderContent({ canSelectAll, children, hasAtLeastOneSelected, selectedCount, deselectText, onSelect, }) {
    if (canSelectAll) {
        return (React__default.createElement(AnimatedSwitcher, { switched: hasAtLeastOneSelected, initialChild: children, switchTo: React__default.createElement("div", { "data-testid": DATA_LIST_HEADER_BATCH_SELECT_TEST_ID, className: styles.batchSelectContainer },
                React__default.createElement("div", { className: styles.headerBatchSelect },
                    Boolean(selectedCount) && React__default.createElement(Text, null,
                        selectedCount,
                        " selected"),
                    React__default.createElement(Button, { label: deselectText, onClick: () => onSelect === null || onSelect === void 0 ? void 0 : onSelect([]), type: "tertiary" })),
                React__default.createElement(InternalDataListBulkActions, null)) }));
    }
    else {
        return children;
    }
}

function DataListHeader() {
    const breakpoints = useResponsiveSizing();
    const { headerVisibility = { xs: true, sm: true, md: true, lg: true, xl: true }, headers, layoutBreakpoints, } = useDataListContext();
    const { hasAtLeastOneSelected, canSelect, canSelectAll } = useBatchSelect();
    const size = getVisibleSize();
    const { layout } = useActiveLayout();
    const visible = headerVisibility[size];
    if (isHeaderHidden(visible, hasAtLeastOneSelected, layout, canSelect, canSelectAll)) {
        return null;
    }
    const headerData = generateHeaderElements(headers);
    if (!headerData || !layout)
        return null;
    return (React__default.createElement("div", { className: styles.headerTitles },
        React__default.createElement(DataListHeaderCheckbox, null, layout(headerData))));
    function getVisibleSize() {
        const visibilityKeys = Object.keys(headerVisibility);
        const headerBreakpoints = sortBreakpoints(visibilityKeys).reverse();
        const visibleHeaderSize = headerBreakpoints.find(key => breakpoints[key]);
        return visibleHeaderSize || layoutBreakpoints[0];
    }
}
function isHeaderHidden(visible, hasAtLeastOneSelected, layout, canSelect, canSelectAll) {
    // When the horizontal area is too small and the data starts to wrap
    // we want to hide the headers that will no longer align with the content
    // The exception is when we have onSelectAll AND one or more are selected
    // because then we show the "header" in its select-all UI variation state
    const isHiddenForSelect = !visible && canSelect && !canSelectAll;
    const isHiddenForSelectAll = !visible && !hasAtLeastOneSelected;
    return !layout || isHiddenForSelect || isHiddenForSelectAll;
}

export { DataListHeader as D };
