"use strict";

exports.__esModule = true;
exports.default = useVisibilityChange;

var _react = require("react");

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

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function useVisibilityChange(target, onChange) {
  (0, _react.useEffect)(function () {
    // compatibility: https://caniuse.com/#feat=intersectionobserver
    if (!_utils.inBrowser || !window.IntersectionObserver) {
      return null;
    }

    var observer = new IntersectionObserver(function (entries) {
      // visibility changed
      onChange(entries[0].intersectionRatio > 0);
    }, {
      root: document.body
    });

    var observe = function observe() {
      if (target.current) {
        observer.observe(target.current);
      }
    };

    var unobserve = function unobserve() {
      if (target.current) {
        observer.unobserve(target.current);
      }
    };

    observe();
    return unobserve;
  }, [target.current]);
}