'use client';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
// packages/ui/src/routing/Link.tsx
/**
 * @fileoverview Link component
 * @description Platform-agnostic link component for navigation
 *
 * @version 0.0.3
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */
import { Link as LinkIcon } from 'lucide-react';
import {} from 'react';
import { cn } from '@donotdev/components';
import { useConsent } from '@donotdev/core';
// Platform-specific hooks via conditional exports
import { useNavigate } from '@donotdev/ui/routing/hooks';
import { Icon } from '../components/common/icon';
/**
 * Link component
 *
 * @version 0.0.3
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */
export const Link = ({ path, replace, prefetch = true, children, icon, iconEnd = false, label, onClick, className, 'aria-label': ariaLabel, 'aria-describedby': ariaDescribedBy, target, rel, ...rest }) => {
    const navigate = useNavigate();
    const showCookieBanner = useConsent('showCookieBanner');
    // Special framework trigger: #cookie-settings
    if (path === '#cookie-settings') {
        const handleCookieSettings = (e) => {
            e.preventDefault();
            e.stopPropagation();
            try {
                showCookieBanner();
            }
            catch (error) {
                console.error('Failed to show cookie banner:', error);
            }
            if (onClick) {
                onClick(e);
            }
        };
        return (_jsxs("a", { href: "#cookie-settings", onClick: handleCookieSettings, className: className, style: {
                display: icon && (label || children) ? 'flex' : undefined,
                alignItems: icon && (label || children) ? 'center' : undefined,
                gap: icon && (label || children) ? 'var(--gap-sm)' : undefined,
                ...rest.style,
            }, "aria-label": ariaLabel || label || 'Cookie Settings', "aria-describedby": ariaDescribedBy, ...rest, children: [icon && !iconEnd && _jsx(Icon, { icon: icon, fallback: LinkIcon }), label && _jsx("span", { children: label }), children, icon && iconEnd && _jsx(Icon, { icon: icon, fallback: LinkIcon })] }));
    }
    // Auto-detect external URLs
    const isExternal = path.startsWith('http://') ||
        path.startsWith('https://') ||
        path.startsWith('mailto:') ||
        path.startsWith('tel:');
    const handleClick = (e) => {
        // External links: let browser handle naturally
        if (isExternal) {
            if (onClick) {
                onClick(e);
            }
            return; // Don't prevent default for external links
        }
        // Internal links: use framework navigation
        e.preventDefault();
        if (onClick) {
            onClick(e);
        }
        // Ensure path is absolute (starts with /) to prevent relative navigation issues
        // but preserve intentional relative paths (starting with '.' or '..')
        const absolutePath = path.startsWith('/') || path.startsWith('.') ? path : `/${path}`;
        navigate(absolutePath, { replace });
    };
    const accessibilityProps = {
        'aria-label': ariaLabel || label,
        'aria-describedby': ariaDescribedBy,
    };
    const linkContent = (_jsxs(_Fragment, { children: [icon && !iconEnd && _jsx(Icon, { icon: icon, fallback: LinkIcon }), label && _jsx("span", { children: label }), children, icon && iconEnd && _jsx(Icon, { icon: icon, fallback: LinkIcon })] }));
    // External links: use regular <a> with target/rel
    if (isExternal) {
        return (_jsx("a", { href: path, onClick: handleClick, target: target || '_blank', rel: rel || 'noopener noreferrer', className: className, style: {
                display: icon && (label || children) ? 'flex' : undefined,
                alignItems: icon && (label || children) ? 'center' : undefined,
                gap: icon && (label || children) ? 'var(--gap-sm)' : undefined,
            }, ...accessibilityProps, ...rest, children: linkContent }));
    }
    // Internal links: use framework Link
    return (_jsx("a", { href: path, onClick: handleClick, className: className, style: {
            display: icon && (label || children) ? 'flex' : undefined,
            alignItems: icon && (label || children) ? 'center' : undefined,
            gap: icon && (label || children) ? 'var(--gap-sm)' : undefined,
        }, ...accessibilityProps, ...rest, children: linkContent }));
};
