import isFunction from 'lodash/isFunction';
import * as React from 'react';
import { canUseDOM } from '../../dom';
var totalLockCount = 0;
var originalOverflow = 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 (isFunction(shouldLock) ? shouldLock() : shouldLock) {
      lock();
      return unlock;
    }
  }, [shouldLock]);
};

export default useLockScroll;