var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { forwardRef, useRef, } from "react";
import { ArrowRightIcon } from "@navikt/aksel-icons";
import { cl, composeEventHandlers, createStrictContext, ownerWindow, } from "../../helpers/index.js";
import { useMergeRefs } from "../../hooks/index.js";
import { Slot } from "../slot/Slot.js";
const { Provider: LinkAnchorContextProvider, useContext: useLinkAnchorContext, } = createStrictContext({
    name: "LinkAnchorOverlayContext",
});
/*
 * NB: Clicks on the overlay are captured with onClick. This does not work with middle-mouse-click.
 * We could capture such click with onAuxClick, but last time we tried that,
 * "forwarding" the click with dispatchEvent didn't work properly.
 * - Chrome: Worked if we dispatched a regular click event.
 * - Firefox: Did not work.
 * - Safari: Opened the link in the same tab (tested in BrowserStack).
 * We could use window.open() instead, but this would not run on(Aux)Click-callbacks on the link.
 * We consider this unacceptable since many rely on this for tracking etc.
 */
const LinkAnchorOverlay = forwardRef((_a, forwardedRef) => {
    var { children, asChild, className, onClick } = _a, restProps = __rest(_a, ["children", "asChild", "className", "onClick"]);
    const anchorRef = useRef(null);
    const Component = asChild ? Slot : "div";
    return (React.createElement(LinkAnchorContextProvider, { anchorRef: anchorRef },
        React.createElement(Component, Object.assign({ ref: forwardedRef }, restProps, { className: cl("aksel-link-anchor__overlay", className), onClick: composeEventHandlers(onClick, (e) => {
                var _a;
                if (e.target === anchorRef.current ||
                    isTextSelected(anchorRef.current)) {
                    return;
                }
                const event = new MouseEvent("click", {
                    bubbles: true,
                    cancelable: true,
                    view: window,
                    ctrlKey: e.ctrlKey,
                    shiftKey: e.shiftKey,
                    altKey: e.altKey,
                    metaKey: e.metaKey,
                    button: e.button,
                    screenX: e.screenX,
                    screenY: e.screenY,
                    clientX: e.clientX,
                    clientY: e.clientY,
                });
                (_a = anchorRef.current) === null || _a === void 0 ? void 0 : _a.dispatchEvent(event);
            }) }), children)));
});
const LinkAnchor = forwardRef((_a, forwardedRef) => {
    var { children, asChild, className } = _a, restProps = __rest(_a, ["children", "asChild", "className"]);
    const context = useLinkAnchorContext(false);
    const mergedRefs = useMergeRefs(forwardedRef, context === null || context === void 0 ? void 0 : context.anchorRef);
    const Component = asChild ? Slot : "a";
    return (React.createElement(Component, Object.assign({ ref: mergedRefs }, restProps, { className: cl("aksel-link-anchor", className) }), children));
});
const LinkAnchorArrow = forwardRef((_a, forwardedRef) => {
    var { className } = _a, restProps = __rest(_a, ["className"]);
    return (React.createElement(ArrowRightIcon, Object.assign({ ref: forwardedRef, "aria-hidden": true, className: cl("aksel-link-anchor__arrow", className) }, restProps)));
});
/* -------------------------- LinkAnchor Utilities -------------------------- */
function isTextSelected(refElement) {
    var _a, _b;
    return !!((_b = (_a = ownerWindow(refElement)) === null || _a === void 0 ? void 0 : _a.getSelection()) === null || _b === void 0 ? void 0 : _b.toString());
}
export { LinkAnchor, LinkAnchorArrow, LinkAnchorOverlay };
//# sourceMappingURL=LinkAnchor.js.map