"use strict";

exports.__esModule = true;
exports["default"] = void 0;

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

var _excluded = ["options", "children"];

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; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var defaultConfig = {
  // threshold属性决定了什么时候触发回调函数。它是一个数组，每个成员都是一个门槛值，默认为[0]，即交叉比例（intersectionRatio）达到0时触发回调函数
  threshold: [0],
  // 1.0
  // 祖先容器
  root: null,
  // 根元素的margin，用来扩展或缩小rootBounds这个矩形的大小，从而影响intersectionRect交叉区域的大小
  rootMargin: '0px'
};

function ContentVisibility(_ref) {
  var _ref$options = _ref.options,
      options = _ref$options === void 0 ? {} : _ref$options,
      children = _ref.children,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  var _useState = (0, _react.useState)(null),
      element = _useState[0],
      setElement = _useState[1];

  var _useState2 = (0, _react.useState)(false),
      isVisible = _useState2[0],
      setIsVisible = _useState2[1];

  var observer = null; // 可见性变化,参数entry:IntersectionObserverEntry对象提供目标元素的信息,一共有六个属性

  /*
    {
      isIntersecting: true, // 是否交叉(显示)
      time: 3893.92,
      rootBounds: ClientRect {
        bottom: 920,
        height: 1024,
        left: 0,
        right: 1024,
        top: 0,
        width: 920
      },
      boundingClientRect: ClientRect {
        // ...
      },
      intersectionRect: ClientRect {
        // ...
      },
      intersectionRatio: 0.54,
      target: element
    }*/

  var visibilityCallBack = function visibilityCallBack(_ref2) {
    var entry = _ref2[0];
    console.log(entry);

    if (entry.isIntersecting) {
      // if (entry.boundingClientRect.top < 100) {
      console.log('显示');
      console.log(element);
      setIsVisible(true); // 关闭观察器
      // observer.disconnect()
    } else {
      console.log('隐藏');
      console.log(element);
      setIsVisible(false);
    }
  };

  (0, _react.useEffect)(function () {
    if (!element) {
      return;
    } // 创建观察器
    // eslint-disable-next-line


    observer = new IntersectionObserver(visibilityCallBack, _extends({}, defaultConfig, options)); // 开始观察

    observer.observe(element); // 关闭观察器

    return function () {
      return observer && observer.disconnect && observer.disconnect();
    };
  }, [element]); // eslint-disable-line

  return /*#__PURE__*/_react["default"].createElement("div", _extends({
    ref: setElement
  }, others), isVisible ? children : null);
}

var _default = ContentVisibility;
exports["default"] = _default;
module.exports = exports.default;