"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _isFunction = _interopRequireDefault(require("lodash/isFunction"));

var React = _interopRequireWildcard(require("react"));

var _dom = require("../../dom");

var totalLockCount = 0;
var originalOverflow = _dom.canUseDOM ? document.body.style.overflow : '';

var useLockScroll = function useLockScroll(shouldLock) {
  var lock = function lock() {
    if (!totalLockCount) {
      document.body.style.overflow = 'hidden';
    }

    totalLockCount += 1;
  };

  var unlock = function unlock() {
    if (!totalLockCount) return;
    totalLockCount -= 1;

    if (!totalLockCount) {
      document.body.style.overflow = originalOverflow || '';
    }
  };

  React.useEffect(function () {
    if ((0, _isFunction.default)(shouldLock) ? shouldLock() : shouldLock) {
      lock();
      return unlock;
    }
  }, [shouldLock]);
};

var _default = useLockScroll;
exports.default = _default;