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, useState } from "react";
import { XMarkIcon } from "@navikt/aksel-icons";
import { Button } from "../button/index.js";
import { Popover } from "../popover/index.js";
import { Portal } from "../portal/index.js";
import { BodyShort } from "../typography/index.js";
import { useId } from "../utils-external/index.js";
import { FocusBoundary } from "../utils/components/focus-boundary/FocusBoundary.js";
import { FocusGuards } from "../utils/components/focus-guards/FocusGuards.js";
import { cl, composeEventHandlers } from "../utils/helpers/index.js";
import { useControllableState, useMergeRefs } from "../utils/hooks/index.js";
import { useI18n } from "../utils/i18n/i18n.hooks.js";
/**
 * Displays a popup with a word explanation when clicked. Used for looking up words or concepts within text.
 *
 * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/lookup)
 * @see 🏷️ {@link LookupProps}
 *
 * @example
 * ```jsx
 * <Lookup word="Lookup">
 *   Lookup component
 * </Lookup>
 * ```
 */
export const Lookup = forwardRef((_a, ref) => {
    var { word, children, className, strategy, onClick, id: idProp, open, defaultOpen = false, onOpenChange, onKeyDown, onKeyUp, tabIndex } = _a, rest = __rest(_a, ["word", "children", "className", "strategy", "onClick", "id", "open", "defaultOpen", "onOpenChange", "onKeyDown", "onKeyUp", "tabIndex"]);
    const [_open, _setOpen] = useControllableState({
        defaultValue: defaultOpen,
        value: open,
        onChange: onOpenChange,
    });
    const anchorRef = useRef(null);
    const [anchorEl, setAnchorEl] = useState(null);
    const mergedRef = useMergeRefs(anchorRef, ref, setAnchorEl);
    const contentRef = useRef(null);
    const triggerId = useId(idProp);
    const popoverContentId = `${triggerId}-content`;
    const popoverTitleId = `${triggerId}-title`;
    const translate = useI18n("global");
    return (React.createElement(React.Fragment, null,
        React.createElement("span", Object.assign({}, rest, { role: "button", id: triggerId, ref: mergedRef, className: cl("aksel-lookup-trigger", className), onClick: composeEventHandlers(onClick, () => _setOpen((old) => !old)), "aria-haspopup": "dialog", "aria-expanded": _open, "aria-controls": _open ? popoverContentId : undefined, tabIndex: tabIndex !== null && tabIndex !== void 0 ? tabIndex : 0, onKeyDown: composeEventHandlers(onKeyDown, (e) => {
                if (e.key === "Enter" && !e.repeat) {
                    e.preventDefault();
                    _setOpen((old) => !old);
                }
            }), onKeyUp: composeEventHandlers(onKeyUp, (e) => {
                if (e.key === " ") {
                    e.preventDefault();
                    _setOpen((old) => !old);
                }
            }) }), word),
        _open && (React.createElement(FocusGuards, null,
            React.createElement(FocusBoundary, { loop: true, trapped: true, initialFocus: contentRef, returnFocus: anchorRef, modal: true },
                React.createElement(Portal, null,
                    React.createElement(Popover, { anchorEl: anchorEl, open: _open, onClose: () => {
                            _setOpen(false);
                        }, strategy: strategy },
                        React.createElement(Popover.Content, { ref: contentRef, id: popoverContentId, role: "dialog", "aria-modal": "true", "aria-labelledby": popoverTitleId, "aria-keyshortcuts": "Escape", tabIndex: -1, className: "aksel-lookup__content" },
                            React.createElement(BodyShort, { weight: "semibold", className: "aksel-lookup__title", id: popoverTitleId }, word),
                            React.createElement("div", null, children),
                            React.createElement(Button, { type: "button", className: "aksel-lookup__close-button", size: "xsmall", variant: "tertiary", "data-color": "neutral", icon: React.createElement(XMarkIcon, { title: translate("close") }), onClick: () => _setOpen(false) })))))))));
});
export default Lookup;
//# sourceMappingURL=Lookup.js.map