import { jsx as _jsx } from "react/jsx-runtime";
import NextLink from "next/link";
import * as Path from "../utils/path";
import { sanitizeInlineHtml, sanitizeNavigationUrl } from "../utils/security";
import { useEdenContext } from "./context";
export default function Link({ ref, title, children, ...props }) {
    const { lang, config, isConnected } = useEdenContext();
    const safeTitle = !children ? sanitizeInlineHtml(title) : undefined;
    const innerHTML = safeTitle ? { __html: safeTitle } : undefined;
    const href = props.as || props.href;
    const isObject = typeof href === "object";
    const path = sanitizeNavigationUrl(isObject ? href.pathname : href) || "#";
    const staticPath = Path.toStatic(path);
    const isExternalHttp = /^https?:\/\//i.test(staticPath);
    const isExternal = isExternalHttp ||
        staticPath.startsWith("mailto:") ||
        staticPath.startsWith("tel:");
    const localizedPath = isExternal
        ? staticPath
        : Path.toLocalized(path, lang, config.langPrefix);
    const asPath = isObject
        ? { ...href, pathname: localizedPath }
        : localizedPath;
    const target = isExternalHttp ? "_blank" : undefined;
    const rel = isExternalHttp ? "noopener noreferrer" : undefined;
    if (isConnected || isExternal) {
        return (_jsx("a", { ref: ref, dangerouslySetInnerHTML: innerHTML, target: target, rel: rel, ...props, href: localizedPath, children: children }));
    }
    return (_jsx(NextLink, { ref: ref, dangerouslySetInnerHTML: innerHTML, target: target, rel: rel, ...props, href: asPath, prefetch: undefined, children: children }));
}
