import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
// (C) 2025 GoodData Corporation
import { useCallback } from "react";
import { useIntl } from "react-intl";
import { UiAsyncTableCheckbox } from "./UiAsyncTableCheckbox.js";
import { UiAsyncTableIconRenderer } from "./UiAsyncTableIconRenderer.js";
import { getColumnWidth, stopPropagationCallback } from "./utils.js";
import { WithConditionalAnchor } from "./WithConditionalAnchor.js";
import { Dropdown } from "../../../Dropdown/Dropdown.js";
import { UiIconButton } from "../../UiIconButton/UiIconButton.js";
import { e } from "../asyncTableBem.js";
import { messages } from "../locales.js";
export function UiAsyncTableRow({ item, columns, onSelect, onClick, isSelected, hasCheckbox, isLarge, }) {
    const { renderCellContent } = useRenderCellContent({ isLarge });
    return (_jsxs("div", { onClick: () => onClick?.(item), className: e("row", { large: isLarge }), children: [hasCheckbox ? (_jsx(UiAsyncTableCheckbox, { checked: isSelected, onChange: () => onSelect(item) })) : null, columns.map((column, index) => {
                const { bold, renderMenu, width: widthProp } = column;
                const width = getColumnWidth(!!renderMenu, isLarge, widthProp);
                const key = index;
                return (_jsx("div", { style: { width }, className: e("cell", { bold, align: column.align }), children: renderCellContent(column, item) }, key));
            })] }));
}
const useRenderCellContent = ({ isLarge }) => {
    const intl = useIntl();
    const renderRoleIconWithWrapper = useCallback((renderRoleIcon, item) => {
        return (_jsx(UiAsyncTableIconRenderer, { renderIcon: renderRoleIcon, className: e("role-icon"), item: item }));
    }, []);
    const renderBadgeWithWrapper = useCallback((renderBadge, item) => {
        return _jsx(UiAsyncTableIconRenderer, { renderIcon: renderBadge, className: e("badge"), item: item });
    }, []);
    const renderPrefixIconWithWrapper = useCallback((renderPrefixIcon, item) => {
        return (_jsx(UiAsyncTableIconRenderer, { renderIcon: renderPrefixIcon, className: e("prefix-icon"), item: item }));
    }, []);
    const renderSuffixIconWithWrapper = useCallback((renderSuffixIcon, item) => {
        return (_jsx(UiAsyncTableIconRenderer, { renderIcon: renderSuffixIcon, className: e("suffix-icon"), item: item }));
    }, []);
    const renderMenuIcon = useCallback((renderMenu, item) => {
        const label = intl.formatMessage(messages["moreActions"]);
        return (_jsx(Dropdown, { renderButton: ({ toggleDropdown, isOpen }) => {
                const isDisabled = !renderMenu(item, () => toggleDropdown(false));
                return (_jsx(UiIconButton, { size: isLarge ? "xxlarge" : "xlarge", icon: "ellipsis", label: label, variant: "table", onClick: (e) => {
                        stopPropagationCallback(e, toggleDropdown);
                    }, isActive: isOpen, isDisabled: isDisabled, accessibilityConfig: {
                        ariaLabel: label,
                    } }));
            }, alignPoints: [{ align: "br tr" }], renderBody: ({ closeDropdown }) => renderMenu(item, closeDropdown) }));
    }, [intl, isLarge]);
    const renderTextContent = useCallback((item, key, titleProvided, getTextContent, getMultiLineTextContent) => {
        if (getMultiLineTextContent) {
            return getMultiLineTextContent(item).map((line, index) => (_jsx("span", { title: titleProvided ? undefined : line, className: e("text-line", { first: index === 0 }), children: line }, index)));
        }
        if (getTextContent) {
            return getTextContent(item);
        }
        return String(item[key]);
    }, []);
    const renderTextContentWithWrapper = useCallback((item, key, getTextContent, getMultiLineTextContent, getTextTitle, getTextHref) => {
        const textContent = renderTextContent(item, key, !!getTextTitle, getTextContent, getMultiLineTextContent);
        const title = getTextTitle
            ? getTextTitle(item)
            : typeof textContent === "string"
                ? textContent
                : "";
        return (_jsx(WithConditionalAnchor, { href: getTextHref?.(item), children: _jsx("div", { title: title, className: e("text", { "multi-line": !!getMultiLineTextContent }), children: textContent }) }));
    }, [renderTextContent]);
    const renderCellContent = useCallback(({ renderButton, renderMenu, renderRoleIcon, renderPrefixIcon, renderSuffixIcon, renderBadge, getTextContent, getMultiLineTextContent, getTextTitle, getTextHref, key, }, item) => {
        if (renderButton) {
            return renderButton(item);
        }
        if (renderMenu) {
            return renderMenuIcon(renderMenu, item);
        }
        return (_jsxs(_Fragment, { children: [renderRoleIconWithWrapper(renderRoleIcon, item), renderPrefixIconWithWrapper(renderPrefixIcon, item), renderTextContentWithWrapper(item, key, getTextContent, getMultiLineTextContent, getTextTitle, getTextHref), renderBadgeWithWrapper(renderBadge, item), renderSuffixIconWithWrapper(renderSuffixIcon, item)] }));
    }, [
        renderMenuIcon,
        renderRoleIconWithWrapper,
        renderPrefixIconWithWrapper,
        renderSuffixIconWithWrapper,
        renderBadgeWithWrapper,
        renderTextContentWithWrapper,
    ]);
    return {
        renderCellContent,
    };
};
//# sourceMappingURL=UiAsyncTableRow.js.map