"use strict";

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

function _react() {
  const data = _interopRequireWildcard(require("react"));

  _react = function () {
    return data;
  };

  return data;
}

var _utils = require("../utils");

var _getTargetElement = require("../utils/dom/getTargetElement");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (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; }

function isInViewPort(el) {
  if (!el) {
    return undefined;
  }

  const viewPortWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  const viewPortHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
  const rect = el.getBoundingClientRect();

  if (rect) {
    const {
      top,
      bottom,
      left,
      right
    } = rect;
    return bottom > 0 && top <= viewPortHeight && left <= viewPortWidth && right > 0;
  }

  return false;
}

function useInViewport(target) {
  const [inViewPort, setInViewport] = (0, _react().useState)(() => {
    if (!_utils.inBrowser || !window.IntersectionObserver) return true;
    const el = (0, _getTargetElement.getTargetElement)(target);
    return isInViewPort(el);
  });
  (0, _react().useEffect)(() => {
    const el = (0, _getTargetElement.getTargetElement)(target); // compatibility: https://caniuse.com/#feat=intersectionobserver

    if (!el || !_utils.inBrowser || !window.IntersectionObserver) {
      return () => {};
    }

    const observer = new IntersectionObserver(entries => {
      entries.some(entry => {
        if (entry.intersectionRatio > 0) {
          setInViewport(true);
          observer.disconnect();
          return true;
        }

        return false;
      });
    }, {
      threshold: [0.0001]
    });
    observer.observe(el);
    return () => {
      observer.disconnect();
    };
  }, [target]);
  return inViewPort;
}

var _default = useInViewport;
exports.default = _default;