import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
// (C) 2025 GoodData Corporation
import { useCallback, useMemo } from "react";
import { useIntl } from "react-intl";
import { UiAsyncTableBulkActions } from "./UiAsyncTableBulkActions.js";
import { UiAsyncTableCheckbox } from "./UiAsyncTableCheckbox.js";
import { UiAsyncTableFilter } from "./UiAsyncTableFilter.js";
import { Input } from "../../../Form/Input.js";
import { e } from "../asyncTableBem.js";
import { messages } from "../locales.js";
import { useAsyncTableSearch } from "../useAsyncTableSearch.js";
export function UiAsyncTableToolbar(props) {
    const { isChecked, isCheckboxIndeterminate, hasContent, checkbox, isCheckboxDisabled, handleCheckboxChange, renderBulkActions, renderFilters, renderSearch, } = useAsyncTableToolbar(props);
    return hasContent ? (_jsxs("div", { className: e("toolbar", { checkbox }), children: [_jsx(UiAsyncTableCheckbox, { onChange: handleCheckboxChange, checked: isChecked, indeterminate: isCheckboxIndeterminate, disabled: isCheckboxDisabled }), renderBulkActions(), renderFilters(), renderSearch()] })) : null;
}
const useAsyncTableToolbar = ({ filters, isFiltersTooLarge, bulkActions, selectedItemIds, setSelectedItemIds, totalItemsCount, items, isSmall, onSearch, }) => {
    const intl = useIntl();
    const { searchValue, setSearchValue } = useAsyncTableSearch(onSearch);
    const handleCheckboxChange = useCallback(() => {
        setSelectedItemIds(selectedItemIds?.length === 0 ? items.map((item) => item.id) : []);
    }, [selectedItemIds, items, setSelectedItemIds]);
    const isCheckboxDisabled = useMemo(() => {
        return items.length === 0;
    }, [items]);
    const renderBulkActions = useCallback(() => {
        const message = intl.formatMessage(messages["selectedCount"], {
            selectedCount: selectedItemIds?.length ?? 0,
            totalCount: totalItemsCount,
        });
        const messageShort = intl.formatMessage(messages["selectedCountShort"], {
            selectedCount: selectedItemIds?.length ?? 0,
        });
        if (bulkActions) {
            return (_jsxs(_Fragment, { children: [_jsx("div", { className: e("toolbar-selected-count", { short: isSmall }), children: isSmall ? messageShort : message }), selectedItemIds?.length > 0 ? (_jsx(UiAsyncTableBulkActions, { bulkActions: bulkActions })) : null] }));
        }
        return null;
    }, [bulkActions, selectedItemIds, isSmall, totalItemsCount, intl]);
    const renderFilters = useCallback(() => {
        return filters?.length ? (_jsxs(_Fragment, { children: [_jsx("div", { className: e("toolbar-label"), children: intl.formatMessage(messages["filterLabel"]) }), filters.map((filter) => (_jsx(UiAsyncTableFilter, { isSmall: isSmall ? !!selectedItemIds?.length : null, isFiltersTooLarge: isFiltersTooLarge, ...filter }, filter.label)))] })) : null;
    }, [filters, intl, isSmall, selectedItemIds?.length, isFiltersTooLarge]);
    const renderSearch = useCallback(() => {
        const placeholder = intl.formatMessage(messages["titleSearchPlaceholder"]);
        return onSearch ? (_jsx("div", { className: e("toolbar-search"), children: _jsx(Input, { isSearch: true, type: "search", isSmall: true, clearOnEsc: true, placeholder: placeholder, accessibilityConfig: {
                    ariaLabel: placeholder,
                }, value: searchValue, onChange: setSearchValue }) })) : null;
    }, [onSearch, intl, searchValue, setSearchValue]);
    const hasContent = useMemo(() => {
        return filters?.length || bulkActions;
    }, [filters, bulkActions]);
    const isChecked = useMemo(() => {
        return !!selectedItemIds?.length;
    }, [selectedItemIds]);
    const isCheckboxIndeterminate = useMemo(() => {
        return !!bulkActions && selectedItemIds?.length !== items.length;
    }, [bulkActions, selectedItemIds, items]);
    return {
        isChecked,
        isCheckboxIndeterminate,
        hasContent,
        checkbox: !!bulkActions,
        isCheckboxDisabled,
        handleCheckboxChange,
        renderBulkActions,
        renderFilters,
        renderSearch,
    };
};
//# sourceMappingURL=UiAsyncTableToolbar.js.map