"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var NO_SCROLL_CLASS = 'sg-dialog-no-scroll';
var DIALOG_SELECTOR = '.js-dialog';

function useBodyNoScroll(overlayRef) {
  var cleanupRef = React.useRef(null);
  var forceCleanup = React.useCallback(function () {
    if (cleanupRef.current) cleanupRef.current();
  }, []);
  React.useEffect(function () {
    var _overlayRef$current, _overlayRef$current$p;

    // @todo Use React Context API for detecting nested components
    // https://github.com/brainly/style-guide/issues/2795
    var isNestedDialog = (_overlayRef$current = overlayRef.current) === null || _overlayRef$current === void 0 ? void 0 : (_overlayRef$current$p = _overlayRef$current.parentElement) === null || _overlayRef$current$p === void 0 ? void 0 : _overlayRef$current$p.closest(DIALOG_SELECTOR);

    if (isNestedDialog) {
      // if dialog is nested, this logic was already fired by parent dialog
      // it prevents an issue with no cleanup when nested dialogs
      return;
    }

    var body = document.body;
    var scrollY = window.scrollY;
    if (!body) return;
    body.style.top = "-".concat(scrollY, "px");
    body.classList.add(NO_SCROLL_CLASS);

    var cleanup = function cleanup() {
      var _overlayRef$current2;

      // it can only be forced once
      cleanupRef.current = null;
      var dialogsOpenCount = document.querySelectorAll(DIALOG_SELECTOR).length;
      var nestedOpenDialogsCount = ((_overlayRef$current2 = overlayRef.current) === null || _overlayRef$current2 === void 0 ? void 0 : _overlayRef$current2.querySelectorAll(DIALOG_SELECTOR).length) | 0; // nested dialogs shouldn't be counted
      // as a parent for nested dialogs, this particular one should perfrom the cleanup

      var notNestedDialogsOpenCount = dialogsOpenCount - nestedOpenDialogsCount;
      var manyDialogsOpened = notNestedDialogsOpenCount > 1;

      if (manyDialogsOpened) {
        return;
      }

      body.style.top = '';
      body.classList.remove(NO_SCROLL_CLASS);
      window.scrollTo(0, scrollY);
    };

    cleanupRef.current = cleanup;
    return cleanup;
  }, [overlayRef]);
  return forceCleanup;
}