'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var useBrowserLayoutEffect = require('@prom-ui/core/useBrowserLayoutEffect');

function _interopNamespaceCompat(e) {
    if (e && typeof e === 'object' && 'default' in e) return e;
    var n = Object.create(null);
    if (e) {
        Object.keys(e).forEach(function (k) {
            if (k !== 'default') {
                var d = Object.getOwnPropertyDescriptor(e, k);
                Object.defineProperty(n, k, d.get ? d : {
                    enumerable: true,
                    get: function () { return e[k]; }
                });
            }
        });
    }
    n.default = e;
    return Object.freeze(n);
}

var React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);

var testPlatform = function testPlatform(re) {
  var _window$navigator$use;
  return typeof window !== 'undefined' && window.navigator != null ?
  // @ts-ignore
  re.test(((_window$navigator$use = window.navigator['userAgentData']) === null || _window$navigator$use === void 0 ? void 0 : _window$navigator$use.platform) || window.navigator.platform) : false;
};
var isMac = function isMac() {
  return testPlatform(/^Mac/i);
};
var isIPhone = function isIPhone() {
  return testPlatform(/^iPhone/i);
};
var isIPad = function isIPad() {
  return testPlatform(/^iPad/i) ||
  // iPadOS 13 lies and says it's a Mac, but we can distinguish by detecting touch support.
  isMac() && navigator.maxTouchPoints > 1;
};
var isIOS = function isIOS() {
  return isIPhone() || isIPad();
};
var preventScroll = function preventScroll() {
  var style = document.createElement('style');
  if (isIOS()) {
    style.dataset.qaid = 'body-scroll-lock-ios';
    style.dataset.testid = 'body-scroll-lock-ios';
    style.textContent = "\n            html {\n                overflow: hidden !important;\n                overscroll-behavior: none !important;\n                touch-action: none !important;\n                -webkit-overflow-scrolling: none !important;\n            }\n            html * {\n                overscroll-behavior: contain;\n            }\n        ";
  } else {
    style.dataset.qaid = 'body-scroll-lock-standart';
    style.dataset.testid = 'body-scroll-lock-standart';
    style.textContent = "\n            html {\n                padding-right: ".concat(window.innerWidth - document.documentElement.clientWidth, "px !important;\n                overflow: hidden !important;\n                overscroll-behavior: none !important;\n            }\n        ");
  }
  document.head.appendChild(style);
  return function () {
    return style.remove();
  };
};

var preventScrollCount = 0;
var preventRestore;
var BodyScrollLock = function BodyScrollLock(_ref) {
  var _ref$isActive = _ref.isActive,
    isActive = _ref$isActive === void 0 ? true : _ref$isActive;
  useBrowserLayoutEffect.useBrowserLayoutEffect(function () {
    if (!isActive) {
      return function () {};
    }
    preventScrollCount++;
    if (preventScrollCount === 1) {
      preventRestore = preventScroll();
    }
    return function () {
      preventScrollCount--;
      if (preventScrollCount === 0) {
        preventRestore();
      }
    };
  }, [isActive]);
  return /*#__PURE__*/React__namespace.createElement(React__namespace.Fragment, null);
};
BodyScrollLock.displayName = 'BodyScrollLock';

exports.BodyScrollLock = BodyScrollLock;

require('./style.development.v2.css');
