"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FocusTrap = void 0;
const react_1 = __importStar(require("react"));
const focus_1 = require("../../utilities/focus");
const FocusTrap_css_1 = __importDefault(require("./FocusTrap.css"));
exports.FocusTrap = ({ children, onContainerFocus }) => {
    const firstBumperRef = react_1.useRef(null);
    const lastBumperRef = react_1.useRef(null);
    const contentRef = react_1.useRef(null);
    react_1.useLayoutEffect(() => {
        var _a;
        (_a = contentRef.current) === null || _a === void 0 ? void 0 : _a.focus({ preventScroll: true });
    }, []);
    const moveFocus = (forward) => {
        const content = contentRef.current;
        if (content) {
            const focusableNode = forward
                ? focus_1.findFirstFocusableNode(content)
                : focus_1.findLastFocusableNode(content);
            focusableNode ? focusableNode.focus() : content.focus();
        }
    };
    return (<>
      <button className={FocusTrap_css_1.default.Bumper} ref={firstBumperRef} aria-hidden="true" onFocus={() => moveFocus(false)}/>

      {react_1.default.cloneElement(children, {
        tabIndex: -1,
        ref: contentRef,
        onFocus: onContainerFocus,
    })}

      <button className={FocusTrap_css_1.default.Bumper} ref={lastBumperRef} aria-hidden="true" onFocus={() => moveFocus(true)}/>
    </>);
};
