import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment } from 'react';
import { styled, useStyleConfig, vui } from '../core';
import Link from '../link';
import { cs, filterUndefined, isReactText, isString } from '../utils';
import { useListContext } from './context';
import ListIcon from './listIcon';
import ListText from './listText';
/** Used as optional content wrapper when list item should be a link. */
const ListItemLink = vui((props, ref) => (_jsx(Link, { borderRadius: "none", className: "vui-listItemLink", h: "100%", px: 2, ref: ref, variant: "plain", w: "100%", ...props })));
/*@formatter:off*/
export const ListItemBase = styled.liBox `
  display: flex;
  line-height: normal;
  outline: none;
  transition-duration: fast;

  &[aria-disabled='true'] {
    cursor: not-allowed;
    user-select: none;
    opacity: 0.5;
  }

  &:focus-visible {
    z-index: 1;
  }
`;
/*@formatter:on*/
/**
 * Displays a list item with text and optional icons. Can be shown as interactive or selected.
 * Optionally wraps the content with an <a> element when rendering a link.
 */
export const ListItem = vui((props, ref) => {
    const mergedProps = { ...useListContext(), ...props };
    const { children, center, centerH, centerV = true, className, column, disabled, iconLeft, iconRight, isInteractive: isInteractiveProp, isSelected, isTruncated, itemLeft, itemRight, linkProps, onClick, text, ...rest } = mergedProps;
    const styles = useStyleConfig('List', useListContext());
    const { activeBg, hoverBg, selectedBg, ...itemStyles } = styles.item;
    const isLinkItem = linkProps !== undefined;
    const isInteractive = isInteractiveProp || isLinkItem || onClick !== undefined;
    const alignItems = (center || (column ? centerH : centerV)) && 'center';
    const flexDirection = column && 'column';
    const justifyContent = (center || (column ? centerV : centerH)) && 'center';
    const ContentWrapper = isLinkItem ? ListItemLink : Fragment;
    const interactiveProps = !disabled && isInteractive
        ? {
            cursor: 'pointer',
            focusVisibleRing: 2,
            hoverBg,
            activeBg,
            tabIndex: !isLinkItem ? 0 : undefined,
            userSelect: 'none',
        }
        : {};
    const aliasedProps = filterUndefined({
        alignItems,
        bg: isSelected ? selectedBg : undefined,
        'aria-disabled': disabled,
        flexDirection,
        justifyContent,
        px: isLinkItem ? 0 : undefined,
    });
    return (_jsx(ListItemBase, { bg: disabled ? rest.disabledBg || 'sandstone.95' : undefined, className: cs('vui-listItem', className), onClick: !disabled ? onClick : undefined, ref: ref, ...itemStyles, ...interactiveProps, ...aliasedProps, ...rest, children: _jsxs(ContentWrapper, { ...linkProps, children: [itemLeft, isString(iconLeft) ? _jsx(ListIcon, { mr: 1, name: iconLeft }) : iconLeft, children ?? (isReactText(text) ? _jsx(ListText, { isTruncated, text }) : text), isString(iconRight) ? _jsx(ListIcon, { ml: "auto", name: iconRight }) : iconRight, itemRight] }) }));
});
ListItem.displayName = 'ListItem';
export default ListItem;
//# sourceMappingURL=listItem.js.map