"use strict";

exports.__esModule = true;
exports.default = useLockScroll;

var _react = require("react");

var _useEventListener = require("./use-event-listener");

var _useScrollParent = require("./use-scroll-parent");

var _useTouch = _interopRequireDefault(require("./use-touch"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* eslint-disable no-plusplus */
var totalLockCount = 0;
var BODY_LOCK_CLASS = 'rv-overflow-hidden'; // 移植自vant：https://github.com/youzan/vant/blob/HEAD/src/composables/use-lock-scroll.ts

function useLockScroll(rootRef, shouldLock) {
  var touch = (0, _useTouch.default)();

  var onTouchMove = function onTouchMove(event) {
    touch.move(event);
    var direction = touch.deltaY > 0 ? '10' : '01';
    var el = (0, _useScrollParent.getScrollParent)(event.target, rootRef.current);
    if (!el) return;
    var scrollHeight = el.scrollHeight,
        offsetHeight = el.offsetHeight,
        scrollTop = el.scrollTop;
    var status = '11';

    if (scrollTop === 0) {
      status = offsetHeight >= scrollHeight ? '00' : '01';
    } else if (scrollTop + offsetHeight >= scrollHeight) {
      status = '10';
    }

    if (status !== '11' && touch.isVertical() && // eslint-disable-next-line no-bitwise
    !(parseInt(status, 2) & parseInt(direction, 2))) {
      if (event.cancelable) {
        event.preventDefault();
      }
    }
  };

  var lock = function lock() {
    document.addEventListener('touchstart', touch.start);
    document.addEventListener('touchmove', onTouchMove, _useEventListener.supportsPassive ? {
      passive: false
    } : false);

    if (!totalLockCount) {
      document.body.classList.add(BODY_LOCK_CLASS);
    }

    totalLockCount++;
  };

  var unlock = function unlock() {
    if (totalLockCount) {
      document.removeEventListener('touchstart', touch.start);
      document.removeEventListener('touchmove', onTouchMove);
      totalLockCount--;

      if (!totalLockCount) {
        document.body.classList.remove(BODY_LOCK_CLASS);
      }
    }
  };

  (0, _react.useEffect)(function () {
    if (shouldLock) {
      lock();
    }

    return function () {
      if (shouldLock) unlock();
    };
  }, [shouldLock]);
}