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 __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
    if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
    return cooked;
};
import React from 'react';
import { RenderEnvironmentContext } from '../../lib/renderEnvironment/index.js';
import { isBrowser } from '../../lib/globalObject.js';
import { getScrollWidth } from '../../lib/dom/getScrollWidth.js';
var disposeDocumentStyle = null;
var HideBodyVerticalScroll = /** @class */ (function (_super) {
    __extends(HideBodyVerticalScroll, _super);
    function HideBodyVerticalScroll() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.initialScroll = 0;
        _this.master = false;
        _this.restoreStyles = function () {
            if (disposeDocumentStyle) {
                disposeDocumentStyle();
                disposeDocumentStyle = null;
                if (HideBodyVerticalScroll.globalObject.document) {
                    HideBodyVerticalScroll.globalObject.document.documentElement.scrollTop = _this.initialScroll;
                }
            }
        };
        return _this;
    }
    HideBodyVerticalScroll.prototype.componentDidMount = function () {
        var _a, _b, _c;
        var counter = VerticalScrollCounter.increment();
        if (counter === 1) {
            this.master = true;
            this.initialScroll = ((_a = HideBodyVerticalScroll.globalObject.document) === null || _a === void 0 ? void 0 : _a.documentElement)
                ? HideBodyVerticalScroll.globalObject.document.documentElement.scrollTop
                : 0;
            HideBodyVerticalScroll.updateScrollVisibility();
            (_c = (_b = HideBodyVerticalScroll.globalObject).addEventListener) === null || _c === void 0 ? void 0 : _c.call(_b, 'resize', HideBodyVerticalScroll.updateScrollVisibility);
        }
    };
    HideBodyVerticalScroll.prototype.componentDidUpdate = function () {
        if (this.master) {
            HideBodyVerticalScroll.updateScrollVisibility();
        }
    };
    HideBodyVerticalScroll.prototype.componentWillUnmount = function () {
        var _a, _b;
        var counter = VerticalScrollCounter.decrement();
        if (counter === 0) {
            this.restoreStyles();
            (_b = (_a = HideBodyVerticalScroll.globalObject).removeEventListener) === null || _b === void 0 ? void 0 : _b.call(_a, 'resize', HideBodyVerticalScroll.updateScrollVisibility);
        }
    };
    HideBodyVerticalScroll.prototype.render = function () {
        return (React.createElement(RenderEnvironmentContext.Consumer, null, function (_a) {
            var globalObject = _a.globalObject, emotion = _a.emotion;
            HideBodyVerticalScroll.globalObject = globalObject;
            HideBodyVerticalScroll.emotion = emotion;
            return null;
        }));
    };
    HideBodyVerticalScroll.__KONTUR_REACT_UI__ = 'HideBodyVerticalScroll';
    HideBodyVerticalScroll.displayName = 'HideBodyVerticalScroll';
    HideBodyVerticalScroll.updateScrollVisibility = function () {
        var shouldHide = !disposeDocumentStyle;
        if (shouldHide) {
            HideBodyVerticalScroll.hideScroll();
        }
    };
    HideBodyVerticalScroll.hideScroll = function () {
        if (!isBrowser(HideBodyVerticalScroll.globalObject)) {
            return;
        }
        var documentElement = HideBodyVerticalScroll.globalObject.document.documentElement;
        var clientHeight = documentElement.clientHeight, scrollHeight = documentElement.scrollHeight;
        var documentComputedStyle = HideBodyVerticalScroll.globalObject.getComputedStyle(documentElement);
        var scrollbarConst = HideBodyVerticalScroll.globalObject.getComputedStyle(documentElement).overflowY === 'scroll';
        var scrollWidth = clientHeight < scrollHeight || scrollbarConst ? getScrollWidth(HideBodyVerticalScroll.globalObject) : 0;
        var documentMargin = parseFloat(documentComputedStyle.marginRight || '');
        var className = generateDocumentStyle(documentMargin + scrollWidth, HideBodyVerticalScroll.emotion);
        disposeDocumentStyle = HideBodyVerticalScroll.attachStyle(documentElement, className);
    };
    HideBodyVerticalScroll.attachStyle = function (element, className) {
        element.classList.add(className);
        return function () {
            element.classList.remove(className);
        };
    };
    return HideBodyVerticalScroll;
}(React.Component));
export { HideBodyVerticalScroll };
var VerticalScrollCounter = /** @class */ (function () {
    function VerticalScrollCounter() {
    }
    VerticalScrollCounter.increment = function () {
        var globalWithRetailUIVerticalScrollCounter = HideBodyVerticalScroll.globalObject;
        var counter = globalWithRetailUIVerticalScrollCounter.RetailUIVerticalScrollCounter || 0;
        return (globalWithRetailUIVerticalScrollCounter.RetailUIVerticalScrollCounter = counter + 1);
    };
    VerticalScrollCounter.decrement = function () {
        var globalWithRetailUIVerticalScrollCounter = HideBodyVerticalScroll.globalObject;
        var counter = globalWithRetailUIVerticalScrollCounter.RetailUIVerticalScrollCounter || 0;
        return (globalWithRetailUIVerticalScrollCounter.RetailUIVerticalScrollCounter = counter - 1);
    };
    VerticalScrollCounter.get = function () {
        var globalWithRetailUIVerticalScrollCounter = HideBodyVerticalScroll.globalObject;
        return globalWithRetailUIVerticalScrollCounter.RetailUIVerticalScrollCounter || 0;
    };
    return VerticalScrollCounter;
}());
function generateDocumentStyle(documentMargin, emotion) {
    return emotion.css(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n    overflow: hidden !important;\n    margin-right: ", "px !important;\n    height: 100%;\n  "], ["\n    overflow: hidden !important;\n    margin-right: ", "px !important;\n    height: 100%;\n  "])), documentMargin);
}
var templateObject_1;
//# sourceMappingURL=HideBodyVerticalScroll.js.map