var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import React from 'react';
import { CommonWrapper } from '../../internal/CommonWrapper/index.js';
import { listen as listenFocusOutside, containsTargetOrRenderContainer } from '../../lib/listenFocusOutside.js';
import { getRootNode, rootNode } from '../../lib/rootNode/index.js';
import { isInstanceOf } from '../../lib/isInstanceOf.js';
import { withRenderEnvironment } from '../../lib/renderEnvironment/index.js';
var FocusTrap = /** @class */ (function (_super) {
    __extends(FocusTrap, _super);
    function FocusTrap() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.focusOutsideListenerToken = null;
        _this.onClickOutside = function (e) {
            if (_this.props.onBlur) {
                _this.props.onBlur(e);
            }
            _this.detachListeners();
        };
        _this.attachListeners = function () {
            var _a;
            var rootNode = getRootNode(_this);
            if (!_this.focusOutsideListenerToken && rootNode) {
                _this.focusOutsideListenerToken = listenFocusOutside([rootNode], _this.onClickOutside, _this.globalObject);
                (_a = _this.globalObject.document) === null || _a === void 0 ? void 0 : _a.addEventListener('ontouchstart' in _this.globalObject.document.documentElement ? 'touchstart' : 'mousedown', _this.handleNativeDocClick);
            }
        };
        _this.handleNativeDocClick = function (event) {
            var target = event.target || event.srcElement;
            var node = getRootNode(_this);
            if (node && isInstanceOf(target, _this.globalObject.Element) && containsTargetOrRenderContainer(target)(node)) {
                return;
            }
            _this.onClickOutside(event);
        };
        return _this;
    }
    FocusTrap.prototype.componentWillUnmount = function () {
        if (this.focusOutsideListenerToken) {
            this.detachListeners();
        }
    };
    FocusTrap.prototype.render = function () {
        var _this = this;
        var _a = this.props, children = _a.children, onBlur = _a.onBlur;
        return (React.createElement(CommonWrapper, __assign({ rootNodeRef: this.setRootNode }, this.props), React.cloneElement(React.Children.only(children), {
            onFocus: function () {
                var _a;
                var args = [];
                for (var _i = 0; _i < arguments.length; _i++) {
                    args[_i] = arguments[_i];
                }
                if (onBlur) {
                    _this.attachListeners();
                }
                if (children.props && children.props.onFocus) {
                    (_a = children.props).onFocus.apply(_a, args);
                }
            },
        })));
    };
    FocusTrap.prototype.detachListeners = function () {
        var _a;
        if (this.focusOutsideListenerToken) {
            this.focusOutsideListenerToken.remove();
            this.focusOutsideListenerToken = null;
            (_a = this.globalObject.document) === null || _a === void 0 ? void 0 : _a.removeEventListener('ontouchstart' in this.globalObject.document.documentElement ? 'touchstart' : 'mousedown', this.handleNativeDocClick);
        }
    };
    FocusTrap.__KONTUR_REACT_UI__ = 'FocusTrap';
    FocusTrap.displayName = 'FocusTrap';
    FocusTrap = __decorate([
        withRenderEnvironment,
        rootNode
    ], FocusTrap);
    return FocusTrap;
}(React.PureComponent));
export { FocusTrap };
//# sourceMappingURL=FocusTrap.js.map