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 { listen as listenFocusOutside, containsTargetOrRenderContainer } from '../../lib/listenFocusOutside.js';
import { CommonWrapper } from '../CommonWrapper/index.js';
import { getRootNode, rootNode } from '../../lib/rootNode/index.js';
import { createPropsGetter } from '../../lib/createPropsGetter.js';
import { isInstanceOf } from '../../lib/isInstanceOf.js';
import { withRenderEnvironment } from '../../lib/renderEnvironment/index.js';
var RenderLayer = /** @class */ (function (_super) {
    __extends(RenderLayer, _super);
    function RenderLayer() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.getProps = createPropsGetter(RenderLayer_1.defaultProps);
        _this.focusOutsideListenerToken = null;
        _this.handleFocusOutside = function (event) {
            if (_this.props.onFocusOutside) {
                _this.props.onFocusOutside(event);
            }
        };
        _this.handleNativeDocClick = function (event) {
            var target = event.target || event.srcElement;
            var node = _this.getAnchorNode();
            if (!node || (isInstanceOf(target, _this.globalObject.Element) && containsTargetOrRenderContainer(target)(node))) {
                return;
            }
            if (_this.props.onClickOutside) {
                _this.props.onClickOutside(event);
            }
        };
        return _this;
    }
    RenderLayer_1 = RenderLayer;
    RenderLayer.prototype.componentDidMount = function () {
        if (this.getProps().active) {
            this.attachListeners();
        }
    };
    RenderLayer.prototype.componentDidUpdate = function (prevProps) {
        var active = this.getProps().active;
        if (!prevProps.active && active) {
            this.attachListeners();
        }
        if (prevProps.active && !active) {
            this.detachListeners();
        }
    };
    RenderLayer.prototype.componentWillUnmount = function () {
        if (this.getProps().active) {
            this.detachListeners();
        }
    };
    RenderLayer.prototype.render = function () {
        return (React.createElement(CommonWrapper, __assign({ rootNodeRef: this.setRootNode }, this.props), React.Children.only(this.props.children)));
    };
    RenderLayer.prototype.getAnchorNode = function () {
        var getAnchorElement = this.props.getAnchorElement;
        return getAnchorElement ? getAnchorElement() : getRootNode(this);
    };
    RenderLayer.prototype.attachListeners = function () {
        var _a, _b, _c;
        var node = this.getAnchorNode();
        if (!node) {
            return;
        }
        this.focusOutsideListenerToken = listenFocusOutside(function () { return [node]; }, this.handleFocusOutside, this.globalObject);
        (_b = (_a = this.globalObject).addEventListener) === null || _b === void 0 ? void 0 : _b.call(_a, 'blur', this.handleFocusOutside);
        (_c = this.globalObject.document) === null || _c === void 0 ? void 0 : _c.addEventListener('ontouchstart' in this.globalObject.document.documentElement &&
            'onpointerup' in this.globalObject.document.documentElement
            ? 'pointerup'
            : 'mousedown', this.handleNativeDocClick);
    };
    RenderLayer.prototype.detachListeners = function () {
        var _a, _b, _c;
        if (this.focusOutsideListenerToken) {
            this.focusOutsideListenerToken.remove();
            this.focusOutsideListenerToken = null;
        }
        (_b = (_a = this.globalObject).removeEventListener) === null || _b === void 0 ? void 0 : _b.call(_a, 'blur', this.handleFocusOutside);
        (_c = this.globalObject.document) === null || _c === void 0 ? void 0 : _c.removeEventListener('ontouchstart' in this.globalObject.document.documentElement &&
            'onpointerup' in this.globalObject.document.documentElement
            ? 'pointerup'
            : 'mousedown', this.handleNativeDocClick);
    };
    var RenderLayer_1;
    RenderLayer.__KONTUR_REACT_UI__ = 'RenderLayer';
    RenderLayer.displayName = 'RenderLayer';
    RenderLayer.defaultProps = {
        active: true,
    };
    RenderLayer = RenderLayer_1 = __decorate([
        withRenderEnvironment,
        rootNode
    ], RenderLayer);
    return RenderLayer;
}(React.Component));
export { RenderLayer };
//# sourceMappingURL=RenderLayer.js.map