import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useMemo, useRef } from 'react';
import { Menu, MenuTrigger, Space, Text, CloseIcon, PlusIcon, CubeIcon } from '@cube-dev/ui-kit';
import { getTypeIcon } from '../utils';
import { PrimaryKeyIcon } from '../icons/PrimaryKeyIcon';
import { NonPublicIcon } from '../icons/NonPublicIcon';
import { ItemInfoIcon } from '../icons/ItemInfoIcon';
import { useEvent, useShownMemberName } from '../hooks';
import { ListMemberButton } from './ListMemberButton';
import { FilterByMemberButton } from './FilterByMemberButton';
import { FilteredLabel } from './FilteredLabel';
import { InstanceTooltipProvider } from './InstanceTooltipProvider';
export function ListMember(props) {
    const textRef = useRef(null);
    const { cube, filterString, category, member, memberViewType, isMissing, isSelected, isFiltered, isImported, onAddFilter, onRemoveFilter, onToggle, } = props;
    const type = 'type' in member ? member.type : undefined;
    const name = member.name.replace(`${cube.name}.`, '').trim();
    const title = 'shortTitle' in member ? member.shortTitle : undefined;
    // @ts-ignore
    const description = member.description;
    const { shownMemberName } = useShownMemberName({
        cubeName: cube.name,
        cubeTitle: 'title' in cube ? cube.title : undefined,
        memberName: name,
        memberTitle: title,
        type: memberViewType,
    });
    const onFilterPress = useEvent(() => !isFiltered ? onAddFilter?.(member.name) : onRemoveFilter?.(member.name));
    const filterMenu = useMemo(() => {
        const dangerProps = isFiltered
            ? {
                color: '#danger-text',
            }
            : {};
        return (_jsxs(MenuTrigger, { children: [_jsx(FilterByMemberButton, { isAngular: true, type: category.replace(/s$/, ''), isFiltered: true, ...(isMissing ? { color: '#danger-text' } : {}) }), _jsxs(Menu, { disabledKeys: isMissing ? ['add'] : [], onAction: (key) => {
                        switch (key) {
                            case 'add':
                                onAddFilter?.(member.name);
                                break;
                            case 'remove':
                                onRemoveFilter?.(member.name);
                                break;
                            default:
                                return;
                        }
                    }, children: [_jsx(Menu.Item, { icon: _jsx(PlusIcon, {}), children: "Filter by This Member" }, "add"), _jsx(Menu.Item, { icon: _jsx(CloseIcon, { ...dangerProps }), children: _jsx(Text, { ...dangerProps, children: "Remove All" }) }, "remove")] })] }));
    }, [category, isMissing, member.name, onAddFilter, onRemoveFilter, isFiltered]);
    return (_jsx(InstanceTooltipProvider, { name: name, fullName: member.name, type: category.replace(/s$/, ''), title: title, overflowRef: textRef, children: _jsxs(ListMemberButton, { qa: "MemberButton", qaVal: member.name, icon: getTypeIcon(category === 'segments' ? 'filter' : type), "data-member": category.replace(/s$/, ''), mods: { selected: isSelected, missing: isMissing }, onPress: () => onToggle?.(member.name), children: [_jsx(Text, { ref: textRef, ellipsis: true, children: filterString ? (_jsx(FilteredLabel, { text: shownMemberName, filter: filterString })) : (shownMemberName) }), _jsxs(Space, { gap: ".5x", children: [description ||
                            isImported ||
                            ('primaryKey' in member && member.primaryKey) ||
                            ('public' in member && member.public === false) ? (_jsxs(Space, { gap: "1x", color: "#dark.6", children: [description || isImported ? (_jsx(ItemInfoIcon, { description: isImported ? (_jsxs(_Fragment, { children: [description ? (_jsxs(_Fragment, { children: [description, _jsx("br", {}), _jsx("br", {})] })) : null, _jsx(Text, { preset: "t4", children: "This member is imported from another cube:" }), _jsx("br", {}), _jsx(CubeIcon, {}), " ", _jsx("b", { children: member.name.split('.')[0] })] })) : (description) })) : undefined, 'primaryKey' in member && member.primaryKey ? (_jsx(PrimaryKeyIcon, { color: 'dark-02' })) : undefined, 'public' in member && member.public === false ? _jsx(NonPublicIcon, {}) : undefined] })) : null, onAddFilter || onRemoveFilter ? (isFiltered && !isMissing ? (filterMenu) : (_jsx(FilterByMemberButton, { isAngular: true, isFiltered: isFiltered || false, type: category.replace(/s$/, ''), onPress: onFilterPress }))) : undefined] })] }) }));
}
//# sourceMappingURL=ListMember.js.map