/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.0.0-canary-127afd4
 * Released under the MIT license.
 */

import { jsx as _jsx } from "react/jsx-runtime";
import { forwardRef, useMemo } from 'react';
import { BASE_STYLES, COLOR_STYLES, extractStyles, filterBaseProps, tasty, TEXT_STYLES, } from '../../../tasty/index.js';
import { highlightText } from '../highlightText.js';
import { Text, TEXT_PROP_MAP } from '../Text.js';
import { useAutoTooltip } from '../use-auto-tooltip.js';
const STYLE_LIST = [...BASE_STYLES, ...TEXT_STYLES, ...COLOR_STYLES];
const TextItemElement = tasty(Text, {
    qa: 'TextItem',
    styles: {
        display: 'inline-block',
        verticalAlign: 'bottom',
        width: 'max 100%',
        whiteSpace: 'nowrap',
        overflow: 'hidden',
        textOverflow: 'ellipsis',
    },
});
export const TextItem = forwardRef(function TextItem(props, ref) {
    const { children, highlight, highlightCaseSensitive = false, highlightStyles, tooltip = true, tooltipPlacement = 'top', ...restProps } = props;
    // Extract style props (preset, color, etc.) to pass via styles prop
    const styles = extractStyles(restProps, STYLE_LIST, {}, TEXT_PROP_MAP);
    const { labelRef, renderWithTooltip } = useAutoTooltip({
        tooltip,
        children,
    });
    // Process children with highlight if applicable
    const processedChildren = useMemo(() => {
        if (typeof children === 'string' && highlight) {
            return highlightText(children, highlight, highlightCaseSensitive, highlightStyles);
        }
        return children;
    }, [children, highlight, highlightCaseSensitive, highlightStyles]);
    const renderElement = (tooltipTriggerProps, tooltipRef) => {
        // Merge refs
        const handleRef = (element) => {
            // Set component forwarded ref
            if (typeof ref === 'function') {
                ref(element);
            }
            else if (ref) {
                ref.current = element;
            }
            // Set tooltip ref
            if (tooltipRef) {
                tooltipRef.current = element;
            }
            // Set label ref for overflow detection
            labelRef(element);
        };
        return (_jsx(TextItemElement, { ...filterBaseProps(restProps, { eventProps: true }), ...tooltipTriggerProps, ref: handleRef, styles: styles, children: processedChildren }));
    };
    return renderWithTooltip(renderElement, tooltipPlacement);
});
TextItem.displayName = 'TextItem';

        