"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.Lookup = void 0;
const react_1 = __importStar(require("react"));
const aksel_icons_1 = require("@navikt/aksel-icons");
const button_1 = require("../button");
const popover_1 = require("../popover");
const portal_1 = require("../portal");
const typography_1 = require("../typography");
const utils_external_1 = require("../utils-external");
const FocusBoundary_1 = require("../utils/components/focus-boundary/FocusBoundary");
const FocusGuards_1 = require("../utils/components/focus-guards/FocusGuards");
const helpers_1 = require("../utils/helpers");
const hooks_1 = require("../utils/hooks");
const i18n_hooks_1 = require("../utils/i18n/i18n.hooks");
/**
 * 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>
 * ```
 */
exports.Lookup = (0, react_1.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] = (0, hooks_1.useControllableState)({
        defaultValue: defaultOpen,
        value: open,
        onChange: onOpenChange,
    });
    const anchorRef = (0, react_1.useRef)(null);
    const [anchorEl, setAnchorEl] = (0, react_1.useState)(null);
    const mergedRef = (0, hooks_1.useMergeRefs)(anchorRef, ref, setAnchorEl);
    const contentRef = (0, react_1.useRef)(null);
    const triggerId = (0, utils_external_1.useId)(idProp);
    const popoverContentId = `${triggerId}-content`;
    const popoverTitleId = `${triggerId}-title`;
    const translate = (0, i18n_hooks_1.useI18n)("global");
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("span", Object.assign({}, rest, { role: "button", id: triggerId, ref: mergedRef, className: (0, helpers_1.cl)("aksel-lookup-trigger", className), onClick: (0, helpers_1.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: (0, helpers_1.composeEventHandlers)(onKeyDown, (e) => {
                if (e.key === "Enter" && !e.repeat) {
                    e.preventDefault();
                    _setOpen((old) => !old);
                }
            }), onKeyUp: (0, helpers_1.composeEventHandlers)(onKeyUp, (e) => {
                if (e.key === " ") {
                    e.preventDefault();
                    _setOpen((old) => !old);
                }
            }) }), word),
        _open && (react_1.default.createElement(FocusGuards_1.FocusGuards, null,
            react_1.default.createElement(FocusBoundary_1.FocusBoundary, { loop: true, trapped: true, initialFocus: contentRef, returnFocus: anchorRef, modal: true },
                react_1.default.createElement(portal_1.Portal, null,
                    react_1.default.createElement(popover_1.Popover, { anchorEl: anchorEl, open: _open, onClose: () => {
                            _setOpen(false);
                        }, strategy: strategy },
                        react_1.default.createElement(popover_1.Popover.Content, { ref: contentRef, id: popoverContentId, role: "dialog", "aria-modal": "true", "aria-labelledby": popoverTitleId, "aria-keyshortcuts": "Escape", tabIndex: -1, className: "aksel-lookup__content" },
                            react_1.default.createElement(typography_1.BodyShort, { weight: "semibold", className: "aksel-lookup__title", id: popoverTitleId }, word),
                            react_1.default.createElement("div", null, children),
                            react_1.default.createElement(button_1.Button, { type: "button", className: "aksel-lookup__close-button", size: "xsmall", variant: "tertiary", "data-color": "neutral", icon: react_1.default.createElement(aksel_icons_1.XMarkIcon, { title: translate("close") }), onClick: () => _setOpen(false) })))))))));
});
exports.default = exports.Lookup;
//# sourceMappingURL=Lookup.js.map