"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.ZIndex = void 0;var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));var _react = _interopRequireDefault(require("react"));
var _globalObject = require("@skbkontur/global-object");
var _lodash = _interopRequireDefault(require("lodash.isequal"));
var _warning = _interopRequireDefault(require("warning"));

var _callChildRef = require("../../lib/callChildRef/callChildRef");

var _rootNode = require("../../lib/rootNode");
var _createPropsGetter = require("../../lib/createPropsGetter");
var _isInstanceOf = require("../../lib/isInstanceOf");
var _Loader = require("../../components/Loader");
var _RenderContainer = require("../RenderContainer");


var _ZIndexStorage = require("./ZIndexStorage");var _excluded = ["style", "children", "delta", "priority", "applyZIndex", "coverChildren", "createStackingContext", "wrapperRef", "useWrapper"];var _class, _ZIndex;

var DEFAULT_ZINDEX_CONTEXT = { parentLayerZIndex: 0, maxZIndex: Infinity };

var ZIndexContext = /*#__PURE__*/_react.default.createContext(DEFAULT_ZINDEX_CONTEXT);

ZIndexContext.displayName = 'ZIndexContext';var




































ZIndex = exports.ZIndex = (0, _rootNode.rootNode)(_class = (_ZIndex = /*#__PURE__*/function (_React$Component) {



































  function ZIndex(props) {var _this;
    _this = _React$Component.call(this, props) || this;_this.state = { zIndex: 0, DOMZIndexContext: null };_this.getProps = (0, _createPropsGetter.createPropsGetter)(ZIndex.defaultProps);_this.zIndexContext = null;_this.












































































    wrapperRef = function (element) {
      var wrapperRef = _this.props.wrapperRef;
      _this.setRootNode(element);
      wrapperRef && (0, _callChildRef.callChildRef)(wrapperRef, element);
      element && _this.tryGetContextByDOM(element);
    };_this.















    increment = function () {
      var _this$getProps = _this.getProps(),priority = _this$getProps.priority,delta = _this$getProps.delta;

      return (0, _ZIndexStorage.incrementZIndex)(priority, delta);
    };_this.

    tryGetContextByDOM = function (element) {
      if (
      _this.props.applyZIndex &&
      (0, _lodash.default)(DEFAULT_ZINDEX_CONTEXT, _this.zIndexContext) &&
      _this.state.DOMZIndexContext === null)
      {var _element$parentElemen;
        var DOMZIndexContext = DEFAULT_ZINDEX_CONTEXT;
        var portal = (_element$parentElemen = element.parentElement) == null ? void 0 : _element$parentElemen.closest("[" + _RenderContainer.PORTAL_OUTLET_ATTR + "]");

        if ((0, _isInstanceOf.isInstanceOf)(portal, _globalObject.globalObject.HTMLElement)) {var _globalObject$documen, _noscript$parentEleme;
          var portalID = portal.getAttribute(_RenderContainer.PORTAL_OUTLET_ATTR);
          var noscript = (_globalObject$documen = _globalObject.globalObject.document) == null ? void 0 : _globalObject$documen.querySelector("noscript[" + _RenderContainer.PORTAL_INLET_ATTR + "=\"" + portalID + "\"]");
          var parent = noscript == null || (_noscript$parentEleme = noscript.parentElement) == null ? void 0 : _noscript$parentEleme.closest('[style*=z-index]');

          if ((0, _isInstanceOf.isInstanceOf)(parent, _globalObject.globalObject.HTMLElement)) {var _parent$parentElement;
            var newZIndex = Number(parent.style.zIndex || 0);

            var maxZIndex = Infinity;

            if (((_parent$parentElement = parent.parentElement) == null ? void 0 : _parent$parentElement.dataset.tid) === _Loader.LoaderDataTids.veil) {
              maxZIndex = _this.calcZIndex(newZIndex, maxZIndex);
            }

            DOMZIndexContext = { maxZIndex: maxZIndex, parentLayerZIndex: newZIndex };
          }
        }

        _this.setState({ DOMZIndexContext: DOMZIndexContext });
      }
    };_this.state.zIndex = _this.increment();return _this;}(0, _inheritsLoose2.default)(ZIndex, _React$Component);var _proto = ZIndex.prototype;_proto.validateProps = function validateProps(delta) {(0, _warning.default)((delta || ZIndex.defaultProps.delta) > 0, "[ZIndex]: Prop 'delta' must be greater than 0, received " + delta);(0, _warning.default)(Math.trunc(delta || ZIndex.defaultProps.delta) === delta, "[ZIndex]: Prop 'delta' must be integer, received " + delta);};_proto.componentDidUpdate = function componentDidUpdate(prevProps) {var props = this.getProps();this.validateProps(props.delta);if (prevProps.priority !== props.priority || prevProps.delta !== props.delta) {(0, _ZIndexStorage.removeZIndex)(this.state.zIndex);this.setState({ zIndex: this.increment() });}};_proto.componentWillUnmount = function componentWillUnmount() {(0, _ZIndexStorage.removeZIndex)(this.state.zIndex);};_proto.render = function render() {var _this2 = this;var _this$getProps2 = this.getProps(),style = _this$getProps2.style,children = _this$getProps2.children,delta = _this$getProps2.delta,priority = _this$getProps2.priority,applyZIndex = _this$getProps2.applyZIndex,coverChildren = _this$getProps2.coverChildren,createStackingContext = _this$getProps2.createStackingContext,wrapperRef = _this$getProps2.wrapperRef,useWrapper = _this$getProps2.useWrapper,rest = (0, _objectWithoutPropertiesLoose2.default)(_this$getProps2, _excluded);var wrapperStyle = {};return /*#__PURE__*/_react.default.createElement(ZIndexContext.Consumer, null, function (context) {_this2.zIndexContext = context;var currentZIndexContext = _this2.state.DOMZIndexContext && (0, _lodash.default)(context, DEFAULT_ZINDEX_CONTEXT) ? _this2.state.DOMZIndexContext : context;var parentLayerZIndex = currentZIndexContext.parentLayerZIndex,maxZIndex = currentZIndexContext.maxZIndex;var newZIndexContext = currentZIndexContext;var newZIndex = 0;if (applyZIndex) {newZIndex = _this2.calcZIndex(parentLayerZIndex, maxZIndex);wrapperStyle.zIndex = newZIndex;newZIndexContext = coverChildren ? { parentLayerZIndex: parentLayerZIndex, maxZIndex: newZIndex } : { parentLayerZIndex: newZIndex, maxZIndex: Number.isFinite(maxZIndex) ? newZIndex : Infinity };if (createStackingContext) {(0, _globalObject.isBrowser)(_globalObject.globalObject) && 'isolation' in _globalObject.globalObject.document.body.style ? wrapperStyle.isolation = 'isolate' : wrapperStyle.transform = 'rotate(0)';}}var child = !useWrapper ? children : /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({ style: (0, _extends2.default)({}, style, wrapperStyle), ref: _this2.wrapperRef }, rest), children);return /*#__PURE__*/_react.default.createElement(ZIndexContext.Provider, { value: newZIndexContext }, child);});};_proto.calcZIndex = function calcZIndex(parentLayerZIndex, maxZIndex) {var newZIndex = this.state.zIndex;if (Number.isFinite(maxZIndex)) {var allowedValuesIntervalLength = maxZIndex - parentLayerZIndex;var scale = _ZIndexStorage.upperBorder / allowedValuesIntervalLength;newZIndex = Math.ceil(newZIndex / scale);}newZIndex += parentLayerZIndex;return newZIndex;};return ZIndex;}(_react.default.Component), _ZIndex.priorities = _ZIndexStorage.componentPriorities, _ZIndex.__KONTUR_REACT_UI__ = 'ZIndex', _ZIndex.displayName = 'ZIndex', _ZIndex.defaultProps = { delta: 10, priority: 0, style: {}, applyZIndex: true, coverChildren: false, createStackingContext: false, useWrapper: true }, _ZIndex)) || _class;