import * as Solid from 'solid-js';
import { deepEqual, exactPathTest, functionalUpdate, isDangerousProtocol, preloadWarning, removeTrailingSlash, } from '@tanstack/router-core';
import { isServer } from '@tanstack/router-core/isServer';
import { Dynamic } from '@solidjs/web';
import { useRouter } from './useRouter';
import { useIntersectionObserver } from './utils';
import { useHydrated } from './ClientOnly';
function mergeRefs(...refs) {
    return (el) => {
        for (const ref of refs) {
            if (typeof ref === 'function') {
                ref(el);
            }
        }
    };
}
function splitProps(props, keys) {
    const _local = {};
    const _rest = {};
    // A safe way to polyfill splitProps if native getter copy is too complex
    // is just to return [props, Solid.omit(props, keys)] but it modifies typing.
    // Actually, Solid.omit exists!
    // Note: Solid.omit uses rest params (...keys), so we must spread the array.
    return [props, Solid.omit(props, ...keys)];
}
const timeoutMap = new WeakMap();
export function useLinkProps(options) {
    const router = useRouter();
    const [isTransitioning, setIsTransitioning] = Solid.createSignal(false);
    const shouldHydrateHash = !isServer && !!router.options.ssr;
    const hasHydrated = useHydrated();
    let hasRenderFetched = false;
    const [local, rest] = splitProps(Solid.merge({
        activeProps: STATIC_ACTIVE_PROPS_GET,
        inactiveProps: STATIC_INACTIVE_PROPS_GET,
    }, options), [
        'activeProps',
        'inactiveProps',
        'activeOptions',
        'to',
        'preload',
        'preloadDelay',
        'hashScrollIntoView',
        'replace',
        'startTransition',
        'resetScroll',
        'viewTransition',
        'target',
        'disabled',
        'style',
        'class',
        'onClick',
        'onBlur',
        'onFocus',
        'onMouseEnter',
        'onMouseLeave',
        'onMouseOver',
        'onMouseOut',
        'onTouchStart',
        'ignoreBlocker',
    ]);
    const [_, propsSafeToSpread] = splitProps(rest, [
        'params',
        'search',
        'hash',
        'state',
        'mask',
        'reloadDocument',
        'unsafeRelative',
    ]);
    const currentLocation = Solid.createMemo(() => router.stores.location.state, undefined, { equals: (prev, next) => prev?.href === next?.href });
    const _options = () => options;
    const next = Solid.createMemo(() => {
        // Rebuild when inherited search/hash or the current route context changes.
        const _fromLocation = currentLocation();
        const options = { _fromLocation, ..._options() };
        // untrack because router-core will also access stores, which are signals in solid
        return Solid.untrack(() => router.buildLocation(options));
    });
    const hrefOption = Solid.createMemo(() => {
        if (_options().disabled)
            return undefined;
        // Use publicHref - it contains the correct href for display
        // When a rewrite changes the origin, publicHref is the full URL
        // Otherwise it's the origin-stripped path
        // This avoids constructing URL objects in the hot path
        const location = next().maskedLocation ?? next();
        const publicHref = location.publicHref;
        const external = location.external;
        if (external) {
            return { href: publicHref, external: true };
        }
        return {
            href: router.history.createHref(publicHref) || '/',
            external: false,
        };
    });
    const externalLink = Solid.createMemo(() => {
        const _href = hrefOption();
        if (_href?.external) {
            // Block dangerous protocols for external links
            if (isDangerousProtocol(_href.href, router.protocolAllowlist)) {
                if (process.env.NODE_ENV !== 'production') {
                    console.warn(`Blocked Link with dangerous protocol: ${_href.href}`);
                }
                return undefined;
            }
            return _href.href;
        }
        const to = _options().to;
        const safeInternal = isSafeInternal(to);
        if (safeInternal)
            return undefined;
        if (typeof to !== 'string' || to.indexOf(':') === -1)
            return undefined;
        try {
            new URL(to);
            // Block dangerous protocols like javascript:, blob:, data:
            if (isDangerousProtocol(to, router.protocolAllowlist)) {
                if (process.env.NODE_ENV !== 'production') {
                    console.warn(`Blocked Link with dangerous protocol: ${to}`);
                }
                return undefined;
            }
            return to;
        }
        catch { }
        return undefined;
    });
    const preload = Solid.createMemo(() => {
        if (_options().reloadDocument || externalLink()) {
            return false;
        }
        return local.preload ?? router.options.defaultPreload;
    });
    const preloadDelay = () => local.preloadDelay ?? router.options.defaultPreloadDelay ?? 0;
    const isActive = Solid.createMemo(() => {
        if (externalLink())
            return false;
        const activeOptions = local.activeOptions;
        const current = currentLocation();
        const nextLocation = next();
        if (activeOptions?.exact) {
            const testExact = exactPathTest(current.pathname, nextLocation.pathname, router.basepath);
            if (!testExact) {
                return false;
            }
        }
        else {
            const currentPath = removeTrailingSlash(current.pathname, router.basepath);
            const nextPath = removeTrailingSlash(nextLocation.pathname, router.basepath);
            const pathIsFuzzyEqual = currentPath.startsWith(nextPath) &&
                (currentPath.length === nextPath.length ||
                    currentPath[nextPath.length] === '/');
            if (!pathIsFuzzyEqual) {
                return false;
            }
        }
        if (activeOptions?.includeSearch ?? true) {
            const searchTest = deepEqual(current.search, nextLocation.search, {
                partial: !activeOptions?.exact,
                ignoreUndefined: !activeOptions?.explicitUndefined,
            });
            if (!searchTest) {
                return false;
            }
        }
        if (activeOptions?.includeHash) {
            const currentHash = shouldHydrateHash && !hasHydrated() ? '' : current.hash;
            return currentHash === nextLocation.hash;
        }
        return true;
    });
    const doPreload = () => router
        .preloadRoute({ ..._options(), _builtLocation: next() })
        .catch((err) => {
        console.warn(err);
        console.warn(preloadWarning);
    });
    const preloadViewportIoCallback = (entry) => {
        if (entry?.isIntersecting) {
            doPreload();
        }
    };
    const [ref, setRefSignal] = Solid.createSignal(null);
    const setRef = (el) => {
        Solid.runWithOwner(null, () => {
            setRefSignal(el);
        });
    };
    useIntersectionObserver(ref, preloadViewportIoCallback, { rootMargin: '100px' }, { disabled: !!local.disabled || !(Solid.untrack(preload) === 'viewport') });
    Solid.createEffect(preload, (preloadValue) => {
        if (hasRenderFetched) {
            return;
        }
        if (!local.disabled && preloadValue === 'render') {
            Solid.untrack(() => doPreload());
            hasRenderFetched = true;
        }
    });
    if (Solid.untrack(externalLink)) {
        const externalHref = Solid.untrack(externalLink);
        return Solid.merge(propsSafeToSpread, {
            // ref: mergeRefs(setRef, _options().ref),
            href: externalHref,
        }, splitProps(local, [
            'target',
            'disabled',
            'style',
            'class',
            'onClick',
            'onBlur',
            'onFocus',
            'onMouseEnter',
            'onMouseLeave',
            'onMouseOut',
            'onMouseOver',
            'onTouchStart',
        ])[0]);
    }
    // The click handler
    const handleClick = (e) => {
        // Check actual element's target attribute as fallback
        const elementTarget = e.currentTarget.getAttribute('target');
        const effectiveTarget = local.target !== undefined ? local.target : elementTarget;
        if (!local.disabled &&
            !isCtrlEvent(e) &&
            !e.defaultPrevented &&
            (!effectiveTarget || effectiveTarget === '_self') &&
            e.button === 0) {
            e.preventDefault();
            Solid.runWithOwner(null, () => {
                setIsTransitioning(true);
            });
            const unsub = router.subscribe('onResolved', () => {
                unsub();
                Solid.runWithOwner(null, () => {
                    setIsTransitioning(false);
                });
            });
            // All is well? Navigate!
            // N.B. we don't call `router.commitLocation(next) here because we want to run `validateSearch` before committing
            router.navigate({
                ..._options(),
                replace: local.replace,
                resetScroll: local.resetScroll,
                hashScrollIntoView: local.hashScrollIntoView,
                startTransition: local.startTransition,
                viewTransition: local.viewTransition,
                ignoreBlocker: local.ignoreBlocker,
            });
        }
    };
    const enqueueIntentPreload = (e) => {
        if (local.disabled || preload() !== 'intent')
            return;
        if (!preloadDelay()) {
            doPreload();
            return;
        }
        const eventTarget = e.currentTarget || e.target;
        if (!eventTarget || timeoutMap.has(eventTarget))
            return;
        timeoutMap.set(eventTarget, setTimeout(() => {
            timeoutMap.delete(eventTarget);
            doPreload();
        }, preloadDelay()));
    };
    const handleTouchStart = (_) => {
        if (local.disabled || preload() !== 'intent')
            return;
        doPreload();
    };
    const handleLeave = (e) => {
        if (local.disabled)
            return;
        const eventTarget = e.currentTarget || e.target;
        if (eventTarget) {
            const id = timeoutMap.get(eventTarget);
            clearTimeout(id);
            timeoutMap.delete(eventTarget);
        }
    };
    const simpleStyling = Solid.createMemo(() => local.activeProps === STATIC_ACTIVE_PROPS_GET &&
        local.inactiveProps === STATIC_INACTIVE_PROPS_GET &&
        local.class === undefined &&
        local.style === undefined);
    const onClick = createComposedHandler(() => local.onClick, handleClick);
    const onBlur = createComposedHandler(() => local.onBlur, handleLeave);
    const onFocus = createComposedHandler(() => local.onFocus, enqueueIntentPreload);
    const onMouseEnter = createComposedHandler(() => local.onMouseEnter, enqueueIntentPreload);
    const onMouseOver = createComposedHandler(() => local.onMouseOver, enqueueIntentPreload);
    const onMouseLeave = createComposedHandler(() => local.onMouseLeave, handleLeave);
    const onMouseOut = createComposedHandler(() => local.onMouseOut, handleLeave);
    const onTouchStart = createComposedHandler(() => local.onTouchStart, handleTouchStart);
    const resolvedProps = Solid.createMemo(() => {
        const active = isActive();
        const base = {
            href: hrefOption()?.href,
            ref: mergeRefs(setRef, _options().ref),
            onClick,
            onBlur,
            onFocus,
            onMouseEnter,
            onMouseOver,
            onMouseLeave,
            onMouseOut,
            onTouchStart,
            disabled: !!local.disabled,
            target: local.target,
            ...(local.disabled && STATIC_DISABLED_PROPS),
            ...(isTransitioning() && STATIC_TRANSITIONING_ATTRIBUTES),
        };
        if (simpleStyling()) {
            return {
                ...base,
                ...(active && STATIC_DEFAULT_ACTIVE_ATTRIBUTES),
            };
        }
        const activeProps = active
            ? (functionalUpdate(local.activeProps, {}) ?? EMPTY_OBJECT)
            : EMPTY_OBJECT;
        const inactiveProps = active
            ? EMPTY_OBJECT
            : functionalUpdate(local.inactiveProps, {});
        const style = {
            ...local.style,
            ...activeProps.style,
            ...inactiveProps.style,
        };
        const className = [local.class, activeProps.class, inactiveProps.class]
            .filter(Boolean)
            .join(' ');
        return {
            ...activeProps,
            ...inactiveProps,
            ...base,
            ...(Object.keys(style).length ? { style } : undefined),
            ...(className ? { class: className } : undefined),
            ...(active && STATIC_ACTIVE_ATTRIBUTES),
        };
    });
    return Solid.merge(propsSafeToSpread, resolvedProps);
}
const STATIC_ACTIVE_PROPS = { class: 'active' };
const STATIC_ACTIVE_PROPS_GET = () => STATIC_ACTIVE_PROPS;
const EMPTY_OBJECT = {};
const STATIC_INACTIVE_PROPS_GET = () => EMPTY_OBJECT;
const STATIC_DEFAULT_ACTIVE_ATTRIBUTES = {
    class: 'active',
    'data-status': 'active',
    'aria-current': 'page',
};
const STATIC_DISABLED_PROPS = {
    role: 'link',
    'aria-disabled': 'true',
};
const STATIC_ACTIVE_ATTRIBUTES = {
    'data-status': 'active',
    'aria-current': 'page',
};
const STATIC_TRANSITIONING_ATTRIBUTES = {
    'data-transitioning': 'transitioning',
};
/** Call a JSX.EventHandlerUnion with the event. */
function callHandler(event, handler) {
    if (typeof handler === 'function') {
        handler(event);
    }
    else {
        handler[0](handler[1], event);
    }
    return event.defaultPrevented;
}
function createComposedHandler(getHandler, fallback) {
    return (event) => {
        const handler = getHandler();
        if (!handler || !callHandler(event, handler))
            fallback(event);
    };
}
export function createLink(Comp) {
    return (props) => <Link {...props} _asChild={Comp}/>;
}
export const Link = (props) => {
    const [local, rest] = splitProps(props, [
        '_asChild',
        'children',
    ]);
    const [_, linkProps] = splitProps(useLinkProps(rest), [
        'type',
    ]);
    // Resolve children once using Solid.children to avoid
    // re-accessing the children getter (which in Solid 2.0 would
    // re-invoke createComponent each time for JSX children).
    const resolvedChildren = Solid.children(() => local.children);
    const children = () => {
        const ch = resolvedChildren();
        if (typeof ch === 'function') {
            return ch({
                get isActive() {
                    return linkProps['data-status'] === 'active';
                },
                get isTransitioning() {
                    return linkProps['data-transitioning'] === 'transitioning';
                },
            });
        }
        return ch;
    };
    if (local._asChild === 'svg') {
        const [_, svgLinkProps] = splitProps(linkProps, ['class']);
        return (<svg>
        <a {...svgLinkProps}>{children()}</a>
      </svg>);
    }
    if (!local._asChild) {
        return <a {...linkProps}>{children()}</a>;
    }
    return (<Dynamic component={local._asChild} {...linkProps}>
      {children()}
    </Dynamic>);
};
function isCtrlEvent(e) {
    return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
}
function isSafeInternal(to) {
    if (typeof to !== 'string')
        return false;
    const zero = to.charCodeAt(0);
    if (zero === 47)
        return to.charCodeAt(1) !== 47; // '/' but not '//'
    return zero === 46; // '.', '..', './', '../'
}
export const linkOptions = (options) => {
    return options;
};
//# sourceMappingURL=link.jsx.map