"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LinkAnchorOverlay = exports.LinkAnchorArrow = exports.LinkAnchor = void 0;
const react_1 = __importStar(require("react"));
const aksel_icons_1 = require("@navikt/aksel-icons");
const helpers_1 = require("../../helpers");
const hooks_1 = require("../../hooks");
const Slot_1 = require("../slot/Slot");
const { Provider: LinkAnchorContextProvider, useContext: useLinkAnchorContext, } = (0, helpers_1.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 = (0, react_1.forwardRef)((_a, forwardedRef) => {
    var { children, asChild, className, onClick } = _a, restProps = __rest(_a, ["children", "asChild", "className", "onClick"]);
    const anchorRef = (0, react_1.useRef)(null);
    const Component = asChild ? Slot_1.Slot : "div";
    return (react_1.default.createElement(LinkAnchorContextProvider, { anchorRef: anchorRef },
        react_1.default.createElement(Component, Object.assign({ ref: forwardedRef }, restProps, { className: (0, helpers_1.cl)("aksel-link-anchor__overlay", className), onClick: (0, helpers_1.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)));
});
exports.LinkAnchorOverlay = LinkAnchorOverlay;
const LinkAnchor = (0, react_1.forwardRef)((_a, forwardedRef) => {
    var { children, asChild, className } = _a, restProps = __rest(_a, ["children", "asChild", "className"]);
    const context = useLinkAnchorContext(false);
    const mergedRefs = (0, hooks_1.useMergeRefs)(forwardedRef, context === null || context === void 0 ? void 0 : context.anchorRef);
    const Component = asChild ? Slot_1.Slot : "a";
    return (react_1.default.createElement(Component, Object.assign({ ref: mergedRefs }, restProps, { className: (0, helpers_1.cl)("aksel-link-anchor", className) }), children));
});
exports.LinkAnchor = LinkAnchor;
const LinkAnchorArrow = (0, react_1.forwardRef)((_a, forwardedRef) => {
    var { className } = _a, restProps = __rest(_a, ["className"]);
    return (react_1.default.createElement(aksel_icons_1.ArrowRightIcon, Object.assign({ ref: forwardedRef, "aria-hidden": true, className: (0, helpers_1.cl)("aksel-link-anchor__arrow", className) }, restProps)));
});
exports.LinkAnchorArrow = LinkAnchorArrow;
/* -------------------------- LinkAnchor Utilities -------------------------- */
function isTextSelected(refElement) {
    var _a, _b;
    return !!((_b = (_a = (0, helpers_1.ownerWindow)(refElement)) === null || _a === void 0 ? void 0 : _a.getSelection()) === null || _b === void 0 ? void 0 : _b.toString());
}
//# sourceMappingURL=LinkAnchor.js.map