import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2007-2026 GoodData Corporation
import { useCallback, useMemo } from "react";
import cx from "classnames";
import { useIntl } from "react-intl";
import { Button } from "../Button/Button.js";
import { FlexDimensions } from "../FlexDimensions/FlexDimensions.js";
import { List } from "./List.js";
/**
 * @internal
 */
export function MultiSelectList({ isMobile, width, height, items, itemHeight, itemsCount, onScrollEnd, renderItem, selectedItems, listClassNames, onSelectAll, onSelectNone, isInverted, isSearching, isSelected, filteredItemsCount, selectAllCheckbox, tagName, }) {
    const intl = useIntl();
    const getSelectionString = useCallback((selection) => {
        if (!selection.length) {
            return "";
        }
        return selection
            .map((item) => {
            if (Object.prototype.hasOwnProperty.call(item, "available") && !item.available) {
                return intl.formatMessage({ id: "gs.list.notAvailableAbbreviation" });
            }
            return item.title || `(${intl.formatMessage({ id: "empty_value" })})`;
        })
            .join(", ");
    }, [intl]);
    const isEmpty = useCallback(() => {
        if ((selectedItems?.length ?? 0) === 0) {
            return !isInverted;
        }
        if (isSearching) {
            return (items ?? []).every((item) => !isSelected?.(item));
        }
        return (((selectedItems?.length ?? 0) === 0 && !isInverted) ||
            ((selectedItems?.length ?? 0) === itemsCount && isInverted));
    }, [selectedItems, isInverted, isSearching, items, isSelected, itemsCount]);
    const isIndefiniteSelection = useCallback(() => {
        if ((selectedItems?.length ?? 0) === 0) {
            return false;
        }
        if (isSearching) {
            const filteredSelectedItems = (items ?? []).filter((item) => isSelected?.(item));
            const selectedItemsCount = filteredSelectedItems.length;
            return selectedItemsCount !== 0 && selectedItemsCount !== filteredItemsCount;
        }
        return true;
    }, [selectedItems, isSearching, items, isSelected, filteredItemsCount]);
    const isAllSelected = useCallback(() => {
        if (isSearching) {
            const selectedItemsCount = (items ?? []).filter((item) => isSelected?.(item)).length;
            const totalItemsCount = (items ?? []).filter((item) => item !== null).length;
            return selectedItemsCount === totalItemsCount;
        }
        return isInverted ? (selectedItems?.length ?? 0) === 0 : (selectedItems?.length ?? 0) === itemsCount;
    }, [isInverted, itemsCount, isSearching, items, isSelected, selectedItems]);
    const onActionCheckboxChange = useCallback(() => {
        if (isAllSelected() || (!isInverted && isSearching && isIndefiniteSelection() && !isEmpty())) {
            return onSelectNone?.();
        }
        return onSelectAll?.();
    }, [onSelectAll, onSelectNone, isInverted, isSearching, isAllSelected, isEmpty, isIndefiniteSelection]);
    const renderSearchResultsLength = useCallback(() => {
        if (isSearching && (itemsCount ?? 0) > 0) {
            return (_jsxs("span", { className: "gd-list-actions-selection-size s-list-search-selection-size", children: [intl.formatMessage({ id: "gs.list.searchResults" }), " (", itemsCount, ")"] }));
        }
        return null;
    }, [isSearching, itemsCount, intl]);
    const renderActions = useCallback(() => {
        if (selectAllCheckbox) {
            const checkboxClasses = cx("input-checkbox", "gd-checkbox-selection", {
                "checkbox-indefinite": isIndefiniteSelection(),
            });
            const labelClasses = cx("input-checkbox-label", "s-select-all-checkbox");
            const checkbox = (_jsxs("label", { className: labelClasses, children: [
                    _jsx("input", { readOnly: true, type: "checkbox", className: checkboxClasses, checked: !isEmpty(), onChange: onActionCheckboxChange }), _jsx("span", { className: "input-label-text", children: intl.formatMessage({ id: "gs.list.all" }) })
                ] }));
            return (_jsxs("div", { className: "gd-list-actions gd-list-actions-invertable", children: [checkbox, renderSearchResultsLength()] }));
        }
        return (_jsxs("div", { className: "gd-list-actions", children: [
                _jsx(Button, { className: "gd-button-link", tagName: "a", onClick: onSelectAll, value: intl.formatMessage({ id: "gs.list.selectAll" }) }), _jsx(Button, { className: "gd-button-link", tagName: "a", onClick: onSelectNone, value: intl.formatMessage({ id: "gs.list.clear" }) })
            ] }));
    }, [
        selectAllCheckbox,
        isIndefiniteSelection,
        isEmpty,
        onActionCheckboxChange,
        intl,
        onSelectAll,
        onSelectNone,
        renderSearchResultsLength,
    ]);
    const renderStatusBar = useCallback(() => {
        if (!selectAllCheckbox) {
            return null;
        }
        const attributeName = (_jsx("span", { className: "gd-shortened-text gd-attribute-name s-dropdown-attribute-filter-name", title: tagName, children: tagName }));
        const selectionItemsStr = getSelectionString(selectedItems ?? []);
        const isSelectionEmpty = (selectedItems?.length ?? 0) === 0;
        const invertedInfo = !isSelectionEmpty && isInverted ? (_jsxs("span", { children: [
                _jsx("b", { children: intl.formatMessage({ id: "gs.list.all" }) }),
                "\u00A0", intl.formatMessage({ id: "gs.list.except" }), "\u00A0"] })) : null;
        const selectionList = isSelectionEmpty ? null : (_jsx("span", { className: "gd-shortened-text gd-selection-list s-dropdown-attribute-selection-list", title: selectionItemsStr, children: `${selectionItemsStr}` }));
        const selectionLengthInfo = (selectedItems?.length ?? 0) > 1 ? `\xa0(${selectedItems?.length})` : null;
        const is = _jsxs("span", { children: ["\u00A0", intl.formatMessage({ id: "gs.list.is" }), "\u00A0"] });
        const allOrNone = isSelectionEmpty &&
            (isInverted ? (_jsx("b", { children: intl.formatMessage({ id: "gs.list.all" }) })) : (`(${intl.formatMessage({ id: "gs.filterLabel.none" })})`));
        return (_jsxs("div", { className: "gd-list-status-bar s-list-status-bar", children: [attributeName, is, allOrNone, invertedInfo, selectionList, selectionLengthInfo] }));
    }, [selectAllCheckbox, tagName, selectedItems, isInverted, intl, getSelectionString]);
    const classNames = useMemo(() => cx("is-multiselect", listClassNames || ""), [listClassNames]);
    return (_jsxs("div", { className: "gd-flex-item-stretch-mobile gd-flex-row-container-mobile", children: [renderActions(), _jsx(FlexDimensions, { measureHeight: isMobile, measureWidth: isMobile || !width, className: "gd-flex-item-stretch-mobile", children: _jsx(List, { className: classNames, width: width, height: height, items: items, itemHeight: itemHeight, itemsCount: itemsCount, renderItem: useCallback(({ item }) => {
                        return renderItem({
                            item,
                            isSelected: isSelected
                                ? isSelected(item)
                                : (selectedItems ?? []).some((_item) => _item === item),
                        });
                    }, [renderItem, isSelected, selectedItems]), onScrollEnd: onScrollEnd, compensateBorder: !isMobile }) }), renderStatusBar()] }));
}
