text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.default = void 0;
  
  var _react = _interopRequireDefault(require("react"));
  
  var _rxjs = require("rxjs");
  
  var _operators = require("rxjs/operators");
  
  var _intersectionObservableFor = _interopRequireDefault(require("../streams/intersectionObservableFor"));
  
  var _visibilityChange = _interopRequireDefault(require("../streams/visibilityChange"));
  
  var _excluded = ["children", "style", "element", "hideDelay"];
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
  
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
  
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
  
  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; }
  
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  
  var isVisible$ = _visibilityChange.default.pipe((0, _operators.map)(event => !event.target.hidden));
  
  var STYLE = {
    minHeight: 1,
    minWidth: 1 // A stream of booleans to signal whether preview component should keep
    // subscriptions active or not
  
  };
  var documentVisibility$ = (0, _rxjs.concat)((0, _rxjs.defer)(() => (0, _rxjs.of)(!document.hidden)), isVisible$).pipe((0, _operators.distinctUntilChanged)(), (0, _operators.publishReplay)(1), (0, _operators.refCount)());
  
  class WithVisibility extends _react.default.Component {
    constructor() {
      super(...arguments);
  
      _defineProperty(this, "element", /*#__PURE__*/_react.default.createRef());
  
      _defineProperty(this, "subscription", null);
  
      _defineProperty(this, "state", {
        isVisible: false
      });
    }
  
    componentDidMount() {
      var _this$props$hideDelay = this.props.hideDelay,
          hideDelay = _this$props$hideDelay === void 0 ? 0 : _this$props$hideDelay;
  
      if (!this.element.current) {
        return;
      }
  
      var inViewport$ = (0, _intersectionObservableFor.default)(this.element.current).pipe((0, _operators.map)(event => event.isIntersecting));
      this.subscription = documentVisibility$.pipe((0, _operators.switchMap)(isVisible => isVisible ? inViewport$ : (0, _rxjs.of)(false)), (0, _operators.switchMap)(isVisible => isVisible ? (0, _rxjs.of)(true) : (0, _rxjs.of)(false).pipe((0, _operators.delay)(hideDelay))), (0, _operators.distinctUntilChanged)(), (0, _operators.tap)(isVisible => {
        this.setState({
          isVisible
        });
      })).subscribe();
    }
  
    componentWillUnmount() {
      if (this.subscription) {
        this.subscription.unsubscribe();
        this.subscription = null;
      }
    }
  
    render() {
      var isVisible = this.state.isVisible;
  
      var _this$props = this.props,
          children = _this$props.children,
          _this$props$style = _this$props.style,
          style = _this$props$style === void 0 ? {} : _this$props$style,
          _this$props$element = _this$props.element,
          element = _this$props$element === void 0 ? 'span' : _this$props$element,
          hideDelay = _this$props.hideDelay,
          rest = _objectWithoutProperties(_this$props, _excluded);
  
      return /*#__PURE__*/_react.default.createElement(element, _objectSpread({
        ref: this.element,
        style: _objectSpread(_objectSpread({}, STYLE), style)
      }, rest), // Render a nonbreaking space here because of https://bugs.chromium.org/p/chromium/issues/detail?id=972196
      isVisible ? children(isVisible) : '\u00A0' // &nbsp
      );
    }
  
  }
  
  exports.default = WithVisibility;