import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2020-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 { guidFor } from "./guid.js";
import { LegacyList } from "./LegacyList.js";
import { LegacyListItem } from "./LegacyListItem.js";
import { LegacyMultiSelectListItem } from "./LegacyMultiSelectListItem.js";
/**
 * @internal
 * @deprecated This component is deprecated use MultiSelectList instead
 */
export const LegacyMultiSelectListWithIntl = function LegacyMultiSelectListWithIntl({ isInverted = false, isSearching = false, selection = [], getItemKey = guidFor, isMobile = false, isSelected = () => false, listItemClass = LegacyMultiSelectListItem, // TODO add tests
filteredItemsCount = 0, onItemMouseOut = () => { }, onItemMouseOver = () => { }, onRangeChange = () => { }, onSelect = () => { }, onSelectAll = () => { }, onSelectNone = () => { }, onSelectOnly = () => { }, rowItem: rowItemProp = null, selectAllCheckbox = false, tagName = "", items, itemsCount, height, itemHeight, width, }) {
    const intl = useIntl();
    const isEmpty = useCallback(() => {
        if (selection.length === 0) {
            return !isInverted;
        }
        if (isSearching) {
            return items.every((item) => !isSelected(item));
        }
        return (selection.length === 0 && !isInverted) || (selection.length === itemsCount && isInverted);
    }, [selection.length, isInverted, isSearching, items, isSelected, itemsCount]);
    const isIndefiniteSelection = useCallback(() => {
        if (selection.length === 0) {
            return false;
        }
        if (isSearching) {
            const selectedItems = items.filter((item) => isSelected(item));
            const selectedItemsCount = selectedItems.length;
            return selectedItemsCount !== 0 && selectedItemsCount !== filteredItemsCount;
        }
        return true;
    }, [selection.length, 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 ? selection.length === 0 : selection.length === itemsCount;
    }, [itemsCount, isInverted, isSearching, items, isSelected, selection.length]);
    const onActionCheckboxChange = useCallback(() => {
        if (isAllSelected() || (!isInverted && isSearching && isIndefiniteSelection() && !isEmpty())) {
            return onSelectNone();
        }
        return onSelectAll();
    }, [onSelectAll, onSelectNone, isInverted, isSearching, isAllSelected, isIndefiniteSelection, isEmpty]);
    const getSelectableItems = useCallback(() => {
        return items.map((source) => ({
            source,
            onSelect: onSelect,
            onMouseOver: onItemMouseOver,
            onMouseOut: onItemMouseOut,
            onOnly: onSelectOnly,
            selected: isSelected(source),
            id: getItemKey(source),
        }));
    }, [items, onSelect, onItemMouseOver, onItemMouseOut, onSelectOnly, isSelected, getItemKey]);
    const getRowItem = useCallback(() => {
        return rowItemProp || _jsx(LegacyListItem, { listItemClass: listItemClass });
    }, [rowItemProp, listItemClass]);
    const getSelectionString = useCallback((selection) => {
        if (!selection.length) {
            return "";
        }
        return selection
            .map((item) => {
            if (item.available !== undefined && !item.available) {
                return intl.formatMessage({ id: "gs.list.notAvailableAbbreviation" });
            }
            return item.title || `(${intl.formatMessage({ id: "empty_value" })})`;
        })
            .join(", ");
    }, [intl]);
    const getDataSource = useCallback(() => {
        const selectableItems = getSelectableItems();
        return {
            rowsCount: itemsCount || selectableItems.length,
            getObjectAt: (rowIndex) => selectableItems[rowIndex],
        };
    }, [getSelectableItems, itemsCount]);
    const renderSearchResultsLength = useCallback(() => {
        if (isSearching && itemsCount > 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;
    }, [itemsCount, isSearching, 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", { role: "select-all-checkbox", 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,
        renderSearchResultsLength,
        onSelectAll,
        onSelectNone,
    ]);
    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(selection);
        const isSelectionEmpty = selection.length === 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 = selection.length > 1 ? `\xa0(${selection.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", { role: "list-status-bar", className: "gd-list-status-bar s-list-status-bar", children: [attributeName, is, allOrNone, invertedInfo, selectionList, selectionLengthInfo] }));
    }, [selectAllCheckbox, tagName, getSelectionString, selection, isInverted, intl]);
    const rowItem = useMemo(() => getRowItem(), [getRowItem]);
    const dataSource = useMemo(() => getDataSource(), [getDataSource]);
    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(LegacyList, { className: "is-multiselect", width: width, height: height, itemHeight: itemHeight, dataSource: dataSource, rowItem: rowItem, onScroll: onRangeChange, compensateBorder: !isMobile }) }), renderStatusBar()] }));
};
