import _taggedTemplateLiteralLoose from "@babel/runtime/helpers/esm/taggedTemplateLiteralLoose";
import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
var _HideBodyVerticalScroll, _templateObject;
import React from 'react';
import { globalObject, isBrowser } from '@skbkontur/global-object';
import { getScrollWidth } from "../../../lib/dom/getScrollWidth";
import { css } from "../../../lib/theming/Emotion";
var disposeDocumentStyle = null;
export var HideBodyVerticalScroll = /*#__PURE__*/function (_React$Component) {
  function HideBodyVerticalScroll() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _this.initialScroll = 0;
    _this.master = false;
    _this.restoreStyles = function () {
      if (disposeDocumentStyle) {
        disposeDocumentStyle();
        disposeDocumentStyle = null;
        if (globalObject.document) {
          globalObject.document.documentElement.scrollTop = _this.initialScroll;
        }
      }
    };
    return _this;
  }
  _inheritsLoose(HideBodyVerticalScroll, _React$Component);
  var _proto = HideBodyVerticalScroll.prototype;
  _proto.componentDidMount = function componentDidMount() {
    var counter = VerticalScrollCounter.increment();
    if (counter === 1) {
      var _globalObject$documen;
      this.master = true;
      this.initialScroll = (_globalObject$documen = globalObject.document) != null && _globalObject$documen.documentElement ? globalObject.document.documentElement.scrollTop : 0;
      HideBodyVerticalScroll.updateScrollVisibility();
      globalObject.addEventListener == null || globalObject.addEventListener('resize', HideBodyVerticalScroll.updateScrollVisibility);
    }
  };
  _proto.componentDidUpdate = function componentDidUpdate() {
    if (this.master) {
      HideBodyVerticalScroll.updateScrollVisibility();
    }
  };
  _proto.componentWillUnmount = function componentWillUnmount() {
    var counter = VerticalScrollCounter.decrement();
    if (counter === 0) {
      this.restoreStyles();
      globalObject.removeEventListener == null || globalObject.removeEventListener('resize', HideBodyVerticalScroll.updateScrollVisibility);
    }
  };
  _proto.render = function render() {
    return null;
  };
  return HideBodyVerticalScroll;
}(React.Component);
_HideBodyVerticalScroll = HideBodyVerticalScroll;
HideBodyVerticalScroll.__KONTUR_REACT_UI__ = 'HideBodyVerticalScroll';
HideBodyVerticalScroll.displayName = 'HideBodyVerticalScroll';
HideBodyVerticalScroll.updateScrollVisibility = function () {
  var shouldHide = !disposeDocumentStyle;
  if (shouldHide) {
    _HideBodyVerticalScroll.hideScroll();
  }
};
HideBodyVerticalScroll.hideScroll = function () {
  if (!isBrowser(globalObject)) {
    return;
  }
  var documentElement = globalObject.document.documentElement;
  var clientHeight = documentElement.clientHeight,
    scrollHeight = documentElement.scrollHeight;
  var documentComputedStyle = globalObject.getComputedStyle(documentElement);
  var scrollbarConst = globalObject.getComputedStyle(documentElement).overflowY === 'scroll';
  var scrollWidth = clientHeight < scrollHeight || scrollbarConst ? getScrollWidth() : 0;
  var documentMargin = parseFloat(documentComputedStyle.marginRight || '');
  var className = generateDocumentStyle(documentMargin + scrollWidth);
  disposeDocumentStyle = _HideBodyVerticalScroll.attachStyle(documentElement, className);
};
HideBodyVerticalScroll.attachStyle = function (element, className) {
  element.classList.add(className);
  return function () {
    element.classList.remove(className);
  };
};
var VerticalScrollCounter = function VerticalScrollCounter() {};
VerticalScrollCounter.increment = function () {
  var globalWithRetailUIVerticalScrollCounter = globalObject;
  var counter = globalWithRetailUIVerticalScrollCounter.RetailUIVerticalScrollCounter || 0;
  return globalWithRetailUIVerticalScrollCounter.RetailUIVerticalScrollCounter = counter + 1;
};
VerticalScrollCounter.decrement = function () {
  var globalWithRetailUIVerticalScrollCounter = globalObject;
  var counter = globalWithRetailUIVerticalScrollCounter.RetailUIVerticalScrollCounter || 0;
  return globalWithRetailUIVerticalScrollCounter.RetailUIVerticalScrollCounter = counter - 1;
};
VerticalScrollCounter.get = function () {
  var globalWithRetailUIVerticalScrollCounter = globalObject;
  return globalWithRetailUIVerticalScrollCounter.RetailUIVerticalScrollCounter || 0;
};
function generateDocumentStyle(documentMargin) {
  return css(_templateObject || (_templateObject = _taggedTemplateLiteralLoose(["\n    overflow: hidden !important;\n    margin-right: ", "px !important;\n    height: 100%;\n  "])), documentMargin);
}