import { jsx as _jsx } from "react/jsx-runtime";
// packages/ui/src/components/common/icon.tsx
/**
 * @fileoverview Icon component for UI package
 * @description Unified icon rendering for Lucide icons, emojis, and custom ReactNode content
 *
 * @version 0.0.1
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */
import * as LucideIcons from 'lucide-react';
import { isValidElement } from 'react';
import { cn } from '@donotdev/components';
function getLucideIcon(name) {
    if (!name)
        return LucideIcons.Palette;
    const cleanIconName = name.replace(/['"]/g, '');
    return LucideIcons[cleanIconName] || LucideIcons.Palette;
}
/**
 * Icon - Unified icon component (FLEXIBLE RUNTIME COMPONENT)
 *
 * **This is the flexible runtime component** - supports Lucide components, emojis, strings, and custom ReactNode.
 *
 * **For PageMeta.icon:** Only lucide-react JSX components are supported (extracted at build time).
 * **For Icon component:** All formats are supported (resolved at runtime).
 *
 * @example
 * ```tsx
 * // Runtime usage - all formats work
 * <Icon icon="🚀" /> // Emoji
 * <Icon icon="Rocket" /> // Lucide name string
 * <Icon icon={<Rocket />} /> // Lucide component
 * <Icon icon={<CustomIcon />} /> // Custom component
 * ```
 *
 * @version 0.0.1
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */
const Icon = ({ icon, fallback, className, ariaHidden = false, }) => {
    const iconToRender = icon || fallback;
    if (!iconToRender)
        return null;
    const sizeClass = 'dndev-size-md';
    if (typeof iconToRender === 'function' ||
        (typeof iconToRender === 'object' &&
            iconToRender !== null &&
            'render' in iconToRender)) {
        const IconComponent = iconToRender;
        return (_jsx(IconComponent, { className: cn(sizeClass, className), style: { flexShrink: 0 }, "aria-hidden": ariaHidden }));
    }
    if (typeof iconToRender === 'string') {
        // Check if string is an emoji (not a valid identifier)
        // Valid identifiers: alphanumeric, underscore, hyphen (Lucide icon names)
        // Emojis: anything else (Unicode symbols, emojis, etc.)
        const isValidIdentifier = /^[a-zA-Z0-9_-]+$/.test(iconToRender);
        const isEmoji = !isValidIdentifier;
        if (isEmoji) {
            return (_jsx("span", { className: cn(sizeClass, className), style: {
                    display: 'inline-flex',
                    alignItems: 'center',
                    justifyContent: 'center',
                }, "aria-hidden": ariaHidden, children: iconToRender }));
        }
        const LucideIcon = getLucideIcon(iconToRender);
        return (_jsx(LucideIcon, { className: cn(sizeClass, className), style: { flexShrink: 0 }, "aria-hidden": ariaHidden }));
    }
    if (isValidElement(iconToRender)) {
        return (_jsx("span", { className: className, style: {
                display: 'inline-flex',
                alignItems: 'center',
                justifyContent: 'center',
            }, "aria-hidden": ariaHidden, children: iconToRender }));
    }
    return (_jsx("span", { className: className, style: {
            display: 'inline-flex',
            alignItems: 'center',
            justifyContent: 'center',
        }, "aria-hidden": ariaHidden, children: iconToRender }));
};
export default Icon;
export { Icon };
